Skip to content

Selection

Overview

useSelection(name, options) creates or retrieves a named reactive selection store. It supports single and multiple selection modes, optional filtering, and item deduplication using a configurable comparator function. The store is initialised with an empty items array, mode: 'single', and enabled: true on first call.

Usage

javascript
import { useSelection } from '@kalisio/kdk/core.client'

const { selection, selectItem, getSelectedItem, clearSelection } = useSelection('myView')

Parameters

ParameterTypeDefaultDescription
namestringUnique selection store name within the application.
options.matchesFunction_.matchesComparator used to determine whether two items are equal. Defaults to Lodash _.matches.

Exposed

NameTypeDescription
selectionreactive ObjectThe raw selection store ({ items, mode, enabled, filter }).
clearSelection()FunctionEmpties the selected items array. No-op if selection is disabled.
getSelectionMode()FunctionReturns the current mode ('single' or 'multiple').
setSelectionMode(mode)FunctionSets the selection mode.
isSingleSelectionMode()FunctionReturns true if current mode is 'single'.
isMultipleSelectionMode()FunctionReturns true if current mode is not 'single'.
setSelectionEnabled(enabled?)FunctionEnables or disables selection. Defaults to true.
isSelectionEnabled()FunctionReturns true if selection is currently enabled.
getSelectionFilter()FunctionReturns the current filter function.
setSelectionFilter(filter)FunctionSets a filter function (item) => boolean that prevents certain items from being selected.
selectItem(item)FunctionAdds an item to the selection if it is not already selected and passes the filter.
unselectItem(item)FunctionRemoves an item from the selection.
hasSelectedItem()FunctionReturns true if at least one item is selected.
hasSelectedItems()FunctionAlias for hasSelectedItem().
getSelectedItem()FunctionReturns the last selected item (useful in single-selection mode).
getSelectedItems()FunctionReturns the full array of selected items.

TIP

In single-selection mode, getSelectedItem() returns the most recently selected item. To keep true single-selection behaviour, call clearSelection() before selectItem().