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.
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 option20 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:
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".
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.
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
Name
Description
Type
Default
Required
accesskey
Specifies a shortcut key to activate or focus an element.
String
autocomplete
Reserved for internal use. Controls auto-filling of the field.
String
body
The body of the component. In markup, this is everything in the body of the tag.
Aura.Component[]
class
A CSS class for the outer element, in addition to the component's base classes.
String
disabled
Specifies that an input element should be disabled. This value defaults to false.
Boolean
false
dropdownAlignment
Specifies 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.
String
left
fieldLevelHelp
Help text detailing the purpose and function of the combobox.
String
label
Text label for the combobox.
String
messageWhenValueMissing
Error message to be displayed when the value is missing and input is required.
String
name
Specifies the name of an input element.
String
onblur
The action triggered when the element releases focus.
Aura.Action
onchange
The action triggered when a value attribute changes.
Aura.Action
onfocus
The action triggered when the element receives focus.
Aura.Action
options
A list of options that are available for selection. Each option has the following attributes: label and value.
Object[]
placeholder
Text that is displayed before an option is selected, to prompt the user to select an option. The default is "Select an Option".
String
Select an Option
readonly
Specifies that an input field is read-only. This value defaults to false.
Boolean
false
required
Specifies that an input field must be filled out before submitting the form. This value defaults to false.
Boolean
false
spinnerActive
Displays a spinner to indicate activity in the dropdown list. This value defaults to false.
Boolean
tabindex
Specifies 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
title
Displays tooltip text when the mouse moves over the element.
String
validity
Represents the validity states that an element can be in, with respect to constraint validation.
Object
value
Specifies the value of an input element.
Object
variant
The 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.
String
standard
Methods
Name
Description
Argument Name
Argument Type
Argument Description
checkValidity
Returns the valid property value (Boolean) on the ValidityState object to indicate whether the combobox has any validity errors.
focus
Sets focus on the element.
reportValidity
Display error messages if the combobox is invalid.
setCustomValidity
Sets a custom error message to be displayed when the combobox value is submitted.
message
String
The string that describes the error. If message is an empty string, the error message is reset.
showHelpMessageIfInvalid
Shows the help message if the form control is in an invalid state.