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/select |
---|---|
version | 3.0.0 |
usage | import {useSelectState} from '@react-stately/select' |
API#
useSelectState<T>(
(props: <T>
)): <T>
Interface#
Properties
Name | Type | Description |
selectedKey | Key | The key for the currently selected item. |
selectedItem | <T> | The value of the currently selected item. |
isFocused | boolean | Whether the select is currently focused. |
collection | <<T>> | A collection of items in the list. |
disabledKeys | Set<Key> | A set of items that are disabled. |
selectionManager |
| A selection manager to read and update multiple selection state. |
focusStrategy |
| Controls which item will be auto focused when the menu opens. |
isOpen | boolean | Whether the overlay is currently open. |
Methods
Method | Description |
setSelectedKey(
(key: Key
)): void | Sets the selected key. |
setFocused(
(isFocused: boolean
)): void | Sets whether the select is focused. |
toggle(
(focusStrategy:
| | null
)): void | Toggles the menu. |
open(): void | Opens the overlay. |
close(): void | Closes the overlay. |
Example#
See the docs for useSelect in react-aria for an example of useSelectState
.
Name | Type | Description |
children | CollectionChildren<T> | The contents of the collection. |
isOpen | boolean | Sets the open state of the menu |
defaultOpen | boolean | Sets the default open state of the menu |
onOpenChange | (
(isOpen: boolean
)) => void | Method that is called when the open state of the menu changes |
shouldFlip | boolean | Whether the menu should automatically flip direction when space is limited |
items | Iterable<T> | Item objects in the collection. |
disabledKeys | Iterable<Key> | The item keys that are disabled. These items cannot be selected, focused, or otherwise interacted with. |
isLoading | boolean | Whether the items are currently loading. |
onLoadMore | () => any | Handler that is called when more items should be loaded, e.g. while scrolling near the bottom. |
isDisabled | boolean | Whether the input is disabled. |
isReadOnly | boolean | Whether the input can be selected but not changed by the user. |
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. |
label | ReactNode | The content to display as the label. |
placeholder | string | Temporary text that occupies the text input when it is empty. |
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. |
Properties
Name | Type | Description |
selectedKey | Key | The key for the currently selected item. |
selectedItem | Node<T> | The value of the currently selected item. |
isFocused | boolean | Whether the select is currently focused. |
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 |
setSelectedKey(
(key: Key
)): void | Sets the selected key. |
setFocused(
(isFocused: boolean
)): void | Sets whether the select is focused. |
toggle(
(focusStrategy: FocusStrategy
| | null
)): void | Toggles the menu. |
open(): void | Opens the overlay. |
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. |
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. |
Methods
Method | Description |
setFocused(
(isFocused: boolean
)): void | Sets whether the collection is focused. |
setFocusedKey(
(key: Key
)): 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. |
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. |
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. |
isFocused | boolean | Whether the collection is currently focused. |
focusedKey | Key | The current focused key in the collection. |
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
)): void | Sets the focused key. |