Autocomplete

Autocomplete

A searchable dropdown component that allows users to filter and select from a list of options.


Import

jsx
import { Autocomplete } from "asheeui";

Usage

tsx
"use client";
import { useState } from "react";
import { Autocomplete } from "asheeui";
const fruits = [
{ label: "Apple", value: "apple" },
{ label: "Banana", value: "banana" },
{ label: "Cherry", value: "cherry" },
{ label: "Date", value: "date" },
{ label: "Elderberry", value: "elderberry" },
];
export default function Basic() {
const [value, setValue] = useState<string | number>("");
return (
<Autocomplete
value={value}
onValueChange={setValue}
options={fruits}
placeholder="Search for a fruit..."
/>
);
}

Examples

With custom input handling

Typing: waiting for input...

Allow custom values

With below list content

Controlled with selected option

Selected: apple


Inheritance

Autocomplete extends the <Input /> component and inherits all of its props and functionality. This means you can use any Input prop with Autocomplete, including:

  • size, radius, variant, color
  • label, labelAlign, description, message
  • status, required, isLoading, disabled
  • startContent, className, and all native input attributes

Reference: For a complete list of inherited props with detailed descriptions, see the Input documentation.

What Autocomplete adds

Autocomplete extends Input with the following additional features:

FeatureDescription
Options ListA dropdown menu showing filtered suggestions based on user input
Value SelectionSelection handling with onValueChange callback
Custom ValuesSupport for free-form values not in the options list
Below List ContentAbility to render custom content below the options list
Menu ConfigurationFull control over dropdown menu appearance and behavior

What Autocomplete modifies

ModificationDescription
valueControlled selected value, shown as its option label
onChangeNot available - use onValueChange for selection and onInputChange for text changes
childrenNot available - options are provided via the options prop

Props

PropTypeDefaultDescription
optionsSelectMenuOption[][]Array of options to display in dropdown
valuestring | numberNoneControlled selected value
onValueChange(value: string | number, option?: SelectMenuOption) => voidNoneCallback when a value is selected
onInputChange(inputValue: string) => voidNoneCallback when input text changes
allowCustomValuebooleanfalseAllows entering values not in options list
belowListReactNodeNoneContent to display below the option list
menuMenuPropsNoneMenu configuration overrides (see below)
placeholderstring"Type to search..."Input placeholder text

Excluded Props

The following Input props are not available on Autocomplete:

PropReason
onChangeReplaced by onValueChange and onInputChange
childrenNot applicable - options are provided via options prop

All other Input props are fully supported. See the Input documentation for the complete list.

SelectMenuOption

PropTypeDescription
labelstringDisplay text for the option
valuestring | numberValue of the option
disabledbooleanPrevents selection of this option
[key: string]unknownAdditional custom properties

The dropdown menu appearance is controlled through the menu prop, which accepts all MenuConfig options:

PropTypeDefaultDescription
menu.radiusRadius"md"*Corner rounding of the dropdown menu
menu.sizeSize"md"*Size of menu items
menu.itemVariantVariant"ghost"*Visual style of inactive options
menu.itemColorColor"primary"*Color of inactive options
menu.activeItemVariantVariant"faded"*Visual style of selected option
menu.activeItemColorColor"primary"*Color of selected option
menu.lockScrollbooleanfalse*Whether to lock body scroll when open
menu.portalbooleantrue*Whether to render menu in a portal
menu.portalTargetHTMLElement | nullnull*Custom portal target element
menu.classNamestringNoneExtra classes for the dropdown menu

* Falls back through Global Configuration if not set. See below.


Global Configuration

Autocomplete reads defaults from four places, in this order of precedence:

  1. Instance prop: set directly on <Autocomplete />
  2. Component config: components.autocomplete in your ashee.config
  3. Theme default: defaultVariant / defaultRadius / defaultColor in your ashee.config
  4. Built-in fallback: component's internal default values

Component config

ts
// ashee.config.ts
import type { ExternalConfig } from "asheeui";
export const config: ExternalConfig = {
components: {
autocomplete: {
size: "md",
radius: "md",
variant: "bordered",
color: "primary",
labelAlign: "left",
menu: {
itemVariant: "ghost",
itemColor: "primary",
activeItemVariant: "faded",
activeItemColor: "primary",
radius: "md",
size: "md",
portal: true,
lockScroll: false,
},
},
},
};

Built-in fallbacks

ts
{
size: "md",
radius: "md",
variant: "bordered",
color: "primary",
labelAlign: "left",
menu: {
itemVariant: "ghost",
itemColor: "primary",
activeItemVariant: "faded",
activeItemColor: "primary",
radius: "md",
size: "md",
portal: true,
lockScroll: false,
},
}

Accessibility

  • Renders with proper ARIA roles (combobox, listbox)
  • Uses aria-expanded and aria-autocomplete for state indication
  • Supports keyboard navigation and selection
  • disabled items are not focusable or selectable
  • Implements focus-visible indicators
  • Uses Floating UI for robust positioning

Portal Behavior

The Autocomplete dropdown menu is rendered in a React portal by default. This means the menu is attached to document.body rather than staying in the component's DOM hierarchy.

Why use a portal?

  • Escapes CSS containment: The menu appears above other content even when inside containers with overflow: hidden or contain: layout
  • Avoids stacking context issues: The menu maintains proper z-index regardless of parent stacking contexts
  • Works with any parent: The menu functions correctly regardless of where the Autocomplete is placed in the component tree
  • Prevents clipping: The menu is never clipped by parent containers

When to disable the portal

You may want to disable the portal (by setting menu.portal={false}) when:

  • You need the menu to stay within a specific container for testing purposes
  • You are rendering inside a shadow DOM or iframe where document.body is not appropriate
  • You have specific layout requirements that depend on the menu remaining in the DOM hierarchy

You can also provide a custom menu.portalTarget to render the menu into a specific container instead of document.body.


Notes

  • Controlled Usage: Use value/onValueChange for controlled behavior.
  • Custom Values: When allowCustomValue is enabled, ensure your application handles string values appropriately.
  • Filtering: Options are filtered in real-time, case-insensitive, matching against option labels.
  • Performance: For large option lists (1000+ items), consider implementing virtual scrolling or server-side filtering.
  • Keyboard Navigation: Up/Down arrows navigate options, Enter selects, Escape closes.
  • Portal: The dropdown menu is portaled to document.body by default. This can be disabled via the menu.portal prop or component config.
  • Menu Configuration: All menu appearance options are consolidated into the menu prop for cleaner API surface.
  • Inheritance: Autocomplete inherits all Input props except onChange and children.
Previous

← Accordion

Next

Button →