Selection
Toggle a value by selection mode, extend a range from an anchor, and the state of a select-all checkbox.
Reach for them in a collection with its own selection input; SelectionController maps clicks and keys to them for you. The helpers are pure: no DOM at import time or in the call path, so they run on the server and in tests. Each result below is computed when the page is built, by running the call shown.
Import
import {
extendSelection,
selectAllState,
toggleSelection,
type ExtendSelectionOptions,
type SelectionMode,
type SelectionOptions,
} from '@ecopages/radiant-ui/helpers';Every helper takes { selectable }, a function that returns false for a value the user cannot select or deselect, such as a disabled item. The arrays passed in are never changed: toggleSelection and extendSelection return a new one. The calls below use order = ['a', 'b', 'c', 'd'] and notC = (value) => value !== 'c'.
Toggle
toggleSelection(values, value, mode, { selectable }) is the selection after the user toggles value. SelectionMode is none, single or multiple. With single the value replaces the selection, or clears it when it was the selected one; with multiple it is added or removed; with none, or for a value selectable refuses, nothing changes.
| Call | Result |
|---|---|
toggleSelection(['a'], 'b', 'multiple') | ["a","b"] |
toggleSelection(['a', 'b'], 'a', 'multiple') | ["b"] |
toggleSelection(['a'], 'b', 'single') | ["b"] |
toggleSelection(['a'], 'a', 'single') | [] |
toggleSelection(['a'], 'b', 'none') | ["a"] |
toggleSelection(['a'], 'c', 'multiple', { selectable: notC }) | ["a"] |
Extend
extendSelection(order, anchor, target, values, { additive, selectable }) is the selection after the user extends it from anchor to target, as Shift+click and Shift+arrow do. The range is every selectable value of order from one to the other, both included, in the order of order. A null anchor, or one that is not in order, starts the range at target; a target that is not in order changes nothing. Without additive the range replaces the selection, except for the selected values selectable refuses, which stay; with additive: true the range is added to it.
| Call | Result |
|---|---|
extendSelection(order, 'b', 'd', ['a']) | ["b","c","d"] |
extendSelection(order, 'b', 'd', ['a'], { additive: true }) | ["a","b","c","d"] |
extendSelection(order, 'd', 'b', []) | ["b","c","d"] |
extendSelection(order, null, 'c', ['a']) | ["c"] |
extendSelection(order, 'a', 'd', [], { selectable: notC }) | ["a","b","d"] |
extendSelection(order, 'a', 'b', ['c'], { selectable: notC }) | ["c","a","b"] |
Select all
selectAllState(order, values, { selectable }) is the state a select-all checkbox shows: true when every selectable value of order is selected, false when none is (or none can be), otherwise 'mixed', which is the checkbox's indeterminate.
| Call | Result |
|---|---|
selectAllState(order, ['a']) | "mixed" |
selectAllState(order, order) | true |
selectAllState(order, []) | false |
selectAllState(order, ['a', 'b', 'd'], { selectable: notC }) | true |
SelectionController has a list with a select-all checkbox built on selectAllState.