Skip to content

@jsvision/datagrid / toggleKey

Function: toggleKey()

toggleKey(current, key, mode): ReadonlySet<Key>

Defined in: datagrid/src/selection.ts:56

Toggle key's membership, returning a new set.

In multi mode the key is added when absent and removed when present, so the same gesture (Space, Ctrl+click) grows or shrinks the selection. In single mode the result holds at most one key: an absent key replaces the selection with just {key}; toggling the one already-selected key clears the selection.

Parameters

current

ReadonlySet<Key>

The current selection (not mutated).

key

Key

The row key to toggle.

mode

SelectionMode

'single' (≤1 key, replace) or 'multi' (accumulate).

Returns

ReadonlySet<Key>

A new selection set with key's membership flipped under the mode's rules.

Example

ts
import { toggleKey } from '@jsvision/datagrid';
import type { Key } from '@jsvision/datagrid';
// toggleKey both takes and returns a ReadonlySet<Key> — it never hands back a mutable Set, so the
// binding that tracks the selection is typed ReadonlySet from the start.
let sel: ReadonlySet<Key> = new Set<Key>();
sel = toggleKey(sel, 1, 'multi'); // {1}
sel = toggleKey(sel, 2, 'multi'); // {1, 2}
sel = toggleKey(sel, 1, 'multi'); // {2}      — present key removed
sel = toggleKey(sel, 9, 'single'); // {9}     — single replaces