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.27.1 |
| usage | import {useComboBoxState} from 'react-stately' |
API#
useComboBoxState<T extends object>(
(props: ComboBoxStateOptions<T>
)): ComboBoxState<T>
Interface#
Properties
| Name | Type | Description |
inputValue | string | The current value of the combo box input. |
isFocused | boolean | Whether the select is currently focused. |
selectedKey | Key | The key for the currently selected item. |
selectedItem | Node<T> | The value of the currently selected item. |
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. |
focusStrategy | FocusStrategy | Controls which item will be auto focused when the menu opens. |
close | () => void | Closes the menu and all submenus in the menu tree. |
UNSTABLE_openSubmenu | (
(triggerKey: Key,
, level: number
)) => void | Opens a specific submenu tied to a specific menu item at a specific level. |
UNSTABLE_closeSubmenu | (
(triggerKey: Key,
, level: number
)) => void | Closes a specific submenu tied to a specific menu item at a specific level. |
UNSTABLE_expandedKeysStack | Key[] | An array of open submenu trigger keys within the menu tree. The index of key within array matches the submenu level in the tree. |
isOpen | boolean | Whether the overlay is currently open. |
Methods
| Method | Description |
setInputValue(
(value: string
)): void | Sets the value of the combo box input. |
commit(): void | Selects the currently focused item and updates the input value. |
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. |
setFocused(
(isFocused: boolean
)): void | Sets whether the select is focused. |
setSelectedKey(
(key: Key
| | null
)): void | Sets the selected key. |
setOpen(
(isOpen: boolean
)): void | Sets whether the overlay is open. |
Example#
See the docs for useComboBox in react-aria for an example of useComboBoxState.