Combobox

lightning-combobox

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

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 corresponds to the value of the input. Multiple selection is not supported. To support multiple selection, use lightning-dual-listbox instead.

This example creates a list of options 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<template>
2    <lightning-combobox
3        name="status"
4        label="Status"
5        placeholder="Choose Status"
6        value={value}
7        onchange={handleChange}
8        options={statusOptions}
9    >
10    </lightning-combobox>
11    <p>Selected option: {value}</p>
12</template>

Design 

lightning-combobox implements the combobox blueprint in the Salesforce Lightning Design System (SLDS). The combobox adapts to SLDS 1 or SLDS 2 styling based on the org’s theme or the container app that you use.

SLDS 1SLDS 2
DesignComboboxCombobox
For Use InLightning Experience, Experience Builder sites, Salesforce mobile app, Lightning Out (Beta), Standalone Lightning app, Mobile OfflineLightning Experience

Create a Combobox 

In your JavaScript, define an array of options. 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 dropdown list shows the label that you provide by using the label value. Select an item updates its corresponding value property.

Define an optional description property to add a line of descriptive text for each option. The descriptive text appears 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 alignment of the items can appear incorrect.

1import { LightningElement } from "lwc";
2export default class ComboboxExample extends LightningElement {
3  statusOptions = [
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
17  value = "new";
18
19  handleChange(event) {
20    // Get the string of the "value" attribute on the selected option
21    this.value = event.detail.value;
22  }
23}

Selecting an option triggers the change event, which calls the handleChange function. To check which option is clicked, use event.detail.value.

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<lightning-combobox
2    label="Filter"
3    variant="button"
4    value={filterValue}
5    options={filterOptions}
6    onchange={handleFilterChange}>
7</lightning-combobox>

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

1import { LightningElement } from "lwc";
2export default class ComboboxExample extends LightningElement {
3  filterValue = 'all';
4  get filterOptions() {
5      return [
6          { label: 'All Items', value: 'all', iconName: 'utility:filter' },
7          { label: 'Active Only', value: 'active', iconName: 'utility:check' },
8          { label: 'Archived', value: 'archived', iconName: 'utility:archive' },
9          { label: 'Pending', value: 'pending', iconName: 'utility:clock' },
10      ];
11  }
12  handleFilterChange(event) {
13          this.filterValue = event.detail.value;
14  }
15}

To create a combobox that appears pill-like without an icon, use the button variant without specifying iconName on the options.

Input Validation 

Client-side input validation is available for this component. You can require the selection by adding the required attribute. An error message is automatically shown when an item isn’t selected on a required element.

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 JavaScript. This validity attribute returns an object with boolean attributes. See lightning-input for more information.

You can override the default message by providing your own value for message-when-value-missing.

Component Styling 

Use a combination of variants and utility classes to customize your combobox styles.

Variants 

Use the variant attribute with one of these values to apply styling.

  • label-hidden hides the label but make it available to assistive technology
  • label-inline horizontally aligns the label and combobox
  • label-stacked to place the label above the combobox
  • button presents a pill-shaped combobox with icon support. See Append an SLDS Icon.

Utility Classes 

To apply additional styling, use the SLDS utility classes with the class attribute,

Usage Considerations 

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

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

When you use single quotes in the 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 these limitations.

  • The dropdown menu doesn’t scroll correctly when there’s limited space to show the complete list of options.
  • The mobile viewport doesn’t display the dropdown menu correctly, especially if you place the component near the bottom of the page.

We recommend that you use the HTML <select> element on mobile instead.

To enable record lookup in a combobox, use the lightning-record-picker component instead.

Accessibility 

Provide a text label for accessibility to make the information available to assistive technology. The label attribute creates an HTML label element for your 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.

Custom Events 

change

The event fired when an item is selected in the combobox.

The change event returns this parameter.

ParameterTypeDescription
selectedValuestringThe value of the selected option.

The event properties are as follows.

PropertyValueDescription
bubblestrueThis event bubbles up through the DOM.
cancelablefalseThis event has no default behavior that can be canceled. You can’t call preventDefault() on this event.
composedtrueThis event propagates outside of the component in which it was dispatched.

open

The event fired when the dropdown is opened.

The open event doesn’t return any parameters.

PropertyValueDescription
bubblesfalseThis event does not bubble.
cancelablefalseThis event has no default behavior that can be canceled. You can’t call preventDefault() on this event.
composedfalseThis event does not propagate outside of the component in which it was dispatched.

Attributes 

NameDescriptionTypeDefaultRequired
aria-described-byReserved for internal use. Use the standard aria-describedby instead. A space-separated list of element IDs that provide descriptive labels for the combobox.string
aria-invalidA Boolean value for aria-invalid.boolean
aria-labelled-byReserved for internal use. Use the standard aria-labelledby instead. A space-separated list of element IDs that provide labels for the combobox.string
autocompleteReserved for internal use. Controls auto-filling of the field.string
disabledIf present, the combobox is disabled and users cannot interact with it.booleanfalse
dropdown-alignmentSpecifies 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 left 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
field-level-helpHelp text detailing the purpose and function of the combobox.string
icon-nameThe name of the icon displayed in the combobox button. Applies only when variant is 'button'.string
labelText label for the combobox.
message-when-value-missingError message to be displayed when the value is missing and input is required.string
nameSpecifies the name of the combobox.string
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
read-onlyIf present, the combobox is read-only. A read-only combobox is also disabled.booleanfalse
requiredIf present, a value must be selected before the form can be submitted.booleanfalse
spinner-activeIf present, a spinner is displayed below the menu items to indicate loading activity.booleanfalse
validityRepresents the validity states that an element can be in, with respect to constraint validation.object
valueSpecifies the value of an input element.string
variantThe variant changes the appearance of the combobox. Accepted variants include standard, label-hidden, label-inline, label-stacked, and button. This value defaults to standard. Use label-hidden to hide the label but make it available to assistive technology. Use label-inline to horizontally align the label and combobox. Use label-stacked to place the label above the combobox. Use button for a pill-shaped button-like combobox trigger.stringstandard

Methods 

NameDescriptionArgument NameArgument TypeArgument Description
blurRemoves focus from the combobox.
checkValidityReturns the valid attribute value (Boolean) on the ValidityState object.
focusSets focus on the combobox.
reportValidityDisplays the error messages and returns false if the input is invalid. If the input is valid, reportValidity() clears displayed error messages and returns true.
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 combobox is in an invalid state.