Autocomplete
Selects a value from a searchable popover list
Usage
import {Autocomplete, EmptyState, Label, ListBox, SearchField} from "@peakui/react";Anatomy
import {Autocomplete, Description, Label, ListBox, SearchField} from "@peakui/react";
export default () => (
<Autocomplete>
<Label />
<Autocomplete.Trigger>
<Autocomplete.Value />
<Autocomplete.ClearButton />
<Autocomplete.Indicator />
</Autocomplete.Trigger>
<Description />
<Autocomplete.Popover>
<Autocomplete.Filter>
<SearchField>
<SearchField.Group>
<SearchField.SearchIcon />
<SearchField.Input />
<SearchField.ClearButton />
</SearchField.Group>
</SearchField>
<ListBox>
<ListBox.Item>
<ListBox.ItemIndicator />
</ListBox.Item>
</ListBox>
</Autocomplete.Filter>
</Autocomplete.Popover>
</Autocomplete>
);Examples
Variants
Full Width
With Description
Multiple Select
With Sections
With Disabled Options
With Clear Button
Autocomplete.ClearButton also arms Backspace/Delete on the trigger; both call onClear.
Custom Indicator
Required
Controlled
Controlled Open State
Asynchronous Filtering
filter={() => true} hands the filtering to the consumer.
User Selection
Disabled
Customization
Tailwind CSS
Global CSS
@layer components {
.autocomplete {
@apply flex flex-col gap-1;
}
.autocomplete__trigger {
@apply rounded-lg border border-border bg-surface p-2;
}
.autocomplete__value {
@apply text-current;
}
.autocomplete__indicator {
@apply text-muted;
}
.autocomplete__popover {
@apply rounded-lg border border-border bg-surface p-2;
}
}Styling Reference
PeakUI follows the BEM methodology, so variants and states are plain class names you can target.
CSS Classes
The Autocomplete component uses these CSS classes (view source styles):
Base & Element Classes
.autocomplete- Base autocomplete container.autocomplete__trigger- The button that opens the popover.autocomplete__value- The displayed value or placeholder.autocomplete__clear-button- The optional clear control inside the trigger.autocomplete__indicator- The dropdown indicator icon.autocomplete__popover- The popover container
Variant Classes
.autocomplete--primary- Primary variant with shadow (default).autocomplete--secondary- Secondary variant without shadow, suitable for use in surfaces
Modifier Classes
.autocomplete--full-width- Takes the full width of the container.autocomplete__trigger--full-width- Full width on the trigger itself
Slots
[data-slot="autocomplete"][data-slot="autocomplete-trigger"][data-slot="autocomplete-value"][data-slot="autocomplete-indicator"][data-slot="autocomplete-default-indicator"][data-slot="autocomplete-clear-button"][data-slot="autocomplete-clear-button-icon"][data-slot="autocomplete-popover"]
CSS Variables
| Variable | Purpose |
|---|---|
--autocomplete-trigger-bg | Secondary variant trigger background |
--autocomplete-trigger-bg-hover | Secondary variant trigger background on hover |
--autocomplete-trigger-bg-focus | Secondary variant trigger background while focused |
Interactive States
The component supports both CSS pseudo-classes and data attributes:
- Hover:
:hoveror[data-hovered]on trigger - Focus:
:focus-visibleor[data-focus-visible]on trigger - Disabled:
:disabled,[data-disabled]or[aria-disabled="true"]on trigger - Invalid:
[data-invalid]or[aria-invalid="true"]on autocomplete - Placeholder:
[data-placeholder]on value - Empty:
[data-empty]on clear button - Open:
[data-open]on indicator
API Reference
Autocomplete
| Prop | Type | Default | Description |
|---|---|---|---|
placeholder | string | 'Select an item' | Temporary text that occupies the autocomplete when it is empty |
selectionMode | "single" | "multiple" | "single" | Whether single or multiple selection is enabled |
value | Key | Key[] | null | - | Current value (controlled) |
defaultValue | Key | Key[] | null | - | Default value (uncontrolled) |
onChange | (value: Key | Key[] | null) => void | - | Handler called when the value changes |
onClear | () => void | - | Handler called when the selection is cleared |
isOpen | boolean | - | Sets the open state of the popover (controlled) |
defaultOpen | boolean | - | Sets the default open state of the popover (uncontrolled) |
onOpenChange | (isOpen: boolean) => void | - | Handler called when the open state changes |
items | Iterable<T> | - | Items for a dynamic collection |
isDisabled | boolean | - | Whether the autocomplete is disabled |
isRequired | boolean | - | Whether user input is required |
isInvalid | boolean | - | Whether the autocomplete value is invalid |
name | string | - | The name of the input, used when submitting an HTML form |
fullWidth | boolean | false | Whether the autocomplete should take full width of its container |
variant | "primary" | "secondary" | "primary" | Visual variant of the component; secondary is the lower emphasis variant for surfaces |
className | string | (values: SelectRenderProps) => string | - | Additional classes, or a function of the render props |
children | ReactNode | (values: SelectRenderProps) => ReactNode | - | Autocomplete content, or a function of the render props |
...rest | Every React Aria Select attribute is accepted, including aria-label |
Autocomplete.Trigger
| Prop | Type | Default | Description |
|---|---|---|---|
className | string | - | Additional classes |
children | ReactNode | (values: SelectRenderProps) => ReactNode | - | Trigger content, or a function of the render props |
...rest | Every React Aria Button attribute is accepted |
Autocomplete.Value
| Prop | Type | Default | Description |
|---|---|---|---|
className | string | - | Additional classes |
children | ReactNode | (values: SelectValueRenderProps<T>) => ReactNode | - | Value content, or a function of the render props |
...rest | Every <span> attribute is accepted |
Autocomplete.Indicator
A child element is cloned with the classes, data-slot="autocomplete-indicator" and data-open; without a child the default chevron renders.
| Prop | Type | Default | Description |
|---|---|---|---|
className | string | - | Additional classes |
children | ReactNode | - | Custom indicator element |
render | DOMRenderFunction<E, undefined> | - | Replaces the rendered element; E defaults to 'svg' |
...rest | Every <svg> attribute is accepted |
Autocomplete.ClearButton
Renders a span, so it nests validly inside the trigger button; it is aria-hidden and hidden while the selection is empty.
| Prop | Type | Default | Description |
|---|---|---|---|
className | string | - | Additional classes |
children | ReactNode | - | Custom content, replacing the default icon |
onClick | (e: MouseEvent) => void | - | Handler called when the control is clicked |
onPointerDown | (e: PointerEvent) => void | - | Handler called on pointer down |
render | DOMRenderFunction<E, undefined> | - | Replaces the rendered element; E defaults to 'span' |
...rest | Every <span> attribute is accepted |
Autocomplete.Popover
| Prop | Type | Default | Description |
|---|---|---|---|
placement | "bottom" | "bottom left" | "bottom right" | "bottom start" | "bottom end" | "top" | "top left" | "top right" | "top start" | "top end" | "left" | "left top" | "left bottom" | "start" | "start top" | "start bottom" | "right" | "right top" | "right bottom" | "end" | "end top" | "end bottom" | "bottom" | Placement of the popover relative to the trigger |
className | string | - | Additional classes |
children | ReactNode | - | Content children |
...rest | Every React Aria Popover attribute is accepted |
Autocomplete.Filter
Renders no element; it links the SearchField to the ListBox.
| Prop | Type | Default | Description |
|---|---|---|---|
filter | (textValue: string, inputValue: string, node: Node<T>) => boolean | Case- and accent-insensitive "contains" | Decides whether an item stays in the list |
inputValue | string | - | Current search text (controlled) |
defaultInputValue | string | - | Default search text (uncontrolled) |
onInputChange | (value: string) => void | - | Handler called when the search text changes |
disableAutoFocusFirst | boolean | false | Whether the first item stays unfocused while typing |
children | ReactNode | - | A SearchField and a ListBox |
Render Props
Autocomplete and Autocomplete.Trigger children functions receive SelectRenderProps; Autocomplete.Value children functions receive these values:
| Prop | Type | Description |
|---|---|---|
defaultChildren | ReactNode | The default rendered value |
isPlaceholder | boolean | Whether the value is a placeholder |
selectedItems | Node<T>[] | The currently selected items |
selectedText | string | The text of the selected items |
state | SelectState<T> | The state of the select |