Text filter
Case- and accent-insensitive contains and starts-with, and type-to-find over item labels.
Reach for them to filter options while the user types, or to move to the item whose label starts with what the user typed. 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 {
createTypeahead,
isTypeaheadKey,
textContains,
textStartsWith,
type TextFilterSensitivity,
} from '@ecopages/radiant-ui/helpers';Sensitivity
base (default) ignores case and accents, accent ignores case only, case is a literal match. An empty query matches everything. textStartsWith takes the same sensitivity and matches only at the start.
| Call | Result |
|---|---|
textContains('Crème brûlée', 'creme') | true |
textContains('Crème brûlée', 'creme', 'accent') | false |
textContains('Crème brûlée', 'crème', 'accent') | true |
textContains('Crème brûlée', 'brûl', 'case') | true |
textContains('Crème brûlée', 'brul', 'case') | false |
textContains('Crème brûlée', '') | true |
textStartsWith('Crème brûlée', 'creme') | true |
textStartsWith('Crème brûlée', 'brulee') | false |
Typeahead
createTypeahead({ timeout }) holds the query of one collection. match(key, labels, currentIndex) adds the typed character and returns the index of the item it points to, or -1: a first character moves to the next item that starts with it, characters typed within timeout (500 ms by default) match a prefix and keep the current item while it still matches, and one character repeated cycles through the items that start with it. It ignores case and accents, and wraps. reset() forgets the query. isTypeaheadKey(event) is true for one printable character other than Space, with no Alt, Ctrl or Meta held.
RovingController takes typeahead: { getLabel } and runs this for you; the menus of MenuTreeController always do. Reach for the helper in a collection with its own key handling, and keep it out of text fields.
Each call below starts a new query on countries = ['Spain', 'Sweden', 'Switzerland'].
| Call | Result |
|---|---|
createTypeahead().match('s', countries, 0) | 1 |
createTypeahead().match('s', countries, 2) | 0 |
createTypeahead().match('x', countries, 0) | -1 |