Combobox

Slant / Forms

An input that filters a list of options as you type, sync or async.

Demo

Usage

@tuttn/slant
import { Combobox, Field, type ComboboxOption } from '@tuttn/slant'

const people = [
  { value: 'ada', label: 'Ada Lovelace' },
  { value: 'grace', label: 'Grace Hopper', description: 'Compilers' },
  { value: 'hedy', label: 'Hedy Lamarr', disabled: true, reason: 'On leave until March' }, // says why it can't be chosen
]

// Uncontrolled: it keeps its own value; the list filters as the reader types.
<Combobox label="Assignee" options={people} defaultValue="ada" />

// Controlled, in a Field: the label names the input and its list.
const [assignee, setAssignee] = useState<string | null>(null)
<Field invalid={assignee === null}>
  <Field.Label>Assignee</Field.Label>
  <Combobox options={people} value={assignee} onValueChange={setAssignee} />
  <Field.Error>Choose who answers for this task.</Field.Error>
</Field>

// Async: filter on the server, pass the results back, and say when they're on their way.
const [results, setResults] = useState<ComboboxOption[]>([])
const [pending, setPending] = useState(false)
const search = async (text: string) => {
  setPending(true)
  setResults(await searchRepositories(text))
  setPending(false)
}
<Combobox label="Repository" options={results} filter={false} loading={pending} onInputValueChange={search} />

// Free text as well as the options, submitted with a form.
<form>
  <Combobox label="Label" options={labels} allowCustom name="label" />
</form>

Props

PropTypeDefaultDescription
optionsreadonly ComboboxOption[]—The options to choose from. Each `value` is unique; `textValue` (else a string `label`) is what the filter matches and what the input shows once the option is chosen.
valuestring | null—The chosen option's value, or null for none. Pass it to control the choice; leave it out and the combobox keeps its own.
defaultValuestring—The value an uncontrolled combobox starts with.
onValueChange(value: string | null) => void—Called with the chosen option's value, and with null when the text is cleared (or, with `allowCustom`, left matching no option).
inputValuestring—The text in the input. Pass it to control the text; leave it out and the combobox keeps its own.
defaultInputValuestringthe chosen option's text, else ''The text an uncontrolled input starts with.
onInputValueChange(text: string) => void—Called with the new text as the reader types, and when choosing, leaving or clearing the field rewrites it. The hook for an async source: search with it, then pass the results as `options`.
filterfalse | ((option: ComboboxOption, query: string) => boolean)a case-insensitive "contains" on each option's textWhich options the typed text keeps. `false` shows `options` as given, for a source that filters them itself (a server search, say).
loadingbooleanfalseThe options are being fetched: the list is marked busy and shows `messages.loadingResults` under whatever options it has.
emptyText(query: string) => ReactNodequery => `No results for “${query}”`What the list shows when no option matches. From the root's `messages.noResults`.
allowCustombooleanfalseAccept text that matches no option: it stays in the input, `onValueChange` gets null, and a form submits the text. Without it, leaving the field puts the chosen option's text back.
openboolean—Whether the list is open. Pass it with onOpenChange to control it; leave it out and the combobox keeps its own.
defaultOpenbooleanfalseWhether the list starts open, when uncontrolled.
onOpenChange(open: boolean) => void—Called with the new state whenever the list opens or closes.
labelstring—The accessible name of the input and its list, unless a Field's label or `aria-labelledby` names them.
namestring—Submits the chosen value (with `allowCustom`, else the typed text) through a hidden input; empty while nothing is chosen.
formstring—The id of the form the hidden input belongs to, when it sits outside it.
size'sm' | 'md' | 'lg''md'Height on the control scale: 28, 34 or 40px at regular density.
invalidboolean—Sets aria-invalid and a danger border. Inside a Field, the field's `invalid` does this.
placementPlacement'bottom-start'Where the list opens against the field. It flips to the other side when this one lacks room.
…restInputHTMLAttributes<HTMLInputElement>—The ref and every other attribute (id, placeholder, autoFocus, aria-*, handlers) reach the visible <input>; className and style go on the group. `name` and `form` go on the hidden input that carries the value.

Keyboard

  • Typing opens the list, filtered to the matching options, with the first enabled match active, so Enter takes it.
  • ArrowDown opens the list on the chosen option (else the first) and ArrowUp on the last; with the list open they move through it, skipping disabled options and wrapping at the ends. Alt+ArrowDown opens it without moving, Alt+ArrowUp closes it.
  • Enter chooses the active option and closes the list, without submitting a form around the field. With no option active it leaves the text as a blur would, and a form submits as usual.
  • Escape closes the list; with the list closed it clears the text, which chooses nothing. Either way it goes no further, so a dialog around the field stays open. On an empty, closed field it does nothing of its own.
  • Home, End and the other editing keys stay with the input's caret. Tab closes the list without choosing and moves on.

Choosing and leaving

The value is the chosen option's value, and choosing one writes its text (textValue, else a string label) into the input. What the reader types filters the list but chooses nothing until Enter or a click. Leaving the field (blur, Tab, a press outside) puts the chosen option's text back, or empties the input when nothing is chosen, and clearing the text sets the value to null. With allowCustom, the typed text stays: text that names an option (in any case) chooses it, and any other text leaves the value null and is what a form submits.

Opening the list with the arrows or the chevron button shows every option; typing narrows it again. The text is state too: inputValue, defaultInputValue and onInputValueChange control it, and onInputValueChange also hears the rewrites that choosing and leaving make.

Async sources

Pass filter={false} to show options as given, search from onInputValueChange, and set loading while the results are on their way: the list is marked aria-busy and shows messages.loadingResults under whatever options it has. The first result becomes active as it arrives. The chosen option's text is remembered, so it comes back on leaving the field even after the option has dropped out of the results. For a value whose option isn't loaded yet, pass its text as defaultInputValue or inputValue.

Accessibility

The input has role="combobox" with aria-autocomplete="list", and focus never leaves it: aria-activedescendant names the active option, and a press on an option or on the chevron keeps focus in the input. The list is a listbox named like the input: by a Field's label, aria-labelledby or label. When nothing matches, the popup shows emptyText (by default messages.noResults) in place of the list, and the input reports itself collapsed, since there is no listbox to point at. The number of results is announced politely (messages.results) once the list has settled for 400 ms after the text or the options change. The chevron button is named by messages.showOptions and stays out of the tab order, since the arrow keys do the same.

A disabled option with a reason shows it as a line under its label, after any description, readable while the rest of the option dims. It is the option’s accessible description (aria-describedby), kept out of its name so it is read once. The arrows skip the option, typing its name leaves nothing active (so Enter chooses nothing), and a click on it does nothing.

Forms

The visible input carries the text and no name. With name, a hidden input submits the chosen value, or with allowCustom the typed text when no option is chosen; it is empty while nothing is. Inside a Field the field's id, description, error, invalid, disabled, required and readOnly reach the input; read-only, it shows the value and never opens.

Behaviour

The list opens in a portal anchored to the whole field, at least as wide as it, and flips above when there is no room below; inside a Dialog or a Popover it renders inside that layer. For a fixed list without typing, use Select; for several values, Tag input; for running commands rather than choosing a value, Command bar. It is drawn as an Input group.