useSelectState
Provides state management for a select component. Handles building a collection of items from props, handles the open state for the popup menu, and manages multiple selection state.
install | yarn add react-stately |
---|---|
version | 3.28.0 |
usage | import {useSelectState} from 'react-stately' |
API#
useSelectState<T extends object>(
(props: SelectStateOptions<T>
)): SelectState<T>
Interface#
Properties
Name | Type | Description |
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. |
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 |
setFocused(
(isFocused: boolean
)): void | Sets whether the select is focused. |
setSelectedKey(
(key: Key
| | null
)): void | Sets the selected key. |
open(
(focusStrategy?: FocusStrategy
| | null
)): void | Opens the menu. |
toggle(
(focusStrategy?: FocusStrategy
| | null
)): void | Toggles the menu. |
setOpen(
(isOpen: boolean
)): void | Sets whether the overlay is open. |
updateValidation(
(result: ValidationResult
)): 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. |
Example#
See the docs for useSelect in react-aria for an example of useSelectState
.