Autocomplete

A text input that provides real-time suggestions as the user types.

Overview

The Autocomplete component provides an interactive text input with real-time suggestions. Built on Base UI primitives, it supports filtering, custom rendering, grouping, async data loading, and multiple size variants.

Usage

import {
  Autocomplete,
  AutocompleteContent,
  AutocompleteEmpty,
  AutocompleteInput,
  AutocompleteItem,
  AutocompleteList,
} from "@tilt-legal/cubitt-components/primitives";
<Autocomplete items={tags}>
  <Label htmlFor="tags">Search tags</Label>
  <AutocompleteInput id="tags" placeholder="e.g. feature" />
  <AutocompleteContent>
    <AutocompleteEmpty>No tags found.</AutocompleteEmpty>
    <AutocompleteList>
      {(tag) => (
        <AutocompleteItem key={tag.id} value={tag.value}>
          {tag.value}
        </AutocompleteItem>
      )}
    </AutocompleteList>
  </AutocompleteContent>
</Autocomplete>

Examples

Auto Highlight

Automatically highlights the first matching item as you type.

Clear Button

Add a clear button to quickly reset the input value.

Grouped Items

Organize suggestions into categorized groups with labels.

Load suggestions dynamically from an API with loading states.

Fuzzy Matching

Advanced fuzzy search with highlighted matches using match-sorter.

Virtualized List

Handle large datasets efficiently with @tanstack/react-virtual. This example demonstrates virtualization with 10, 000 items.

Size Variants

Three size variants: small, medium (default), and large.

API Reference

Autocomplete

PropTypeDescription
itemsT[]Array of items to display in the autocomplete list. Required
valuestring | number | string[]The controlled value of the input.
defaultValuestring | number | string[]The default value when uncontrolled.
onValueChange(value: string | number | string[]) => voidCallback fired when the input value changes.
openbooleanThe controlled open state of the popup.
defaultOpenbooleanThe default open state when uncontrolled. Defaults to false.
onOpenChange(open: boolean, eventDetails) => voidCallback fired when the open state changes.
autoHighlightbooleanAutomatically highlight the first matching item. Defaults to false.
filter(item: T, value: string) => boolean | nullCustom filter function. Set to null to disable filtering.
itemToStringValue(item: T) => stringFunction to convert an item to a string value.
namestringIdentifies the field when a form is submitted.
disabledbooleanWhether the autocomplete is disabled. Defaults to false.
readOnlybooleanWhether the autocomplete is read-only. Defaults to false.
classNamestringAdditional CSS classes for the root element.

AutocompleteInput

PropTypeDescription
size"sm" | "md" | "lg"Size of the input. Defaults to "md".
classNamestringAdditional CSS classes for the input.

AutocompleteClear

Button to clear the input value.

PropTypeDescription
childrenReactNodeCustom icon/content for the clear button.
classNamestringAdditional CSS classes for the clear button.

AutocompleteWrapper

Container for the input and clear button. Styled like InputWrapper and automatically serves as the anchor for popup positioning.

PropTypeDescription
classNamestringAdditional CSS classes for the container.

AutocompleteContent

Wrapper for the popup content, portal, and positioner.

PropTypeDescription
align"start" | "center" | "end"Alignment of the popup. Defaults to "start".
side"top" | "bottom"Side of the input to display popup. Defaults to "bottom".
sideOffsetnumberOffset from the input in pixels. Defaults to 4.
alignOffsetnumberOffset along the alignment axis. Defaults to 0.
showBackdropbooleanWhether to show a backdrop overlay. Defaults to false.
classNamestringAdditional CSS classes for the popup.

AutocompleteList

Container for autocomplete items.

PropTypeDescription
children(item: T) => ReactElementRender function for each item.
classNamestringAdditional CSS classes for the list.

AutocompleteItem

Individual selectable option.

PropTypeDescription
valueTThe item value.
classNamestringAdditional CSS classes for the item.

AutocompleteGroup

Groups related items together.

PropTypeDescription
itemsT[]Array of items in this group.
classNamestringAdditional CSS classes for the group.

AutocompleteGroupLabel

Label for a group of items.

PropTypeDescription
classNamestringAdditional CSS classes for the group label.

AutocompleteCollection

Nested collection for rendering grouped items.

PropTypeDescription
children(item: T) => ReactElementRender function for each item.

AutocompleteEmpty

Message displayed when no items match the search.

PropTypeDescription
classNamestringAdditional CSS classes for the empty state.

AutocompleteStatus

Status message displayed above the list (e.g., loading, results count).

PropTypeDescription
classNamestringAdditional CSS classes for the status.

On this page