SelectionManager
An interface for reading and updating multiple selection state.
install | yarn add @react-stately/selection |
---|---|
version | 3.0.0 |
usage | import {SelectionManager} from '@react-stately/selection' |
Introduction#
A provides a generic interface for reading and
updating selection and focus state based on a
.
As discussed in the selection introduction, a selection is represented
by a Set of item keys. Focus
is represented by a single item key.
Focus state is updated when navigating a collection with the keyboard. Selection state is updated when a user
clicks or taps an item, or uses the keyboard to select an item. These interactions are handled by
the hook in react-aria.
A SelectionManager
wraps the state returned by .
Oftentimes, you won't need to construct these directly because hooks like
and
already handle this and return
a
SelectionManager
for you.
Interface#
Properties
Name | Type | Description |
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. |
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. |
useSelectableCollection(
(options: SelectableCollectionOptions
)): SelectableCollectionAria
Name | Type | Default | Description |
selectionManager | MultipleSelectionManager | — | An interface for reading and updating multiple selection state. |
keyboardDelegate | KeyboardDelegate | — | A delegate object that implements behavior for keyboard focus movement. |
ref | RefObject<HTMLElement> | — | The ref attached to the element representing the collection. |
autoFocus | boolean | FocusStrategy | false | Whether the collection or one of its items should be automatically focused upon render. |
shouldFocusWrap | boolean | false | Whether focus should wrap around when the end/start is reached. |
disallowEmptySelection | boolean | false | Whether the collection allows empty selection. |
disallowSelectAll | boolean | false | Whether the collection allows the user to select all items via keyboard shortcut. |
Properties
Name | Type | Description |
selectionMode | SelectionMode | The type of selection that is allowed 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. |
isFocused | boolean | Whether the collection is currently focused. |
focusedKey | Key | The current focused key in the collection. |
disallowEmptySelection | boolean | Whether the collection allows empty selection. |
Methods
Method | Description |
isSelected(
(key: Key
)): boolean | 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. |
setFocused(
(isFocused: boolean
)): void | Sets whether the collection is focused. |
setFocusedKey(
(key: Key
)): void | Sets the focused key. |
Name | Type | Description |
collectionProps | HTMLAttributes<HTMLElement> | Props for the collection element. |
Manages state for multiple selection and focus in a collection.
useMultipleSelectionState(
(props: MultipleSelection
)): MultipleSelectionState
Provides state management for list-like components. Handles building a collection of items from props, and manages multiple selection state.
useListState<T>(
(props: ListProps<T>
)): ListState<T>
Name | Type | Description |
children | CollectionChildren<T> | The contents of the collection. |
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. |
selectionMode | SelectionMode | The type of selection that is allowed in the collection. |
disallowEmptySelection | boolean | Whether the collection allows empty selection. |
selectedKeys | 'all' | Iterable<Key> | The currently selected keys in the collection (controlled). |
defaultSelectedKeys | 'all' | Iterable<Key> | The initial selected keys in the collection (uncontrolled). |
onSelectionChange | (
(keys: Selection
)) => any | Handler that is called when the selection changes. |
Name | Type | Description |
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. |
Provides state management for tree-like components. Handles building a collection of items from props, item expanded state, and manages multiple selection state.
useTreeState<T>(
(props: TreeProps<T>
)): TreeState<T>
Name | Type | Description |
children | CollectionChildren<T> | The contents of the collection. |
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. |
expandedKeys | Iterable<Key> | The currently expanded keys in the collection (controlled). |
defaultExpandedKeys | Iterable<Key> | The initial expanded keys in the collection (uncontrolled). |
onExpandedChange | (
(keys: Set<Key>
)) => any | Handler that is called when items are expanded or collapsed. |
selectionMode | SelectionMode | The type of selection that is allowed in the collection. |
disallowEmptySelection | boolean | Whether the collection allows empty selection. |
selectedKeys | 'all' | Iterable<Key> | The currently selected keys in the collection (controlled). |
defaultSelectedKeys | 'all' | Iterable<Key> | The initial selected keys in the collection (uncontrolled). |
onSelectionChange | (
(keys: Selection
)) => any | Handler that is called when the selection changes. |
Properties
Name | Type | Description |
collection | Collection<Node<T>> | A collection of items in the tree. |
disabledKeys | Set<Key> | A set of keys for items that are disabled. |
expandedKeys | Set<Key> | A set of keys for items that are expanded. |
selectionManager | SelectionManager | A selection manager to read and update multiple selection state. |
Methods
Method | Description |
toggleKey(
(key: Key
)): void | Toggles the expanded state for an item by its key. |