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.40.0
usageimport {useSelectState} from 'react-stately'

API#


useSelectState<T extends object, M extends SelectionMode = 'single'>( (props: SelectStateOptions<T, M> )): SelectState<T, M>

Interface#


Properties

NameTypeDescription
valueValueType<SelectionMode>The current select value.
defaultValueValueType<SelectionMode>The default select value.
selectedItemsNode<T>[]The value of the selected items.
isFocusedbooleanWhether the select is currently focused.
focusStrategyFocusStrategynullControls which item will be auto focused when the menu opens.
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.
isOpenbooleanWhether the overlay is currently open.
realtimeValidationValidationResultRealtime validation results, updated as the user edits the value.
displayValidationValidationResultCurrently displayed validation results, updated when the user commits their changes.

Methods

MethodDescription
setValue( (value: KeyKey[]null )): voidSets the select value.
setFocused( (isFocused: boolean )): voidSets whether the select is focused.
open( (focusStrategy?: FocusStrategynull )): voidOpens the menu.
toggle( (focusStrategy?: FocusStrategynull )): voidToggles the menu.
setOpen( (isOpen: boolean )): voidSets whether the overlay is open.
close(): voidCloses the overlay.
updateValidation( (result: ValidationResult )): voidUpdates the current validation result. Not displayed to the user until commitValidation is called.
resetValidation(): voidResets the displayed validation state to valid when the user resets the form.
commitValidation(): voidCommits the realtime validation so it is displayed to the user.

Example#


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