Krizaka
Documentation

Combobox

A text field that suggests and completes — one value or several as chips, from a static list or a server, with values created from what was typed.

WebBêtaFormulaires

Web uniquement — React, depuis @krizaka/ui/combobox. Bêta : son API peut encore changer à une version mineure.

Quand l’utiliser

  • To choose among many values the person knows by name: a creator, a country, a model, a playlist.
  • With onSearch when the values live on a server (debounced, the previous request aborted).
  • With multiple for several values (collaborators, categories), and creatable when a new one can be added.

Quand ne pas l’utiliser

  • For fewer than about seven options known in advance: radios or a select.À la place : Radio group
  • For free words without a closed list (tags, keywords): a tag input.À la place : Tag input
  • To run a search and show results: a search field.À la place : Search field

Installation

Installer

npm install @krizaka/ui @krizaka/tailwind @krizaka/tokens tailwindcss

Styles

@import "tailwindcss";
@import "@krizaka/tailwind";
@import "@krizaka/ui/tailwind.css";

Importer

import { Combobox } from "@krizaka/ui/combobox";

Exemples

From a server

onSearch: creators fetched as you type, debounced, the stale request aborted.

Several values

Collaborators as chips; Backspace removes the last one, at most three.

EricInès

Up to three collaborators share the revenue of this video.

Create a value

Pick a playlist or create one from what was typed.

With an error

A required choice left empty: the error is read with the field.

Choose your country to receive payouts.

Props

A text field that suggests, filters and completes: one value or several (chips), static or fetched, creatable.

PropTypeDéfautDescription
labelrequisstring—The visible label of the field, passed translated (also its accessible name).
classNamestring—Classes merged on the root.
creatableboolean—Offers to create the query as a new value when no option has that exact label.
debouncenumber—The wait, in ms, between the last key and onSearch. Default 250.
defaultValueComboboxOption | readonly ComboboxOption[] | null—The chosen option at first, uncontrolled. The chosen options at first, uncontrolled.
disabledboolean—Not available.
errorstring—An error under the field: marks it invalid and is read with it.
filter((option: ComboboxOption, query: string) => boolean)—Whether an option matches a query. Default: its label contains the query, ignoring case and accents.
hideLabelboolean—Hides the label visually, keeping it as the accessible name (a search bar with its own context).
hintstring—A help text under the field.
idstring—The id of the text input (default: generated).
labelsPartial<ComboboxLabels>—The words, passed translated; English by default.
maxSelectednumber—Most options that can be chosen (several values only). Most options that can be chosen; the list stops offering more past it.
multipleboolean—One value (default) or several. Several values, shown as chips.
namestring—The name of a hidden input holding the value(s), for a plain <form> post.
onCreate((query: string) => ComboboxOption)—Turns a query into the created option (an id from the server…). Default { value: query, label: query }.
onSearch((query: string, signal: AbortSignal) => Promise<readonly ComboboxOption[]>)—Fetches the options for a query (debounced by debounce); the signal aborts when a newer query starts.
onValueChange((value: ComboboxOption | null) => void) | ((value: ComboboxOption[]) => void)—Called with the chosen option, or null when it is cleared. Called with every chosen option.
optionsreadonly ComboboxOption[]—The options, filtered as the person types (by filter).
placeholderstring—The input's placeholder, passed translated.
separatorsreadonly string[]—Keys that create the query at once, as Enter does ([","] for tags); a paste is split on them too.
valueComboboxOption | readonly ComboboxOption[] | null—The chosen option (controlled), null for none. The chosen options (controlled).

Accessibilité

  • The ARIA 1.2 combobox pattern: the focus stays in the input, aria-activedescendant points at the active option of the listbox.
  • The number of results is announced in a polite live region (labels.results).
  • Chosen options carry aria-selected; each chip's remove button is named (“Remove Eric”).
  • The label is a real <label>; hint and error are wired with aria-describedby, error sets aria-invalid.

Clavier

TouchesAction
Arrow Down / Arrow UpOpen the list and move the active option.
Home / EndFirst or last option, when the list is open.
EnterChoose the active option (or create the typed value).
EscClose the list; for one value, restore its label.
BackspaceIn an empty input, remove the last chip.

Bonnes pratiques

  • Write label as the question (“Collaborators”), the placeholder as an example (“Type a name”).
  • Return at most 20 options from onSearch; put the best match first.
  • Give options a description when names collide (two creators named Eric: their handles).
  • Set maxSelected when the product has a limit, and say it in hint.

Générée depuis le code de @krizaka/ui 2.4.0 : son meta.ts, ses exemples et ses types.Modifier cette documentation sur GitHub

Sur cette page