sdxc

Type to search, or start from one of these:

[ @sdxc/ui/behaviors ]

FilterModel

Headless filtering model for search-as-you-type option lists such as a command palette.

import { FilterModel } from "@sdxc/ui/behaviors";

Usage

Owns the query, the matched option set, and the active match for a filterable list, dispatching "change" whenever any of the three moves, so a DOM adapter stays a thin layer forwarding input and reading state back.

Signature

new FilterModel(init?: FilterModel.Init)

Parameters

ParameterTypeDescription
init?FilterModel.InitInitial options, query, and match override.

Properties

PropertyTypeDescription
querystringCurrent query string filtering the option set.
optionsreadonly FilterModel.Option[]Full option set the model filters over, in the order last provided.
matchesreadonly FilterModel.Option[]Options whose value or keywords currently match the query, in their original order.
activeIdstring | nullId of the currently active match, or null when nothing is active.
activeOptionFilterModel.Option | nullThe active match's full option, or null when nothing is active.
isEmptybooleantrue when the current query has no matches.

Methods

MethodDescription
setOptions(options: Iterable<FilterModel.Option>): voidReplaces the option set and recomputes matches against the current query, keeping the active option while it still matches and falling back to the first match otherwise. Always dispatches "change".
setQuery(query: string): voidUpdates the query and recomputes matches, keeping the active option while it still matches and falling back to the first match otherwise. A no-op when query equals the current query.
isMatch(id: string): booleanReports whether an option id is part of the current matched set.
setActive(id: string | null): voidSets the active option explicitly. An id outside the current matches is ignored, so the active option always stays a visible match. Dispatches "change" only when the active id actually changes.
moveNext(): voidMoves activation to the match after the current one, wrapping to the first match after the last. Activates the first match when nothing is currently active.
movePrevious(): voidMoves activation to the match before the current one, wrapping to the last match before the first. Activates the last match when nothing is currently active.
moveFirst(): voidActivates the first match, or clears activation when there are no matches.
moveLast(): voidActivates the last match, or clears activation when there are no matches.

The events, constants and types its module publishes, each imported from @sdxc/ui/behaviors too.

Interface

FilterModel.Option

One filterable option. id is the stable key a consumer uses to correlate a rendered item with matched/active state; value and keywords are the text compared against the query.

MemberTypeDescription
idstringStable identifier correlated with a rendered item.
valuestringPrimary text compared against the query.
keywords?readonly string[]Additional search terms folded into matching alongside value.
Interface

FilterModel.Init

Construction options accepted by FilterModel.

MemberTypeDescription
options?Iterable<Option>Initial option set the model filters over. Defaults to none.
query?stringInitial query string. Defaults to an empty string.
match?(option: Option, query: string) => booleanOverrides the default case-insensitive substring match against value and keywords.
Interface

FilterModel.EventMap

Events dispatched by FilterModel as its state changes.

MemberTypeDescription
changeEventDispatched whenever the query, the matched set, or the active option changes.

Written by Sergio Xalambrí. Follow @sergiodxa for new packages, or sponsor the work.