# Autocomplete

A filterable text input that suggests matching values.

A filterable text input that suggests matching values.

<p className="zenos-base-ui-source">
  Backed by <a href="https://base-ui.com/react/components/autocomplete">Base UI Autocomplete</a> for
  upstream behavior and configuration. Zenos owns the public API, styling, and composition shown on
  this page.
</p>

```tsx
import { Autocomplete, Field } from "@zentara-co/zenos-ui";

const cities = ["Bandung", "Jakarta", "Surabaya"];

function AutocompleteExample() {
  return (
    <Field.Root className="w-full max-w-sm">
      <Field.Label>City</Field.Label>
      <Autocomplete.Root items={cities}>
        <Autocomplete.InputGroup>
          <Autocomplete.Input placeholder="Search cities" />
          <Autocomplete.Trigger aria-label="Show cities" />
        </Autocomplete.InputGroup>
        <Autocomplete.Content>
          <Autocomplete.Empty>No cities found.</Autocomplete.Empty>
          <Autocomplete.List>
            {(city: string) => (
              <Autocomplete.Item key={city} value={city}>
                {city}
              </Autocomplete.Item>
            )}
          </Autocomplete.List>
        </Autocomplete.Content>
      </Autocomplete.Root>
    </Field.Root>
  );
}
```

## Import

```tsx
import { Autocomplete } from "@zentara-co/zenos-ui";
```

## Sizes

`Autocomplete.InputGroup` uses the shared `sm`, `md`, and `lg` control scale from Input.

```tsx
import { Autocomplete, Stack } from "@zentara-co/zenos-ui";

const cities = ["Bandung", "Jakarta", "Surabaya"];

function AutocompleteSizesExample() {
  return (
    <Stack gap="3" className="w-full max-w-sm">
      {(["sm", "md", "lg"] as const).map((size) => (
        <Autocomplete.Root key={size} items={cities}>
          <Autocomplete.InputGroup size={size}>
            <Autocomplete.Input placeholder={`Size ${size}`} />
            <Autocomplete.Trigger aria-label={`Show ${size} city suggestions`} />
          </Autocomplete.InputGroup>
        </Autocomplete.Root>
      ))}
    </Stack>
  );
}
```

## Variants

Choose a surface that matches the field hierarchy. `default` is the standard bordered control, `filled` is the subtle filled surface, and `none` is borderless for inline editing—the same variants as [`Input`](/components/input/).

```tsx
import { Autocomplete, Field, Grid } from "@zentara-co/zenos-ui";

const cities = ["Bandung", "Jakarta", "Surabaya"];

function AutocompleteVariantsExample() {
  return (
    <Grid columns={{ base: 1, md: 3 }} gap="4" className="w-full">
      {(["default", "filled", "none"] as const).map((variant) => (
        <Field.Root key={variant}>
          <Field.Label className="capitalize">{variant}</Field.Label>
          <Autocomplete.Root items={cities}>
            <Autocomplete.InputGroup variant={variant}>
              <Autocomplete.Input placeholder="Search cities" />
              <Autocomplete.Trigger aria-label={`Show ${variant} city suggestions`} />
            </Autocomplete.InputGroup>
            <Autocomplete.Content>
              <Autocomplete.Empty>No cities found.</Autocomplete.Empty>
              <Autocomplete.List>
                {(city: string) => (
                  <Autocomplete.Item key={city} value={city}>
                    {city}
                  </Autocomplete.Item>
                )}
              </Autocomplete.List>
            </Autocomplete.Content>
          </Autocomplete.Root>
        </Field.Root>
      ))}
    </Grid>
  );
}
```

## Custom styling

Every Autocomplete part accepts `className`. Style `Autocomplete.InputGroup` for the field, then style `Autocomplete.Content` separately because the popup is rendered in a portal. Stable `data-slot` values and the `data-highlighted` state can restyle suggestions without replacing filtering or keyboard behavior. Pass `positionerClassName` on `Content` when the positioning wrapper needs classes of its own.

```tsx
import { Autocomplete, Field } from "@zentara-co/zenos-ui";

const cities = ["Bandung", "Jakarta", "Surabaya"];

function AutocompleteCustomStylingExample() {
  return (
    <Field.Root className="w-full max-w-sm">
      <Field.Label>Workspace city</Field.Label>
      <Autocomplete.Root items={cities}>
        <Autocomplete.InputGroup className="rounded-full border-border-accent-teal-default bg-background-accent-teal-subtlest-default hover:bg-background-accent-teal-subtlest-hover has-[[data-slot=autocomplete-input]:focus-visible]:!border-border-accent-teal-default has-[[data-slot=autocomplete-input]:focus-visible]:!bg-background-accent-teal-subtlest-default has-[[data-slot=autocomplete-trigger]:focus-visible]:!border-border-accent-teal-default has-[[data-slot=autocomplete-trigger]:focus-visible]:!bg-background-accent-teal-subtlest-default">
          <Autocomplete.Input
            placeholder="Search cities"
            className="text-text-accent-teal-bolder placeholder:text-text-accent-teal-bolder/70"
          />
          <Autocomplete.Trigger
            aria-label="Show branded city suggestions"
            className="text-text-accent-teal-bolder hover:bg-transparent hover:text-text-accent-teal-bolder"
          />
        </Autocomplete.InputGroup>
        <Autocomplete.Content className="rounded-xl border border-border-accent-teal-default bg-background-accent-teal-subtlest-default shadow-overlay dark:border-border-accent-teal-default dark:bg-background-accent-teal-subtlest-default">
          <Autocomplete.Empty className="text-text-accent-teal-bolder dark:text-text-accent-teal-bolder">
            No cities found.
          </Autocomplete.Empty>
          <Autocomplete.List>
            {(city: string) => (
              <Autocomplete.Item
                key={city}
                value={city}
                className="rounded-lg text-text-accent-teal-bolder data-[highlighted]:bg-background-accent-teal-subtlest-hover dark:text-text-accent-teal-bolder dark:data-[highlighted]:bg-background-accent-teal-subtlest-hover"
              >
                {city}
              </Autocomplete.Item>
            )}
          </Autocomplete.List>
        </Autocomplete.Content>
      </Autocomplete.Root>
      <Field.Description>A branded treatment built from semantic color tokens.</Field.Description>
    </Field.Root>
  );
}
```

## Grouped suggestions

Pass an array of groups to `items`, with an `items` array on each group. Keep any label or other metadata alongside those items. Render the filtered groups through `Autocomplete.List`, then pass each group's filtered `items` to `Autocomplete.Group`.

Use `Autocomplete.GroupLabel` to name the group and `Autocomplete.Collection` to render its suggestions. Collection does not add a wrapper element; it uses the items supplied by its group. Filtering matches the suggestions, hides groups with no matches, and keeps keyboard navigation on options rather than group headings.

```tsx
import { Autocomplete, Field } from "@zentara-co/zenos-ui";

function AutocompleteGroupedExample() {
  const groups = [
    { label: "West Java", items: ["Bandung", "Bogor"] },
    { label: "East Java", items: ["Malang", "Surabaya"] },
  ];

  return (
    <Field.Root className="w-full max-w-sm">
      <Field.Label>City by province</Field.Label>
      <Autocomplete.Root<string> items={groups}>
        <Autocomplete.InputGroup>
          <Autocomplete.Input placeholder="Search cities" />
          <Autocomplete.Clear aria-label="Clear grouped city" />
          <Autocomplete.Trigger aria-label="Show grouped cities" />
        </Autocomplete.InputGroup>
        <Autocomplete.Content>
          <Autocomplete.Empty>No cities found.</Autocomplete.Empty>
          <Autocomplete.List>
            {(group: (typeof groups)[number]) => (
              <Autocomplete.Group key={group.label} items={group.items} className="not-first:mt-2">
                <Autocomplete.GroupLabel>{group.label}</Autocomplete.GroupLabel>
                <Autocomplete.Collection>
                  {(city: string) => (
                    <Autocomplete.Item key={city} value={city}>
                      {city}
                    </Autocomplete.Item>
                  )}
                </Autocomplete.Collection>
              </Autocomplete.Group>
            )}
          </Autocomplete.List>
        </Autocomplete.Content>
        <Field.Description>
          Suggestions are grouped by province; you can also enter another city.
        </Field.Description>
      </Autocomplete.Root>
    </Field.Root>
  );
}
```

Do not map the original groups directly when using built-in filtering: the function child of `Autocomplete.List` receives the filtered groups. For object suggestions, provide `itemToStringValue` to define the text used for filtering and input completion, and pass the full item to `Autocomplete.Item value`.

`Group` and `GroupLabel` accept native props, refs, and `className` for custom spacing or heading styling. Group labels describe suggestions; they are not selectable options. Keep `Autocomplete.Empty` in the popup to explain when no group has matching results.

## Opening

The suggestion popup opens when the input receives focus. Set `openOnFocus={false}` when the list should wait for typing or `Autocomplete.Trigger`.

Focus the first field to open its suggestions. The second field stays closed until you type or press its trigger.

```tsx
import { Autocomplete, Field, Grid } from "@zentara-co/zenos-ui";

const cities = ["Bandung", "Jakarta", "Surabaya"];

function AutocompleteOpeningExample() {
  return (
    <Grid columns={{ base: 1, md: 2 }} gap="4" className="w-full">
      <Field.Root>
        <Field.Label>Opens on focus</Field.Label>
        <Autocomplete.Root items={cities}>
          <Autocomplete.InputGroup>
            <Autocomplete.Input placeholder="Search cities" />
            <Autocomplete.Trigger aria-label="Show cities that open on focus" />
          </Autocomplete.InputGroup>
          <Autocomplete.Content>
            <Autocomplete.Empty>No cities found.</Autocomplete.Empty>
            <Autocomplete.List>
              {(city: string) => (
                <Autocomplete.Item key={city} value={city}>
                  {city}
                </Autocomplete.Item>
              )}
            </Autocomplete.List>
          </Autocomplete.Content>
        </Autocomplete.Root>
        <Field.Description>Focus opens the suggestions.</Field.Description>
      </Field.Root>
      <Field.Root>
        <Field.Label>Opens from typing</Field.Label>
        <Autocomplete.Root items={cities} openOnFocus={false}>
          <Autocomplete.InputGroup>
            <Autocomplete.Input placeholder="Search cities" />
            <Autocomplete.Trigger aria-label="Show cities after typing" />
          </Autocomplete.InputGroup>
          <Autocomplete.Content>
            <Autocomplete.Empty>No cities found.</Autocomplete.Empty>
            <Autocomplete.List>
              {(city: string) => (
                <Autocomplete.Item key={city} value={city}>
                  {city}
                </Autocomplete.Item>
              )}
            </Autocomplete.List>
          </Autocomplete.Content>
        </Autocomplete.Root>
        <Field.Description>Focus keeps the list closed. Type or use the trigger.</Field.Description>
      </Field.Root>
    </Grid>
  );
}
```

## Controlled value

Use `value` and `onValueChange` when selection belongs to application state. `Autocomplete.Clear` resets the value through the same callback.

```tsx
import { Autocomplete, Field, Stack, Text } from "@zentara-co/zenos-ui";
import { useState } from "react";

const cities = ["Bandung", "Jakarta", "Surabaya"];

function AutocompleteControlledExample() {
  const [value, setValue] = useState("Jakarta");

  return (
    <Stack gap="2" className="w-full max-w-sm">
      <Field.Root>
        <Field.Label>City</Field.Label>
        <Autocomplete.Root items={cities} value={value} onValueChange={setValue}>
          <Autocomplete.InputGroup>
            <Autocomplete.Input placeholder="Search cities" />
            <Autocomplete.Clear aria-label="Clear city" />
            <Autocomplete.Trigger aria-label="Show cities" />
          </Autocomplete.InputGroup>
          <Autocomplete.Content>
            <Autocomplete.Empty>No cities found.</Autocomplete.Empty>
            <Autocomplete.List>
              {(city: string) => (
                <Autocomplete.Item key={city} value={city}>
                  {city}
                </Autocomplete.Item>
              )}
            </Autocomplete.List>
          </Autocomplete.Content>
        </Autocomplete.Root>
      </Field.Root>
      <Text size="s" color="subtle">
        Selected: {value || "None"}
      </Text>
    </Stack>
  );
}
```

## Motion

`Autocomplete.Content` fades, scales, and moves from its resolved side when it opens, then reverses that movement when it closes. The animation uses the fast motion rhythm and automatically stands down for reduced-motion preferences.

Open both fields. The first popup animates in and out. `disableMotion` makes the second appear and disappear immediately.

```tsx
import { Autocomplete, Field, Grid } from "@zentara-co/zenos-ui";

const cities = ["Bandung", "Jakarta", "Surabaya"];

function AutocompleteMotionExample() {
  return (
    <Grid columns={{ base: 1, md: 2 }} gap="4" className="w-full">
      <Field.Root>
        <Field.Label>With motion</Field.Label>
        <Autocomplete.Root items={cities}>
          <Autocomplete.InputGroup>
            <Autocomplete.Input placeholder="Search cities" />
            <Autocomplete.Trigger aria-label="Show cities with motion" />
          </Autocomplete.InputGroup>
          <Autocomplete.Content>
            <Autocomplete.Empty>No cities found.</Autocomplete.Empty>
            <Autocomplete.List>
              {(city: string) => (
                <Autocomplete.Item key={city} value={city}>
                  {city}
                </Autocomplete.Item>
              )}
            </Autocomplete.List>
          </Autocomplete.Content>
        </Autocomplete.Root>
      </Field.Root>
      <Field.Root>
        <Field.Label>Without motion</Field.Label>
        <Autocomplete.Root items={cities}>
          <Autocomplete.InputGroup>
            <Autocomplete.Input placeholder="Search cities" />
            <Autocomplete.Trigger aria-label="Show cities without motion" />
          </Autocomplete.InputGroup>
          <Autocomplete.Content disableMotion>
            <Autocomplete.Empty>No cities found.</Autocomplete.Empty>
            <Autocomplete.List>
              {(city: string) => (
                <Autocomplete.Item key={city} value={city}>
                  {city}
                </Autocomplete.Item>
              )}
            </Autocomplete.List>
          </Autocomplete.Content>
        </Autocomplete.Root>
      </Field.Root>
    </Grid>
  );
}
```

## Disabled and invalid

Place Autocomplete inside `Field` to share disabled and invalid semantics with its label, description, and error message. Hover, focus, invalid, and disabled treatments follow the Input spec: the **input group** owns the 2px focused border and pressed background (no inner focus ring on the text field or trigger). Invalid groups use the same danger-subtle border and accent red subtlest fill as Input.

```tsx
import { Autocomplete, Field, Grid } from "@zentara-co/zenos-ui";

const cities = ["Bandung", "Jakarta", "Surabaya"];

function AutocompleteStatesExample() {
  return (
    <Grid columns={{ base: 1, md: 2 }} gap="4" className="w-full">
      <Field.Root disabled>
        <Field.Label>Disabled</Field.Label>
        <Autocomplete.Root items={cities} disabled>
          <Autocomplete.InputGroup>
            <Autocomplete.Input placeholder="Search cities" />
            <Autocomplete.Trigger aria-label="Show disabled city suggestions" />
          </Autocomplete.InputGroup>
        </Autocomplete.Root>
      </Field.Root>
      <Field.Root invalid>
        <Field.Label>Invalid</Field.Label>
        <Autocomplete.Root items={cities}>
          <Autocomplete.InputGroup>
            <Autocomplete.Input placeholder="Search cities" />
            <Autocomplete.Trigger aria-label="Show city suggestions" />
          </Autocomplete.InputGroup>
          <Autocomplete.Content>
            <Autocomplete.List>
              {(city: string) => (
                <Autocomplete.Item key={city} value={city}>
                  {city}
                </Autocomplete.Item>
              )}
            </Autocomplete.List>
          </Autocomplete.Content>
        </Autocomplete.Root>
        <Field.Error>Select a supported city.</Field.Error>
      </Field.Root>
    </Grid>
  );
}
```

## Anatomy

```text
Autocomplete.Root
Autocomplete.InputGroup
Autocomplete.Input
Autocomplete.Trigger
Autocomplete.Clear
Autocomplete.Content
Autocomplete.List
Autocomplete.Item
Autocomplete.Empty
Autocomplete.Group
Autocomplete.GroupLabel
Autocomplete.Collection
Autocomplete.Separator
Autocomplete.Status
```

## API

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| value | ItemValue | — | Controlled input value. |
| defaultValue | ItemValue | — | Initial uncontrolled value. |
| onValueChange | (value: ItemValue) => void | — | Runs when selection changes. |
| openOnFocus | boolean | true | Opens the suggestion popup when the input receives focus. Typing and Autocomplete.Trigger still open it when this is false. |
| items | readonly ItemValue[] \| readonly { items: readonly ItemValue[] }[] | — | Flat suggestions or groups containing suggestions. Groups may include labels and other metadata; List renders the filtered groups. |
| itemToStringValue | (item: ItemValue) => string | — | Text used to filter and complete object suggestions. Receives individual suggestions, not their groups. |
| Group.items | readonly ItemValue[] | — | The filtered suggestions for this group. Its nested Collection renders these items. |
| GroupLabel.children | ReactNode | — | Visible heading and accessible name for a suggestion group. |
| Collection.children | (item: ItemValue, index: number) => ReactNode | — | Renders the group's suggestions without an additional DOM wrapper. |
| size | 'sm' \| 'md' \| 'lg' | md | Input group height (`Autocomplete.InputGroup` / `Autocomplete.Input`). |
| variant | 'default' \| 'filled' \| 'none' | default | Surface treatment on `Autocomplete.InputGroup` (`default`, `filled`, or `none`). |
| Content.disableMotion | boolean | false | Disables the popup enter and exit animation for this instance. |
| className | string | — | Merged last on every part. Style InputGroup on the field and Content for the portaled popup. |
| Content.positionerClassName | string | — | Classes on the positioning wrapper around the popup. |

Unless narrowed above, each DOM-rendering part also accepts its underlying native or Base UI props, including className, style, aria-*, and data-*.

## Accessibility

Autocomplete preserves Base UI's combobox semantics, filtering, active-option announcements, and keyboard navigation. Provide a visible label through `Field.Label`, label icon-only trigger and clear controls, and keep empty-state text meaningful. In grouped lists, use `GroupLabel` so each group has an accessible name; arrow keys navigate its options and continue across group boundaries.

Source: https://zenos-ui.pages.dev/components/autocomplete/