Appearance
useBulkSelection
Manages bulk selection state for collections of items.
Signature
ts
function useBulkSelection<T extends SelectableId = SelectableId>(
options?: UseBulkSelectionOptions<T>
): UseBulkSelectionReturn<T>Parameters
UseBulkSelectionOptions<T>
| Property | Type | Default | Description |
|---|---|---|---|
initialSelected | T[] | [] | Initially selected IDs |
Return Type
UseBulkSelectionReturn<T>
| Property | Type | Description |
|---|---|---|
selectedIds | Set<T> | Set of currently selected IDs |
selectedCount | number | Number of selected items |
isSelected | (id: T) => boolean | Check if an ID is selected |
select | (id: T) => void | Add an ID to selection |
deselect | (id: T) => void | Remove an ID from selection |
toggle | (id: T) => void | Toggle an ID's selection state |
selectAll | (ids: T[]) => void | Replace selection with given IDs |
clear | () => void | Clear all selections |
isAllSelected | (ids: T[]) => boolean | Check if all given IDs are selected |
isSomeSelected | (ids: T[]) => boolean | Check if some (but not all) IDs are selected |
Types
ts
type SelectableId = string | numberExample
tsx
const { selectedIds, toggle, isSelected, clear } = useBulkSelection<number>()
<Checkbox checked={isSelected(item.id)} onChange={() => toggle(item.id)} />