Skip to content

useTableSelectionState

@astryxdesign/core v0.7.1 · useTableSelectionState

data and selectedKeys are MaybeRefOrGetter, so a ref or a getter keeps the state live. The other fields are read from the config object you pass.

Select-all only touches rows that can actually be checked: rows where getIsItemSelectable or getIsItemEnabled is false keep whatever selection they already had, and a key that is not in data stays selected too.

Parameters ​

NameTypeDefaultDescription
dataMaybeRefOrGetter<readonly T[]>—The full data array rendered in the table.
idKey(keyof T & string) | ((item: T) => string)—Key extractor: either the property name that holds a unique string ID, or a function that returns it.
selectedKeysMaybeRefOrGetter<ReadonlySet<string>>—Controlled set of selected item IDs.
setSelectedKeys(keys: Set<string>) => void—Setter for the controlled selected keys. It is always handed a ready-made Set; assign it to your own ref. A dispatcher that also accepts updater functions, such as React's setState, fits the same signature.
getIsItemSelectable(item: T) => boolean() => trueShould this row show a checkbox? Non-selectable rows render nothing in the selection cell and are excluded from select-all.
getIsItemEnabled(item: T) => boolean() => trueIs this row checkbox interactive? Disabled rows keep their checkbox, rendered disabled, and are frozen: select-all preserves their state.
getRowLabel(item: T) => string—Passed through to the selection plugin as the row checkbox label.
hasRowHighlightbooleantruePassed through to the selection plugin. false drops the accent wash on checked rows and keeps aria-selected.

Returns ​

FieldTypeDescription
selectionConfigUseTableSelectionConfig<T> & { readonly rows: readonly T[] }Everything useTableSelection needs. It also carries a rows getter with the current data, so a select-all handler that wants the rows can read them from the config.
selectionStateTableSelectionStateRead-only snapshot for selection-aware UI. Pass it to TableSelectionToolbar and the toolbar hides itself while nothing is selected.
selectionState.selectedKeysReadonlySet<string>Every selected key, including rows that are not on the current page.
selectionState.selectedCountnumberHow many keys are selected.
selectionState.hasSelectionbooleanTrue when at least one key is selected.
selectionState.clearSelection() => voidCalls setSelectedKeys with an empty Set.