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.

installyarn add react-stately
version3.27.1
usageimport {useSelectState} from 'react-stately'

API#


useSelectState<T extends object>( (props: SelectStateOptions<T> )): SelectState<T>

Interface#


Properties

NameTypeDescription
isFocusedbooleanWhether the select is currently focused.
selectedKeyKeyThe key for the currently selected item.
selectedItemNode<T>The value of the currently selected item.
collectionCollection<Node<T>>A collection of items in the list.
disabledKeysSet<Key>A set of items that are disabled.
selectionManagerSelectionManagerA selection manager to read and update multiple selection state.
focusStrategyFocusStrategyControls which item will be auto focused when the menu opens.
close() => voidCloses the menu and all submenus in the menu tree.
UNSTABLE_openSubmenu( (triggerKey: Key, , level: number )) => voidOpens a specific submenu tied to a specific menu item at a specific level.
UNSTABLE_closeSubmenu( (triggerKey: Key, , level: number )) => voidCloses a specific submenu tied to a specific menu item at a specific level.
UNSTABLE_expandedKeysStackKey[]

An array of open submenu trigger keys within the menu tree. The index of key within array matches the submenu level in the tree.

isOpenbooleanWhether the overlay is currently open.

Methods

MethodDescription
setFocused( (isFocused: boolean )): voidSets whether the select is focused.
setSelectedKey( (key: Keynull )): voidSets the selected key.
open( (focusStrategy?: FocusStrategynull )): voidOpens the menu.
toggle( (focusStrategy?: FocusStrategynull )): voidToggles the menu.
setOpen( (isOpen: boolean )): voidSets whether the overlay is open.

Example#


See the docs for useSelect in react-aria for an example of useSelectState.