Introducing PrimeReact v11-alpha 🎉Discover Now

AutoComplete

AutoComplete is an input component that provides real-time suggestions when being typed.

Type react to see suggestions
preview

Installation#

npx shadcn@latest add @primereact/autocomplete

Usage#

import { AutoComplete, AutoCompleteInput, AutoCompleteList, AutoCompleteOption, AutoCompletePopup, AutoCompletePortal, AutoCompletePositioner } from '@/components/ui/autocomplete';
<AutoComplete options={items} optionLabel="label" onComplete={search}>
    <AutoCompleteInput placeholder="Search..." />
    <AutoCompletePortal>
        <AutoCompletePositioner>
            <AutoCompletePopup>
                <AutoCompleteList>
                    {items.map((item, index) => (
                        <AutoCompleteOption key={index} index={index}>
                            {item.label}
                        </AutoCompleteOption>
                    ))}
                </AutoCompleteList>
            </AutoCompletePopup>
        </AutoCompletePositioner>
    </AutoCompletePortal>
</AutoComplete>

Examples#

Basic#

An input that filters and suggests options as the user types.

basic-demo

Sizes#

Use the size property to change the size of an autocomplete.

sizes-demo

Filled#

Specify the variant="filled" property to display the component with a higher visual emphasis than the default outlined style.

filled-demo

Disabled#

Use the disabled property to disable an autocomplete.

disabled-demo

Invalid#

Specify the invalid property to display the component with a red border for validation errors.

invalid-demo

Trigger#

Use AutoComplete.Trigger inside the root to display a trigger toggle button that opens the suggestion list.

trigger-demo

Chips#

Use AutoComplete.Input with AutoComplete.Value inside AutoComplete.Trigger to compose a chip-based tag input. Selected items are displayed as removable chips inside the value area while the input remains editable for searching.

chip-demo

Clear#

Use AutoComplete.Clear inside the root to display a clear button that resets the input value.

clear-icon-demo

Custom Option#

Customize option content using a render function inside AutoComplete.Option that receives the option instance including the selected state.

option-demo

Force Selection#

Use the forceSelection property to validate manual input against the suggestions list. If no match is found, the input value is cleared to ensure the value is always from the suggestion list.

forceselection-demo

Group#

Simple#

Options can be grouped using the optionGroupLabel and optionGroupChildren properties.

group-demo

Custom#

Customize group headers with custom rendering by accessing the list instance inside AutoComplete.List.

custom-group-demo

Arrow#

Use AutoComplete.Arrow inside the popup to display an arrow pointing to the trigger element. Set sideOffset on AutoComplete.Positioner for spacing.

arrow-demo

Fluid#

Use the fluid property to make the autocomplete take up the full width of its container.

fluid-demo

Loading#

Compose a loading indicator alongside the autocomplete input using InputGroup to display an async loading state.

loading-demo

Focus Behavior#

Customize keyboard and mouse focus behavior with autoOptionFocus, selectOnFocus, and focusOnHover props.

focus-behavior-demo

API#

Sub-Components#

See Primitive API for AutoCompleteRoot, AutoCompleteInput, AutoCompleteTrigger, AutoCompleteList, AutoCompleteOption, and other component documentation.

Hooks#

See Headless API for useAutoComplete hook documentation.

Accessibility#

See AutoComplete Primitive for WAI-ARIA compliance details and keyboard support.