Skip to the content
human-kit design
19 — Pickers

Autocomplete

A list always in sight, with a search row at its head that filters it as you type.

Blood count
Blood glucose
Cholesterol
Ferritin
Hemoglobin A1c
Liver panel
Thyroid panel
Vitamin D
No tests match
No results available

Installation

pnpm dlx @human-kit/design add autocomplete
npx @human-kit/design add autocomplete
yarn dlx @human-kit/design add autocomplete
bunx @human-kit/design add autocomplete

The command copies the files to src/lib/components/ui/autocomplete. It also installs the packages that the files import.

Usage

<script lang="ts">
	import * as Autocomplete from '$lib/components/ui/autocomplete';

	let value = $state(new Set<string | number>());
</script>

<Autocomplete.Root>
	<Autocomplete.Input aria-label="Search tests" placeholder="Search tests…" />
	<Autocomplete.List aria-label="Tests" {value} onChange={(next) => (value = next)}>
		<Autocomplete.Item id="ferritin">Ferritin</Autocomplete.Item>
		<Autocomplete.Item id="vitamin-d">Vitamin D</Autocomplete.Item>
		<Autocomplete.Empty>No tests match</Autocomplete.Empty>
	</Autocomplete.List>
</Autocomplete.Root>

Examples

Filtered by a server

With filter={null} the list stops filtering on its own and shows every row it is given. Bind inputValue and fetch the rows that match it.

Ana María Ruiz
Julián Ortega
Lucía Fernández
Pedro Gómez
Sara Díaz
No patients match
No results available

A long list

For thousands of rows, give the array as items on the root, a snippet that draws one row, and virtualizer on the list: the root filters the array itself and the list draws only the rows near its viewport. Rows are one height, 24px, the rowHeight it is told. Put the empty text in the empty snippet, since the row snippet has no place for it.

E-0001 · cycle 001 · day 5 · AA
E-0002 · cycle 001 · day 6 · CB
E-0003 · cycle 001 · day 5 · BC
E-0004 · cycle 001 · day 6 · BB
E-0005 · cycle 001 · day 5 · BA
E-0006 · cycle 001 · day 6 · AB
E-0007 · cycle 001 · day 5 · AA
E-0008 · cycle 001 · day 6 · CB
E-0009 · cycle 002 · day 5 · BC
4000 results available

Guidelines

  • Use an autocomplete where the list is always in sight, in a side panel, a card or a popover of your own. For a field that opens a list, use a combobox.
  • It has no card of its own: put it inside the box that holds it.
  • Keep the default local filter for a list the page already has, up to a few hundred rows. Pass filter={null} when a server does the search.
  • Use selectionMode="multiple" on the list to pick several rows; each press then adds or takes away one.
  • Always add an Autocomplete.Empty that says what found nothing: "No tests match", not "No results".

Accessibility

  • The input is a searchbox and the list a listbox of options. The focus stays in the input: the arrows move a highlight through the rows, read through aria-activedescendant.
  • ArrowDown and ArrowUp move the highlight, PageDown and PageUp move it a page, Enter picks the highlighted row and Escape clears the search.
  • A hidden live region says how many rows the search left, each time the search changes.
  • Name the input and the list, with aria-label or aria-labelledby: a placeholder is not a name.
  • A press anywhere on the search row lands in the input, not only on its text.

API reference

Autocomplete.Root

PropTypeDefault
inputValuestring

The search text. Bindable.

—
defaultInputValuestring

The search text at the start, when you give no inputValue.

—
onInputChange(value: string) => void

Called when the search text changes.

—
filter((textValue: string, inputValue: string) => boolean) | null

Which rows the search keeps. By default, the rows that contain the text, in any case. null keeps every row.

—
autoHighlightboolean

Highlights the first row that matches each time the search changes.

true
disabledboolean

Turns off the search and the rows.

false
readonlyboolean

Shows the list and the choice, and refuses changes.

false
idstring

The base of the ARIA ids inside. Give a stable one when the page is drawn on a server.

—

It takes these props alone: put attributes on the input or the list.

Autocomplete.Input

PropTypeDefault
aria-labelstring

The name of the search, when no visible label gives one.

—
aria-labelledbystring

The id of the element that names the search.

—
aria-describedbystring

The id of the element that describes the search, such as a hint.

—

Plus every attribute of an <input>, such as placeholder.

Autocomplete.List

PropTypeDefault
selectionMode'single' | 'multiple'

Whether one row can be chosen, or several.

'single'
selectionBehavior'replace' | 'toggle'

What a press on a row does: replace the choice with it, or add it and take it away.

'replace', 'toggle' for multiple
valueIterable<string | number>

The ids of the chosen rows.

—
defaultValueIterable<string | number>

The rows chosen at the start, when you give no value.

—
onChange(value: Set<string | number>) => void

Called with the chosen ids when the user picks a row.

—
controlledValueboolean

Stops the list keeping a choice of its own: it only reports through onChange.

false
disabledKeysIterable<string | number>

The ids of the rows that cannot be picked.

—
itemsIterable<T>

The rows as an array. children then gets one item at a time.

—
aria-labelstring

The name of the list.

—

Plus every attribute of a <div>. The choice lives here, not on the root.

Autocomplete.Item

PropTypeDefault
idstring | number

Required. The id of the row, the one the choice holds.

—
textValuestring

The text the search compares. Without it, the text of the row.

—
disabledboolean

Shows the row and keeps it from being picked.

false

Plus every attribute of a <div>.

Autocomplete.Empty

PropTypeDefault
childrenSnippet

What the row says.

'No results'

Plus every attribute of a <div>. It shows only while the search leaves no row.