PeakUI

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

VariablePurpose
--autocomplete-trigger-bgSecondary variant trigger background
--autocomplete-trigger-bg-hoverSecondary variant trigger background on hover
--autocomplete-trigger-bg-focusSecondary variant trigger background while focused

Interactive States

The component supports both CSS pseudo-classes and data attributes:

  • Hover: :hover or [data-hovered] on trigger
  • Focus: :focus-visible or [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

PropTypeDefaultDescription
placeholderstring'Select an item'Temporary text that occupies the autocomplete when it is empty
selectionMode"single" | "multiple""single"Whether single or multiple selection is enabled
valueKey | Key[] | null-Current value (controlled)
defaultValueKey | 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
isOpenboolean-Sets the open state of the popover (controlled)
defaultOpenboolean-Sets the default open state of the popover (uncontrolled)
onOpenChange(isOpen: boolean) => void-Handler called when the open state changes
itemsIterable<T>-Items for a dynamic collection
isDisabledboolean-Whether the autocomplete is disabled
isRequiredboolean-Whether user input is required
isInvalidboolean-Whether the autocomplete value is invalid
namestring-The name of the input, used when submitting an HTML form
fullWidthbooleanfalseWhether 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
classNamestring | (values: SelectRenderProps) => string-Additional classes, or a function of the render props
childrenReactNode | (values: SelectRenderProps) => ReactNode-Autocomplete content, or a function of the render props
...restEvery React Aria Select attribute is accepted, including aria-label

Autocomplete.Trigger

PropTypeDefaultDescription
classNamestring-Additional classes
childrenReactNode | (values: SelectRenderProps) => ReactNode-Trigger content, or a function of the render props
...restEvery React Aria Button attribute is accepted

Autocomplete.Value

PropTypeDefaultDescription
classNamestring-Additional classes
childrenReactNode | (values: SelectValueRenderProps<T>) => ReactNode-Value content, or a function of the render props
...restEvery <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.

PropTypeDefaultDescription
classNamestring-Additional classes
childrenReactNode-Custom indicator element
renderDOMRenderFunction<E, undefined>-Replaces the rendered element; E defaults to 'svg'
...restEvery <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.

PropTypeDefaultDescription
classNamestring-Additional classes
childrenReactNode-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
renderDOMRenderFunction<E, undefined>-Replaces the rendered element; E defaults to 'span'
...restEvery <span> attribute is accepted

Autocomplete.Popover

PropTypeDefaultDescription
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
classNamestring-Additional classes
childrenReactNode-Content children
...restEvery React Aria Popover attribute is accepted

Autocomplete.Filter

Renders no element; it links the SearchField to the ListBox.

PropTypeDefaultDescription
filter(textValue: string, inputValue: string, node: Node<T>) => booleanCase- and accent-insensitive "contains"Decides whether an item stays in the list
inputValuestring-Current search text (controlled)
defaultInputValuestring-Default search text (uncontrolled)
onInputChange(value: string) => void-Handler called when the search text changes
disableAutoFocusFirstbooleanfalseWhether the first item stays unfocused while typing
childrenReactNode-A SearchField and a ListBox

Render Props

Autocomplete and Autocomplete.Trigger children functions receive SelectRenderProps; Autocomplete.Value children functions receive these values:

PropTypeDescription
defaultChildrenReactNodeThe default rendered value
isPlaceholderbooleanWhether the value is a placeholder
selectedItemsNode<T>[]The currently selected items
selectedTextstringThe text of the selected items
stateSelectState<T>The state of the select

On this page