useComboBox

Provides the behavior and accessibility implementation for a combo box component. A combo box combines a text input with a listbox, allowing users to filter a list of options to items matching a query.

Example
ComboBox.tsx
DropdownListBox.tsx
ComboBox.css
Form.css
ListBox.css
Popover.css
import {ComboBox, ComboBoxItem} from './ComboBox';

<ComboBox label="Favorite Animal">
  <ComboBoxItem>Aardvark</ComboBoxItem>
  <ComboBoxItem>Cat</ComboBoxItem>
  <ComboBoxItem>Dog</ComboBoxItem>
  <ComboBoxItem>Kangaroo</ComboBoxItem>
  <ComboBoxItem>Panda</ComboBoxItem>
  <ComboBoxItem>Snake</ComboBoxItem>
</ComboBox>

API

LabelHelp textLabelInputLabelOp​​Help text (description or error message)OpButtonOption 1Option 2ItemItem labelDescriptionDescriptionOption 3DescriptionItem descriptionSECTION TITLESection headerSectionPopoverListBox
<ComboBox>
  <Label />
  <Group>
    <Input />
    <Button />
  </Group>
  <ComboBoxValue />
  <Text slot="description" />
  <FieldError />
  <Popover>
    <ListBox />
  </Popover>
</ComboBox>
useComboBoxState<T, M extends = 'single'>(props: <T, M>): <T, M>useComboBox<T, M extends = 'single'>(props: <T, M>, state: <T, M>): <T>

ComboBoxState

Properties

NameType
selectedItems<T>[]

The value of the selected items.

focusStrategynull

Controls which item will be auto focused when the menu opens.

isFocusedboolean

Whether the select is currently focused.

collection<<T>>

A collection of items in the list.

selectionManager

A selection manager to read and update multiple selection state.

pointnull

The cursor position when the overlay was triggered, relative to the window viewport.

realtimeValidation

Realtime validation results, updated as the user edits the value.

displayValidation

Currently displayed validation results, updated when the user commits their changes.

disabledKeysSet<Key>

A set of items that are disabled.

value<>

The current combobox value.

defaultValue<>

The default combobox value.

inputValuestring

The current value of the combo box input.

defaultInputValuestring

The default value of the combo box input.

Methods

setValue(value: Keyreadonly Key[]null): void
Sets the combobox value.
setInputValue(value: string): void
Sets the value of the combo box input.
commit(): void
Selects the currently focused item and updates the input value.
setFocused(isFocused: boolean): void
Sets whether the select is focused.
open(focusStrategy?: null, trigger?: ): void
Opens the menu.
toggle(focusStrategy?: null, trigger?: ): void
Toggles the menu.
revert(): void
Resets the input value to the previously selected item's text if any and closes the menu.
setOpen(isOpen: boolean): void
Sets whether the overlay is open.
close(): void
Closes the overlay.
setPoint(point: ): void
Sets the cursor position relative to the window viewport.
updateValidation(result: ): void
Updates the current validation result. Not displayed to the user until commitValidation is called.
resetValidation(): void
Resets the displayed validation state to valid when the user resets the form.
commitValidation(): void
Commits the realtime validation so it is displayed to the user.

AriaComboBoxOptions

NameTypeDefault
inputRef<HTMLInputElementnull>Default:

The ref for the input element.

popoverRef<Elementnull>Default:

The ref for the list box popover.

listBoxRef<HTMLElementnull>Default:

The ref for the list box.

buttonRef<Elementnull>Default:

The ref for the optional list box popup trigger button.

labelElementTypeElementTypeDefault: 'label'

The HTML element used to render the label, e.g. 'label', or 'span'.

keyboardDelegateDefault:

An optional keyboard delegate implementation, to override the default.

layoutDelegateDefault:

A delegate object that provides layout information for items in the collection. By default this uses the DOM, but this can be overridden to implement things like virtualized scrolling.

allowsCustomValuebooleanDefault:

Whether the ComboBox allows a non-item matching input value to be set.

menuTriggerDefault: 'input'

The interaction required to display the ComboBox menu.

isDisabledbooleanDefault:

Whether the input is disabled.

isReadOnlybooleanDefault:

Whether the input can be selected but not changed by the user.

placeholderstringDefault:

Temporary text that occupies the text input when it is empty.

itemsIterable<T>Default:

The list of ComboBox items (controlled).

defaultItemsIterable<T>Default:

The list of ComboBox items (uncontrolled).

selectionModeDefault: 'single'

Whether single or multiple selection is enabled.

disabledKeysIterable<Key>Default:

The item keys that are disabled. These items cannot be selected, focused, or otherwise interacted with.

shouldFocusWrapbooleanDefault:

Whether keyboard navigation is circular.

value<>Default:

The current value (controlled).

defaultValue<>Default:

The default value (uncontrolled).

onChange(value: <>) => voidDefault:

Handler that is called when the value changes.

inputValuestringDefault:

The value of the ComboBox input (controlled).

defaultInputValuestringDefault:

The default value of the ComboBox input (uncontrolled).

onInputChange(value: string) => voidDefault:

Handler that is called when the ComboBox input value changes.

ComboBoxAria

NameType
labelPropsDOMAttributes

Props for the label element.

inputPropsInputHTMLAttributes<HTMLInputElement>

Props for the combo box input element.

listBoxProps<T>

Props for the list box, to be passed to useListBox.

buttonProps

Props for the optional trigger button, to be passed to useButton.

valuePropsDOMAttributes

Props for the element representing the selected value.

descriptionPropsDOMAttributes

Props for the combo box description element, if any.

errorMessagePropsDOMAttributes

Props for the combo box error message element, if any.

validationDetailsValidityState

The native validation details for the input.