Autocomplete

A text field that suggests as you type and lets you keep what you typed, on the input's surface over the menu's rows.

01Preview

01
Full screen

Suggestions fill the field; anything else can still be typed.

02Installation

npx shadcn@latest add https://aiellie.app/r/autocomplete.json

03API reference

22
From the Base UI documentation

Groups all parts of the autocomplete. Doesn't render its own HTML element.

Props

PropTypeDefaultDescription
namestring-Identifies the field when a form is submitted.
defaultValuestring | number | string[]-The uncontrolled input value of the autocomplete when it's initially rendered. To render a controlled autocomplete, use the value prop instead.
valuestring | string[] | number-The input value of the autocomplete. Use when controlled.
onValueChange((value: string, eventDetails: Autocomplete.Root.ChangeEventDetails) => void)-Event handler called when the input value of the autocomplete changes.
defaultOpenbooleanfalseWhether the popup is initially open. To render a controlled popup, use the open prop instead.
openboolean-Whether the popup is currently open. Use when controlled.
onOpenChange((open: boolean, eventDetails: Autocomplete.Root.ChangeEventDetails) => void)-Event handler called when the popup is opened or closed.
autoHighlightboolean | 'always'falseWhether the first matching item is highlighted automatically. true: highlight after the user types and keep the highlight while the query changes.'always': always highlight the first item.
keepHighlightbooleanfalseWhether the highlighted item should be preserved when the pointer leaves the list.
highlightItemOnHoverbooleantrueWhether moving the pointer over items should highlight them. Disabling this prop allows CSS :hover to be differentiated from the :focus (data-highlighted) state.
actionsRefReact.RefObject<Autocomplete.Root.Actions | null>-A ref to imperative actions. unmount: Manually unmounts the autocomplete. Call this after any externally controlled closing animation finishes.
filter((item: ItemValue, query: string, itemToString?: ((item: ItemValue) => string)) => boolean) | null-AutocompleteFilter function used to match items against the input query.
filteredItemsany[] | Group<any>[] | ItemValue[] | Group<ItemValue>[]-Filtered items to display in the list. When provided, the list uses these items instead of filtering the items prop internally. When items is also provided, this array must preserve its flat or grouped structure. Nullish entries are not supported, as in items. Use when you want to control filtering logic externally with the useFilter() hook.
formstring-Identifies the form that owns the internal input. Useful when the autocomplete is rendered outside the form.
gridbooleanfalseWhether list items are presented in a grid layout. When enabled, arrow keys navigate across rows and columns inferred from DOM rows.
inlinebooleanfalseWhether the list is rendered inline without using the component's own popup. Specify open unconditionally in conjunction with this prop so the list is considered visible: <Autocomplete.Root inline open>
itemToStringValue((itemValue: ItemValue) => string)-When the item values are objects (<Autocomplete.Item value={object}>), this function converts the object value to a string representation for both display in the input and form submission. If the shape of the object is { value, label }, the label will be used automatically without needing to specify this prop.
items({ items: any[] })[] | ItemValue[]-The items to be displayed in the list. Can be either a flat array of items or an array of groups with items. Nullish entries are not supported: remove them from the data before passing it.
limitnumber-1The maximum number of items to display in the list.
localeIntl.LocalesArgument-The locale to use for string comparison. Defaults to the user's runtime locale.
loopFocusbooleantrueWhether to loop keyboard focus back to the input when the end of the list is reached while using the arrow keys. The first item can then be reached by pressing ArrowDown again from the input, or the last item can be reached by pressing ArrowUp from the input. The input is always included in the focus loop per [ARIA Authoring Practices](https://www.w3.org/WAI/ARIA/apg/patterns/combobox/). When disabled, focus does not move when on the last element and the user presses ArrowDown, or when on the first element and the user presses ArrowUp.
modalbooleanfalseDetermines if the popup enters a modal state when open. true: user interaction is limited to the popup: document page scroll is locked and pointer interactions on outside elements are disabled.false: user interaction with the rest of the document is allowed. On touch devices, a true modal blocks outside taps but leaves the page scrollable unless the popup spans nearly the full viewport width, matching native iOS behavior.
mode'list' | 'both' | 'inline' | 'none''list'Controls how the autocomplete behaves with respect to list filtering and inline autocompletion. list (default): items are dynamically filtered based on the input value. The input value does not change based on the active item.both: items are dynamically filtered based on the input value, which will temporarily change based on the active item (inline autocompletion).inline: items are static (not filtered), and the input value will temporarily change based on the active item (inline autocompletion).none: items are static (not filtered), and the input value will not change based on the active item.
onItemHighlighted((highlightedValue: ItemValue | undefined, eventDetails: Autocomplete.Root.HighlightEventDetails) => void)-Callback fired when an item is highlighted or unhighlighted. Receives the highlighted item value (or undefined if no item is highlighted) and event details with a reason property describing why the highlight changed. The reason can be: 'keyboard': the highlight changed due to keyboard navigation.'pointer': the highlight changed due to pointer hovering.'none': the highlight changed programmatically.
onOpenChangeComplete((open: boolean) => void)-Event handler called after any animations complete when the popup is opened or closed.
openOnInputClickbooleanfalseWhether the popup opens when clicking the input.
submitOnItemClickbooleanfalseWhether clicking an item should submit the autocomplete's owning form. By default, clicking an item via a pointer or Enter key does not submit the owning form. Useful when the autocomplete is used as a single-field form search input.
virtualizedbooleanfalseWhether the items are being externally virtualized.
disabledbooleanfalseWhether the component should ignore user interaction.
readOnlybooleanfalseWhether the user should be unable to choose a different option from the popup.
requiredbooleanfalseWhether the user must choose a value before submitting a form.
inputRefReact.Ref<HTMLInputElement>-A ref to the hidden input element.
idstring-The id of the component.
childrenReact.ReactNode--