Combobox

lightning:combobox

A widget that provides an input field that is readonly, accompanied with a dropdown list of selectable options.

For Aura components only. For LWC development, use lightning-combobox.

For Use In

Lightning Experience, Experience Builder Sites, Salesforce Mobile App, Lightning Out (Beta), Standalone Lightning App, Mobile Offline

lightning:combobox is an input element that enables single selection from a list of options. The result of the selection is stored as the value of the input. Multiple selection is currently not supported. To support multiple selection, use lightning:dualListbox instead.

This component implements styling from combobox in the Lightning Design System.

This example creates a list of options during init, with a default selection that’s specified with the value attribute. The options attribute specifies the name of an array of items for the dropdown list.

1<aura:component>
2  <aura:attribute name="statusOptions" type="List" default="[]" />
3  <aura:handler name="init" value="{! this }" action="{! c.loadOptions }" />
4  <lightning:combobox
5    aura:id="selectItem"
6    name="status"
7    label="Status"
8    placeholder="Choose Status"
9    value="new"
10    onchange="{!c.handleOptionSelected}"
11    options="{!v.statusOptions}"
12  />
13</aura:component>

In your client-side controller, define an array of options and assign it to the statusOptions attribute. Each option corresponds to a list item on the dropdown list. Define the content of each option by specifying a value property and a label property. The label value is displayed for the item in the rendered dropdown list, while the value property’s value is stored when the item is selected.

Define an optional description property to add a line of descriptive text for each option. The descriptive text displays below the label of the list item. When adding descriptions, specify a description for each item in a list. If some items are missing descriptions, the text of the items can be misaligned.

1({
2  loadOptions: function (component, event, helper) {
3    var options = [
4      { value: "new", label: "New", description: "A new item" },
5      {
6        value: "in-progress",
7        label: "In Progress",
8        description: "Currently working on this item",
9      },
10      {
11        value: "finished",
12        label: "Finished",
13        description: "Done working on this item",
14      },
15    ];
16    component.set("v.statusOptions", options);
17  },
18  handleChange: function (cmp, event) {
19    // Get the string of the "value" attribute on the selected option
20    var selectedOptionValue = event.getParam("value");
21    alert("Option selected with value: '" + selectedOptionValue + "'");
22  },
23});

Selecting an option triggers the onchange event, which calls the handleChange client-side controller. To check which option has been clicked, use event.getParam("value"). Calling cmp.find("mycombobox").get("v.value"); returns the currently selected option.

Append an SLDS Icon 

By default, the combobox options don’t include an icon. To include an SLDS icon on the options, use the button variant. This variant supports options with and without icons. Consider these icon behaviors:

  • The icon is placed before the option label
  • The icon size is xx-small (14 px)
  • Each option can have a unique SLDS icon

Visit icons to view the available icons.

To create a combobox with a unique SLDS icon on each option, use the button variant with the iconName property on the options.

1<aura:component>
2  <aura:attribute name="options" type="List" default="[
3      {'label': 'Slack', 'value': 'new', iconName: 'standard:slack'},
4      {'label': 'In Progress', 'value': 'inProgress', iconName: 'utility:filter'},
5      {'label': 'Finished', 'value': 'finished', iconName: 'utility:archive'},
6      ]"/>
7  <lightning:combobox
8    variant="button"
9    name="progress"
10    label="Status"
11    value="inProgress"
12    placeholder="Select Progress"
13    options="{! v.options }" />
14</aura:component>

When you select an option from the list, the icon and label are shown on the button.

Input Validation 

Client-side input validation is available for this component. You can require the user to make a selection by setting required="true". An error message is automatically displayed when an item is not selected and required="true".

To check the validity states of an input, use the validity attribute, which is based on the ValidityState object. You can access the validity states in your client-side controller. This validity attribute returns an object with boolean properties.

You can override the default message by providing your own value for messageWhenValueMissing.

To programmatically display error messages on invalid fields, use the reportValidity() method. For custom validity error messages, display the message using setCustomValidity() and reportValidity(). For more information, see the lightning:input documentation.

Usage Considerations 

Special characters like " must be escaped. For example, you want to display "New".

1var options = [
2  { value: '"new"', label: '"New"' },
3  { value: "expired", label: "Expired" },
4];

When using single quotes in your value, escape the quote with a double slash instead of a single slash.

lightning:combobox doesn’t currently support autocomplete or typeahead. The autocomplete attribute is reserved for internal use.

In Lightning Experience, a lightning:combobox dropdown list that’s opened overlays the record edit page or modal, the global header, and record form footer when scrolling.

On mobile devices, lightning:combobox has the following limitations.

  • The dropdown menu doesn’t scroll correctly when there isn’t enough room to display the complete list of options.
  • The mobile viewport doesn’t display the dropdown menu correctly especially if the component is placed near the bottom of the page.

We recommend using the lightning:select component on mobile instead.

Accessibility 

You must provide a text label for accessibility to make the information available to assistive technology. The label attribute creates an HTML label element for your input component. To hide a label from view and make it available to assistive technology, use the label-hidden variant.

This component uses button elements for select-only comboboxes to comply with the Lightning Design System combobox blueprint.

When an option label is too long to fit in a single line, the label wraps and continues in a new line. If a long word in an option label isn’t fully visible, scroll horizontally on the dropdown list to reveal the whole word.

Attributes 

NameDescriptionTypeDefaultRequired
accesskeySpecifies a shortcut key to activate or focus an element.String
autocompleteReserved for internal use. Controls auto-filling of the field.String
bodyThe body of the component. In markup, this is everything in the body of the tag.Aura.Component[]
classA CSS class for the outer element, in addition to the component's base classes.String
disabledSpecifies that an input element should be disabled. This value defaults to false.Booleanfalse
dropdownAlignmentSpecifies where the drop-down list is aligned with or anchored to the selection field. By default the list is aligned with the selection field at the top so the list opens down. Use bottom-left to make the selection field display at the bottom so the list opens above it. Use auto to let the component determine where to open the list based on space available.Stringleft
fieldLevelHelpHelp text detailing the purpose and function of the combobox.String
labelText label for the combobox.String
messageWhenValueMissingError message to be displayed when the value is missing and input is required.String
nameSpecifies the name of an input element.String
onblurThe action triggered when the element releases focus.Aura.Action
onchangeThe action triggered when a value attribute changes.Aura.Action
onfocusThe action triggered when the element receives focus.Aura.Action
optionsA list of options that are available for selection. Each option has the following attributes: label and value.Object[]
placeholderText that is displayed before an option is selected, to prompt the user to select an option. The default is "Select an Option".StringSelect an Option
readonlySpecifies that an input field is read-only. This value defaults to false.Booleanfalse
requiredSpecifies that an input field must be filled out before submitting the form. This value defaults to false.Booleanfalse
spinnerActiveDisplays a spinner to indicate activity in the dropdown list. This value defaults to false.Boolean
tabindexSpecifies the tab order of an element when the Tab key is used for navigating. The tabindex value can be set to 0 or -1. The default is 0, which means that the component is focusable and participates in sequential keyboard navigation. -1 means that the component is focusable but does not participate in keyboard navigation.Integer
titleDisplays tooltip text when the mouse moves over the element.String
validityRepresents the validity states that an element can be in, with respect to constraint validation.Object
valueSpecifies the value of an input element.Object
variantThe variant changes the appearance of an input field. Accepted variants include standard, label-inline, label-hidden, and label-stacked. This value defaults to standard, which displays the label above the field. Use label-hidden to hide the label but make it available to assistive technology. Use label-inline to horizontally align the label and input field. Use label-stacked to place the label above the input field.Stringstandard

Methods 

NameDescriptionArgument NameArgument TypeArgument Description
checkValidityReturns the valid property value (Boolean) on the ValidityState object to indicate whether the combobox has any validity errors.
focusSets focus on the element.
reportValidityDisplay error messages if the combobox is invalid.
setCustomValiditySets a custom error message to be displayed when the combobox value is submitted.messageStringThe string that describes the error. If message is an empty string, the error message is reset.
showHelpMessageIfInvalidShows the help message if the form control is in an invalid state.