import { Autocomplete, type AutocompleteSuggestion } from "@senvenli/react/selection";
import { useState } from "react";
const COMPANIES: AutocompleteSuggestion[] = [
{ id: "analytical", label: "Analytical Engines Ltd", description: "London · 12 open invoices" },
{ id: "northwind", label: "Northwind Traders", description: "Seattle · 3 open invoices" },
{ id: "norfolk", label: "Norfolk Paper Co.", description: "Norwich · no open invoices" },
{ id: "acme", label: "Acme Supplies", description: "Chicago · 1 open invoice" },
];
// Stands in for a request to your API; pass `signal` to fetch so superseded requests abort.
async function getSuggestions(text: string) {
const query = text.toLowerCase();
return COMPANIES.filter((company) => company.label.toLowerCase().includes(query));
}
export default function Example() {
const [company, setCompany] = useState("");
return (
<div className="max-w-80">
<Autocomplete
label="Company"
icon="building-2"
placeholder="Start typing a name"
description="Pick a customer or type a new company."
value={company}
onChange={setCompany}
getSuggestions={getSuggestions}
/>
</div>
);
}import { Autocomplete } from "@senvenli/react/selection";Examples
Grouped, with icons
Suggestions under headers, in the order the groups prop gives.import { Autocomplete, type AutocompleteSuggestion } from "@senvenli/react/selection";
import { useState } from "react";
const PLACES: AutocompleteSuggestion[] = [
{ id: "hq", label: "Head office", description: "1 Market Street, San Francisco", icon: "building-2", group: "Saved addresses" },
{ id: "warehouse", label: "Main warehouse", description: "480 Harbor Road, Oakland", icon: "warehouse", group: "Saved addresses" },
{ id: "sfo", label: "San Francisco International Airport", description: "Cargo terminal", icon: "plane", group: "Places" },
{ id: "sjc", label: "San Jose", description: "California", icon: "map-pin", group: "Places" },
{ id: "sac", label: "Sacramento", description: "California", icon: "map-pin", group: "Places" },
];
async function getSuggestions(text: string) {
const query = text.toLowerCase();
return PLACES.filter((place) => `${place.label} ${place.description}`.toLowerCase().includes(query));
}
export default function Example() {
const [address, setAddress] = useState("");
const [picked, setPicked] = useState<AutocompleteSuggestion | null>(null);
return (
<div className="max-w-80">
<Autocomplete
label="Ship to"
icon="map-pin"
placeholder="Address or place"
description={picked ? `Picked: ${picked.label}` : "Try “sa” or “market”."}
groups={["Saved addresses", "Places"]}
value={address}
onChange={(text) => {
setAddress(text);
setPicked(null);
}}
onSelectSuggestion={setPicked}
getSuggestions={getSuggestions}
/>
</div>
);
}Required
Left empty, the field reports itself on blur.import { Autocomplete } from "@senvenli/react/selection";
import { useState } from "react";
const CITIES = ["Amsterdam", "Berlin", "Lisbon", "London", "Madrid", "Paris", "Vienna"];
async function getSuggestions(text: string) {
return CITIES.filter((city) => city.toLowerCase().startsWith(text.toLowerCase())).map((city) => ({ id: city, label: city }));
}
export default function Example() {
const [city, setCity] = useState("");
return (
<div className="max-w-80">
<Autocomplete label="City" isRequired minLength={1} value={city} onChange={setCity} getSuggestions={getSuggestions} />
</div>
);
}Guidelines
When to use it
- Free text where suggestions help: addresses, places.
Use something else when
- The value must be one of a known list. Combo box
Accessibility
Built in
- Announces loading and the suggestion count.
- Required fields show "Fill in this field." after a blur or a submit; your own
errorMessagereplaces it.
You provide
- A visible label.
API reference
Autocomplete
| Prop | Type | Default | Description |
|---|---|---|---|
labelRequired | React.ReactNode | – | |
valueRequired | string | – | The free text; it is a valid value on its own. |
onChangeRequired | (value: string) => void | – | |
getSuggestionsRequired | (text: string, signal: AbortSignal) => Promise<AutocompleteSuggestion[]> | – | Suggestions for the typed text (called after |
onSelectSuggestion | (suggestion: AutocompleteSuggestion) => void | – | Called when a suggestion fills the text, with the suggestion itself (coordinates, ids). |
groups | readonly string[] | – | Group order ("Suggested places" first, then "Addresses"). |
icon | IconName | – | The field's context icon (default |
description | React.ReactNode | – | |
errorMessage | React.ReactNode | – | |
placeholder | string | – | |
isRequired | boolean | – | |
requiredValidation | RequiredValidation | – | Who says an empty required Autocomplete is missing: |
isDisabled | boolean | – | |
isInvalid | boolean | – | |
name | string | – | |
minLength | number | – | Characters before suggesting (default 2). |
debounce | number | – | Debounce in ms (default 250). |
size | FieldSize | – | |
radius | ComponentRadius | – | Corner radius: |
labelRequiredReact.ReactNodevalueRequiredstringThe free text; it is a valid value on its own.
onChangeRequired(value: string) => voidgetSuggestionsRequired(text: string, signal: AbortSignal) => Promise<AutocompleteSuggestion[]>Suggestions for the typed text (called after
minLengthcharacters, debounced).onSelectSuggestion(suggestion: AutocompleteSuggestion) => voidCalled when a suggestion fills the text, with the suggestion itself (coordinates, ids).
groupsreadonly string[]Group order ("Suggested places" first, then "Addresses").
iconIconNameThe field's context icon (default
search).descriptionReact.ReactNodeerrorMessageReact.ReactNodeplaceholderstringisRequiredbooleanrequiredValidationRequiredValidationWho says an empty required Autocomplete is missing:
field(default) shows "Fill in this field." on blur and on a DS Form submit, which it then blocks;apponly marks the field. See {@link RequiredValidation}. The app'sisInvalid(truewith itserrorMessage, orfalse) overrides the check, as on TextField. Disabled, empty and required, it still blocks a DS Form's submit and cannot take focus (as TextField).isDisabledbooleanisInvalidbooleannamestringminLengthnumberCharacters before suggesting (default 2).
debouncenumberDebounce in ms (default 250).
sizeFieldSizeradiusComponentRadiusCorner radius:
none,small,medium(the design),largeorfull. The provider's default otherwise.