useComboBoxState
Provides state management for a combo box component. Handles building a collection of items from props and manages the option selection state of the combo box. In addition, it tracks the input value, focus state, and other properties of the combo box.
| install | yarn add react-stately |
|---|---|
| version | 3.47.0 |
| usage | import {useComboBoxState} from 'react-stately' |
API#
useComboBoxState<T, M extends SelectionMode = 'single'>(
(props: ComboBoxStateOptions<T, M>
)): ComboBoxState<T, M>
Interface#
Properties
| Name | Type | Description |
value | ValueType<SelectionMode> | The current combobox value. |
defaultValue | ValueType<SelectionMode> | The default combobox value. |
selectedItems | Node<T>[] | The value of the selected items. |
inputValue | string | The current value of the combo box input. |
defaultInputValue | string | The default value of the combo box input. |
focusStrategy | FocusStrategy | null | Controls which item will be auto focused when the menu opens. |
isFocused | boolean | Whether the select is currently focused. |
collection | Collection<Node<T>> | A collection of items in the list. |
disabledKeys | Set<Key> | A set of items that are disabled. |
selectionManager | SelectionManager | A selection manager to read and update multiple selection state. |
isOpen | boolean | Whether the overlay is currently open. |
realtimeValidation | ValidationResult | Realtime validation results, updated as the user edits the value. |
displayValidation | ValidationResult | Currently displayed validation results, updated when the user commits their changes. |
Methods
| Method | Description |
setValue(
(value: Key
| readonly 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?: FocusStrategy
| | null,
, trigger?: MenuTriggerAction
)): void | Opens the menu. |
toggle(
(focusStrategy?: FocusStrategy
| | null,
, trigger?: MenuTriggerAction
)): 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. |
updateValidation(
(result: ValidationResult
)): void | Updates the current validation result. Not displayed to the user until |
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. |
Example#
See the docs for useComboBox in react-aria for an example of useComboBoxState.