SelectionManager

An interface for reading and updating multiple selection state.

installyarn add @react-stately/selection
version3.3.0
usageimport {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

NameTypeDescription
selectionModeThe type of selection that is allowed in the collection.
disallowEmptySelectionbooleanWhether the collection allows empty selection.
isFocusedbooleanWhether the collection is currently focused.
isFocusWithinItembooleanWhether focus is currently within a cell in the collection.
focusedKeyKeyThe current focused key in the collection.
selectedKeysSet<Key>The currently selected keys in the collection.
isEmptybooleanWhether the selection is empty.
isSelectAllbooleanWhether all items in the collection are selected.
firstSelectedKeyKeynull
lastSelectedKeyKeynull

Methods

MethodDescription
setFocused( (isFocused: boolean )): voidSets whether the collection is focused.
setFocusWithinItem( (isFocused: boolean )): voidSets whether focus is currently within a cell in the collection.
setFocusedKey( (key: Key )): voidSets the focused key.
isSelected( (key: Key )): voidReturns whether a key is selected.
extendSelection( (toKey: Key )): voidExtends the selection to the given key.
toggleSelection( (key: Key )): voidToggles whether the given key is selected.
replaceSelection( (key: Key )): voidReplaces the selection with only the given key.
selectAll(): voidSelects all items in the collection.
clearSelection(): voidRemoves all keys from the selection.
toggleSelectAll(): voidToggles between select all and an empty selection.
select( (key: Key, , e: PointerEvent )): void