SelectionManager
An interface for reading and updating multiple selection state.
install | yarn add @react-stately/selection |
---|---|
version | 3.9.3 |
usage | import {SelectionManager} from '@react-stately/selection' |
Introduction#
A SelectionManager
provides a generic interface for reading and
updating selection and focus state based on a Collection
.
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 useSelectableCollection
hook in react-aria.
A SelectionManager
wraps the state returned by useMultipleSelectionState
.
Oftentimes, you won't need to construct these directly because hooks like useListState
and useTreeState
already handle this and return
a SelectionManager
for you.
Interface#
Properties
Name | Type | Description |
selectionMode | SelectionMode | The type of selection that is allowed in the collection. |
disallowEmptySelection | boolean | Whether the collection allows empty selection. |
selectionBehavior | SelectionBehavior | The selection behavior for the collection. |
isFocused | boolean | Whether the collection is currently focused. |
focusedKey | Key | The current focused key in the collection. |
childFocusStrategy | FocusStrategy | Whether the first or last child of the focused key should receive focus. |
selectedKeys | Set<Key> | The currently selected keys in the collection. |
rawSelection | Selection | The raw selection value for the collection. Either 'all' for select all, or a set of keys. |
isEmpty | boolean | Whether the selection is empty. |
isSelectAll | boolean | Whether all items in the collection are selected. |
firstSelectedKey | Key | null | |
lastSelectedKey | Key | null |
Methods
Method | Description |
constructor(
collection: Collection<Node<unknown>>,
state: MultipleSelectionState,
options?: SelectionManagerOptions
): void | |
setSelectionBehavior(
(selectionBehavior: SelectionBehavior
)): void | Sets the selection behavior for the collection. |
setFocused(
(isFocused: boolean
)): void | Sets whether the collection is focused. |
setFocusedKey(
(key: Key,
, childFocusStrategy?: FocusStrategy
)): 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. |
setSelectedKeys(
(keys: Iterable<Key>
)): void | Replaces the selection with the given keys. |
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. |
select(
(key: Key,
, e?: PressEvent
| LongPressEvent
| PointerEvent
)): void | |
isSelectionEqual(
(selection: Set<Key>
)): void | Returns whether the current selection is equal to the given selection. |
canSelectItem(
(key: Key
)): void |