- Components
- Menus and selection
- Combobox
Combobox
Combobox combines a text input with a listbox, allowing users to filter a list of options to items matching a query.
Installation
Usage
Use Combobox
to allow users to filter a list of options to items matching a query and select an item from the list.
Options
Label
A visual label can be provided for the combobox using the label
prop, or a hidden label using aria-label
prop.
Description
A description can be supplied to the combobox via the description
prop. The description is always visible unless the validationState
is “invalid”
and an error message is provided.
Contextual help
A ContextualHelp
element may be placed next to the label to provide additional information or help about the Combobox
Validation
An errorMessage
can be supplied to a Combobox
, which will be displayed when the isInvalid
prop is set to true
.
Custom value
By default, Combobox
doesn't allow users to specify a value that doesn't exist in the list of options and will revert the input value to the current selected value on blur. By specifying allowsCustomValue
, this behavior is suppressed.
Disabled
The isDisabled
prop can be used to disable the combobox.
Loading
Use the isLoading
prop to indicate that the list is loading.
Required
Use the isRequired
prop to mark the combobox as required.
Use the necessityIndicator
prop to control the visual style of the required state.
Sections
Combobox supports sections in order to group options. Sections can be used by wrapping groups of items in a Section element.
Sections without a title
must provide an aria-label for accessibility.
Item options
See Item options for more information on the available options.
Uncontrolled
Use defaultSelectedKey
to provide a default set of selected item.
Controlled
Use selectedKey
to control the selected item.
You selected: tn
Composition
If you need to customize things further, you can drop down to the composition level.
Examples
Asynchronous loading
API Reference
Prop | Type | Default | Description |
---|---|---|---|
defaultFilter | (textValue: string, inputValue: string) => boolean | - | The filter function used to determine if a option should be included in the combo box list. |
formValue | 'text' | 'key' | - | Whether the text or key of the selected item is submitted as part of an HTML form. When allowsCustomValue is true, this option does not apply and the text is always submitted. |
allowsEmptyCollection | boolean | - | Whether the combo box allows the menu to be open when the collection is empty. |
shouldFocusWrap | boolean | - | Whether keyboard navigation is circular. |
defaultItems | Iterable<T> | - | The list of ComboBox items (uncontrolled). |
items | Iterable<T> | - | The list of ComboBox items (controlled). |
inputValue | string | - | The value of the ComboBox input (controlled). |
defaultInputValue | string | - | The default value of the ComboBox input (uncontrolled). |
allowsCustomValue | boolean | - | Whether the ComboBox allows a non-item matching input value to be set. |
menuTrigger | 'focus' | 'input' | 'manual' | 'input' | The interaction required to display the ComboBox menu. |
disabledKeys | Iterable<Key> | - | The item keys that are disabled. These items cannot be selected, focused, or otherwise interacted with. |
selectedKey | Key | null | - | The currently selected key in the collection (controlled). |
defaultSelectedKey | Key | - | The initial selected key in the collection (uncontrolled). |
isDisabled | boolean | - | Whether the input is disabled. |
isReadOnly | boolean | - | Whether the input can be selected but not changed by the user. |
isRequired | boolean | - | Whether user input is required on the input before form submission. |
isInvalid | boolean | - | Whether the input value is invalid. |
validate | (value: ComboBoxValidationValue) => ValidationError | true | null | undefined | - | A function that returns an error message if a given value is invalid. Validation errors are displayed to the user when the form is submitted if validationBehavior="native". For realtime validation, use the isInvalid prop instead. |
autoFocus | boolean | - | Whether the element should receive focus on render. |
name | string | - | The name of the input element, used when submitting an HTML form. |
validationBehavior | 'native' | 'aria' | 'native' | Whether to use native HTML form validation to prevent form submission when the value is missing or invalid, or mark the field as required or invalid via ARIA. |
children | ReactNode | (values: ComboBoxRenderProps & {defaultChildren: ReactNode | undefined}) => ReactNode | - | The children of the component. A function may be provided to alter the children based on component state. |
className | string | - | The CSS className for the element. |
style | CSSProperties | (values: ComboBoxRenderProps & {defaultStyle: CSSProperties}) => CSSProperties | - | The inline style for the element. A function may be provided to compute the style based on component state. |
Event | Type | Description |
---|---|---|
onOpenChange | (isOpen: boolean) => void | Method that is called when the open state of the menu changes. |
onSelectionChange | (key: Key) => void | Handler that is called when the selection changes. |
onInputChange | (value: string) => void | Handler that is called when the ComboBox input value changes. |
onFocus | (e: FocusEvent<Target>) => void | Handler that is called when the element receives focus. |
onBlur | (e: FocusEvent<Target>) => void | Handler that is called when the element loses focus. |
onFocusChange | (isFocused: boolean) => void | Handler that is called when the element's focus status changes. |
onKeyDown | (e: KeyboardEvent) => void | Handler that is called when a key is pressed. |
onKeyUp | (e: KeyboardEvent) => void | Handler that is called when a key is released. |
Data attribute | Description |
---|---|
[data-open] | Whether the combobox is currently open. |
[data-disabled] | Whether the combobox is disabled. |
[data-invalid] | Whether the combobox is invalid. |
[data-required] | Whether the combobox is required. |
Accessibility
Keyboard interactions
Key | Description |
---|---|
Typing | Initiates autocomplete or popover (unless suppressed). |
ArrowDown ArrowUp | If the popover is unsuppressed and not already open, the down arrow opens the popover menu. |
Esc | If the popover is open, close the popover. |