Selection & Dates
@hacline/ui/autocompleteAutocomplete
Provide suggestions for free-form text input.
Usage
Typing "Design" shows matching tags, while new terms can be kept directly as entered. When items is provided, an empty AutocompleteList renders the current suggestions.
"use client";
import {
Autocomplete,
AutocompleteContent,
AutocompleteEmpty,
AutocompleteInput,
AutocompleteLabel,
AutocompleteList,
} from "@/components/ui/hacline/components/autocomplete";
const tags = [
"Design review",
"Design system",
"Project plan",
"Release notes",
];
export const Example = () => (
<div className="grid w-full max-w-xs gap-2">
<Autocomplete items={tags} name="tag">
<AutocompleteLabel>Document tag</AutocompleteLabel>
<AutocompleteInput placeholder='Type "Design" or a new tag' />
<AutocompleteContent>
<AutocompleteEmpty>Use your typed tag directly</AutocompleteEmpty>
<AutocompleteList />
</AutocompleteContent>
</Autocomplete>
</div>
);Controlled Input and Clearing
Synchronize application state with value and clear the input with AutocompleteClear. This example matches only cities starting with the entered text.
"use client";
import { useState } from "react";
import {
Autocomplete,
AutocompleteClear,
AutocompleteContent,
AutocompleteInput,
AutocompleteLabel,
AutocompleteList,
} from "@/components/ui/hacline/components/autocomplete";
export const Example = () => {
const [value, setValue] = useState("Seattle");
return (
<div className="grid w-full max-w-xs gap-2">
<Autocomplete
filter="startsWith"
items={["Seattle", "Portland", "Denver"]}
onValueChange={setValue}
value={value}
>
<AutocompleteLabel>Shipping city</AutocompleteLabel>
<div className="flex items-center gap-2">
<AutocompleteInput className="w-full" />
<AutocompleteClear aria-label="Clear city" />
</div>
<AutocompleteContent>
<AutocompleteList />
</AutocompleteContent>
</Autocomplete>
<output className="text-hl-fg-muted text-xs">
City: {value || "Not set"}
</output>
</div>
);
};Parts
| Part | Description | Extended Props |
|---|---|---|
AutocompleteArrow | An arrow on the popup's edge that points at the field. | — |
AutocompleteClear | A button that empties the field. Shown while there's text to clear. Name it with aria-label. | — |
AutocompleteContent | The popup that holds the suggestions, positioned against the field. | — |
AutocompleteEmpty | Shown while no suggestion matches. Its message is announced when it appears. | — |
AutocompleteGroup | A group of items, named by its AutocompleteGroupLabel, and hidden while none of its items shows. | — |
AutocompleteGroupLabel | The group's heading, which names the group. | — |
AutocompleteInput | The text field. It's the form control, so the text submits under the autocomplete's name. | size |
AutocompleteItem | A suggestion. Choosing it fills the field in with its text. | — |
AutocompleteItemIcon | An icon inside a suggestion. Screen readers, typeahead and filtering skip it, and mirror flips it in right-to-left text. | — |
AutocompleteItems | Renders data suggestions. Used by data autocompletes, and available for custom layouts. | — |
AutocompleteItemText | The suggestion's text, with the part the field's text matched in <mark>. Without string content, it shows the suggestion's label. | — |
AutocompleteLabel | A visible label for the field. | — |
AutocompleteList | The listbox. Without children, it renders the autocomplete's items, filtered. | — |
AutocompleteSeparator | A line between items or groups, hidden from screen readers. | — |
AutocompleteStatus | A polite live region for loading and result counts. Its content is yours, usually a function of its state. | — |
AutocompleteTrigger | A button that opens and closes the suggestions, out of the tab order. | size |
Props
Prop
Type