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/combobox |
|---|---|
| version | 3.0.0-beta.0 |
| usage | import {useComboBoxState} from '@react-stately/combobox' |
API#
useComboBoxState<T>(
(props: ComboBoxStateProps<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. |
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. |
setFocused(
(isFocused: boolean
)): void | Sets whether the select is focused. |
setSelectedKey(
(key: Key
)): void | Sets the selected key. |
close(): void | Closes the overlay. |
Example#
See the docs for useComboBox in react-aria for an example of useComboBoxState.
| Name | Type | Default | Description |
children | CollectionChildren<T> | — | The contents of the collection. |
defaultFilter | FilterFn | — | The filter function used to determine if a option should be included in the combo box list. |
allowsEmptyCollection | boolean | — | Whether the combo box allows the menu to be open when the collection is empty. |
shouldCloseOnBlur | boolean | — | Whether the combo box menu should close on blur. |
defaultItems | Iterable<T> | — | The list of ComboBox items (uncontrolled). |
items | Iterable<T> | — | The list of ComboBox items (controlled). |
isOpen | boolean | — | Sets the open state of the menu. |
defaultOpen | boolean | — | Sets the default open state of the menu. |
onOpenChange | (
(isOpen: boolean,
, menuTrigger: MenuTriggerAction
)) => void | — | Method that is called when the open state of the menu changes. Returns the new open state and the action that caused the opening of the menu. |
inputValue | string | — | The value of the ComboBox input (controlled). |
defaultInputValue | string | — | The default value of the ComboBox input (uncontrolled). |
onInputChange | (
(value: string
)) => void | — | Handler that is called when the ComboBox input value changes. |
allowsCustomValue | boolean | — | Whether the ComboBox allows a non-item matching input value to be set. |
menuTrigger | MenuTriggerAction | 'input' | The interaction required to display the ComboBox menu. |
shouldFlip | boolean | true | Whether the menu should automatically flip direction when space is limited. |
disabledKeys | Iterable<Key> | — | The item keys that are disabled. These items cannot be selected, focused, or otherwise interacted with. |
disallowEmptySelection | boolean | — | Whether the collection allows empty selection. |
selectedKey | Key | — | The currently selected key in the collection (controlled). |
defaultSelectedKey | Key | — | The initial selected key in the collection (uncontrolled). |
onSelectionChange | (
(key: Key
)) => any | — | Handler that is called when the selection changes. |
isDisabled | boolean | — | Whether the input is disabled. |
isReadOnly | boolean | — | Whether the input can be selected but not changed by the user. |
placeholder | string | — | Temporary text that occupies the text input when it is empty. |
id | string | — | The element's unique identifier. See MDN. |
validationState | ValidationState | — | Whether the input should display its "valid" or "invalid" visual styling. |
isRequired | boolean | — | Whether user input is required on the input before form submission.
Often paired with the necessityIndicator prop to add a visual indicator to the input. |
autoFocus | boolean | — | Whether the element should receive focus on render. |
onFocus | (
(e: FocusEvent
)) => void | — | Handler that is called when the element receives focus. |
onBlur | (
(e: FocusEvent
)) => 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. |
(
(textValue: string,
, inputValue: string
)) => boolean'focus'
| 'input'
| 'manual'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. |
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. |
setFocused(
(isFocused: boolean
)): void | Sets whether the select is focused. |
setSelectedKey(
(key: Key
)): void | Sets the selected key. |
close(): void | Closes the overlay. |
An interface for reading and updating multiple selection state.
Properties
| Name | Type | Description |
selectionMode | SelectionMode | The type of selection that is allowed in the collection. |
disallowEmptySelection | boolean | Whether the collection allows empty selection. |
isFocused | boolean | Whether the collection is currently focused. |
focusedKey | Key | The current focused key in the collection. |
childFocusStrategy | FocusStrategy | Whether the first or last child of the focused key should receive focus. |
selectedKeys | Set<Key> | The currently selected keys in the collection. |
isEmpty | boolean | Whether the selection is empty. |
isSelectAll | boolean | Whether all items in the collection are selected. |
firstSelectedKey | Key | null | |
lastSelectedKey | Key | null |
Methods
| Method | Description |
setFocused(
(isFocused: boolean
)): void | Sets whether the collection is focused. |
setFocusedKey(
(key: Key,
, childFocusStrategy: FocusStrategy
)): void | Sets the focused key. |
isSelected(
(key: Key
)): void | Returns whether a key is selected. |
extendSelection(
(toKey: Key
)): void | Extends the selection to the given key. |
toggleSelection(
(key: Key
)): void | Toggles whether the given key is selected. |
replaceSelection(
(key: Key
)): void | Replaces the selection with only the given key. |
setSelectedKeys(
(keys: Iterable<Key>
)): void | Replaces the selection with the given keys. |
selectAll(): void | Selects all items in the collection. |
clearSelection(): void | Removes all keys from the selection. |
toggleSelectAll(): void | Toggles between select all and an empty selection. |
select(
(key: Key,
, e: PressEvent
| | PointerEvent
)): void | |
isSelectionEqual(
(selection: Set<Key>
)): void | Returns whether the current selection is equal to the given selection. |
Properties
| Name | Type | Description |
selectionMode | SelectionMode | The type of selection that is allowed in the collection. |
disallowEmptySelection | boolean | Whether the collection allows empty selection. |
selectedKeys | Selection | The currently selected keys in the collection. |
disabledKeys | Set<Key> | The currently disabled keys in the collection. |
isFocused | boolean | Whether the collection is currently focused. |
focusedKey | Key | The current focused key in the collection. |
childFocusStrategy | FocusStrategy | Whether the first or last child of the focused key should receive focus. |
Methods
| Method | Description |
setSelectedKeys(
(keys: Selection
| | (
(v: Selection
)) => Selection
)): void | Sets the selected keys in the collection. |
setFocused(
(isFocused: boolean
)): void | Sets whether the collection is focused. |
setFocusedKey(
(key: Key,
, child: FocusStrategy
)): void | Sets the focused key, and optionally, whether the first or last child of that key should receive focus. |