Autocomplete

View as Markdown

A text field that suggests as you type and keeps what you write, from a list or looked up as the text changes.

Job title

When to use

A text field that suggests as you type and keeps what you write, from a list or looked up as the text changes. Use Combobox instead when people must pick from the list, not type their own value.

Usage

import { Autocomplete } from "./ui/uicontext/components/Autocomplete.ts"
const Message = UiSchema.TaggedEnum({ ChangedCity: { value: Schema.String } })const cities = ["Lisbon", "London", "Los Angeles", "Lyon"].map((label) => ({ value: label, label }))export const City = (h: Html.H<typeof Message.Type>, city: string) =>	Autocomplete(h, { key: "city", props: { label: "City", value: city, suggestions: cities }, onEmit: { Changed: ({ value }) => Message.ChangedCity({ value }) } })

Examples

Looked up as you type

remote shows the suggestions the app gives, unfiltered; loading says it's looking. A newer query replaces an older one.

Street address

API reference

Props

Autocomplete props
NameTypeDescription
labelrequiredstringNames the field; shown above it unless hideLabel
hideLabelboolean—
valuerequiredstring—
suggestionsrequiredAutocompleteSuggestion[]—
remotebooleanThe app filters: the list shows suggestions as given, for a lookup as the text changes
loadingbooleanThe app is still looking: "Looking…" shows in the list
placeholderstring—
namestringSubmits the text under this name
autocompletestringThe browser's own autofill hint: "address-level2" for a city
emptystringWhat the list says when nothing matches; "No suggestions" by default
disabledboolean—

Events

Autocomplete events
EventPayloadDescription
Changed{ value: string }The text, typed or filled in by a suggestion

Keyboard and accessibility

KeysDoes
a letterType; the suggestions follow
Down arrow Up arrowOpen the suggestions, and move through them
EnterFill in the highlighted suggestion
EscapeClose the suggestions; again, clear the text

Installation

pnpm dlx shadcn@latest add checkfu/uicontext/autocomplete

Source