SearchCtrlK

  • ↑↓navigate
  • ↵open
  • escclose

Can’t find it? Contact us

FilterModel

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

tsx
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

typescript
new FilterModel(init?: FilterModel.Init)

Parameters

ParameterTypeDefaultRequiredDescription
initFilterModel.Init—NoInitial options, query, and match override.

Properties

PropertyTypeDefaultRequiredDescription
querystring—YesCurrent query string filtering the option set.
optionsreadonly FilterModel.Option[]—YesFull option set the model filters over, in the order last provided.
matchesreadonly FilterModel.Option[]—YesOptions whose value or keywords currently match the query, in their original order.
activeIdstring | null—YesId of the currently active match, or null when nothing is active.
activeOptionFilterModel.Option | null—YesThe active match's full option, or null when nothing is active.
isEmptyboolean—Yestrue 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.

MemberTypeDefaultRequiredDescription
idstring—YesStable identifier correlated with a rendered item.
valuestring—YesPrimary text compared against the query.
keywordsreadonly string[]—NoAdditional search terms folded into matching alongside value.
Interface

FilterModel.Init

Construction options accepted by FilterModel.

MemberTypeDefaultRequiredDescription
optionsIterable<Option>—NoInitial option set the model filters over. Defaults to none.
querystring—NoInitial query string. Defaults to an empty string.
match(option: Option, query: string) => boolean—NoOverrides the default case-insensitive substring match against value and keywords.
Interface

FilterModel.EventMap

Events dispatched by FilterModel as its state changes.

MemberTypeDefaultRequiredDescription
changeEvent—YesDispatched whenever the query, the matched set, or the active option changes.
@sdxc/ui

Can’t find it? Contact us