useAutoComplete
Hook that manages autocomplete state, search callbacks, and popup positioning for combobox inputs.
Usage#
import { useAutoComplete } from '@primereact/headless/autocomplete';
const autocomplete = useAutoComplete({ options: filteredItems, onComplete: search });
const { rootProps, inputProps, triggerProps, listProps, positionerProps, popupProps, arrowProps, state, listbox } = autocomplete;
return (
<div {...rootProps}>
<input {...inputProps} />
<button {...triggerProps}></button>
<div {...positionerProps}>
<div {...popupProps}>
<ul {...listProps}>...</ul>
</div>
<div {...arrowProps} />
</div>
</div>
);useAutoComplete composes useListbox and usePopover internally, managing search callbacks, input value synchronization, and popup lifecycle — see Primitive for a component-based API.
Features#
rootProps,inputProps,triggerProps,listProps,positionerProps,popupProps,arrowProps,indicatorProps, andclearPropsreturn spread-ready prop objects- Composes
useListboxinternally — the returnedlistboxinstance provides option iteration, selection state, anduseListboxOptionsupport onCompletecallback fires with the current query for custom filtering logicshow(),hide(),toggle(), andfocus()for imperative popup and input controlgetSelectedOptionLabel()andhasValue()for reading the current selectionsetRendered()controls whether the overlay has been rendered at least once, useful for lazy rendering strategies
Behavior#
Default Value#
Set defaultValue for uncontrolled selection.
const autocomplete = useAutoComplete({ options: items, onComplete: search, defaultValue: 'React' });Default Input Value#
Set defaultInputValue to provide an initial input text in uncontrolled mode. This is independent of defaultValue — it controls only the text shown in the input field.
const autocomplete = useAutoComplete({ options: items, onComplete: search, defaultInputValue: 'Rea' });Controlled#
Pass value and onValueChange for controlled selection state.
const [selected, setSelected] = React.useState(null);
const autocomplete = useAutoComplete({
options: items,
onComplete: search,
value: selected,
onValueChange: (e) => setSelected(e.value)
});Controlled Input Value#
Use inputValue and onInputValueChange to control the text input independently from the selection. This is useful for custom filtering, clearing the input programmatically, or syncing the query with external state.
const [query, setQuery] = React.useState('');
const autocomplete = useAutoComplete({
options: items,
onComplete: search,
inputValue: query,
onInputValueChange: (e) => setQuery(e.query)
});Option Fields#
Use optionLabel, optionValue, optionDisabled, and optionKey to map fields when options are objects.
const autocomplete = useAutoComplete({
optionLabel: 'name',
optionValue: 'code',
optionDisabled: 'inactive',
optionKey: 'code'
});Grouped Options#
Set optionGroupLabel and optionGroupChildren to support grouped option structures.
const autocomplete = useAutoComplete({
optionGroupLabel: 'label',
optionGroupChildren: 'items'
});Search Delay#
Set delay to debounce the onComplete callback in milliseconds.
const autocomplete = useAutoComplete({ delay: 300 });Minimum Length#
Set minLength to require a minimum number of characters before triggering the search.
const autocomplete = useAutoComplete({ minLength: 2 });Force Selection#
Enable forceSelection to clear the input on blur if the typed value does not match any option.
const autocomplete = useAutoComplete({ forceSelection: true });Disabled#
Set disabled to prevent all interaction.
const autocomplete = useAutoComplete({ disabled: true });Focus Behavior#
Use autoOptionFocus to automatically focus the first option when the popup opens, selectOnFocus to select while navigating, and focusOnHover to move focus with mouse hover.
const autocomplete = useAutoComplete({ autoOptionFocus: true, selectOnFocus: true });Popup Control#
Use open and onOpenChange for controlled popup state, or defaultOpen for uncontrolled initial state.
const [open, setOpen] = React.useState(false);
const autocomplete = useAutoComplete({
open,
onOpenChange: (e) => setOpen(e.value)
});Popup Behavior#
Set closeOnEscape to dismiss the overlay with the Escape key, autoFocus to move focus into the overlay when it opens, and trapped to keep focus within the overlay.
const autocomplete = useAutoComplete({ closeOnEscape: true, autoFocus: true, trapped: true });Meta Key Selection#
Enable metaKeySelection to require holding the meta key (Cmd/Ctrl) to select or deselect an option.
const autocomplete = useAutoComplete({ metaKeySelection: true });Locale#
Set locale to control the locale used for string comparison during force selection matching.
const autocomplete = useAutoComplete({ locale: 'tr-TR', forceSelection: true });Form Integration#
Use name to set the input's name attribute for form submission, and tabIndex to control the tab order.
const autocomplete = useAutoComplete({ name: 'city', tabIndex: 0 });Custom Styling with Data Attributes#
Every prop object includes data-scope="autocomplete" and a data-part that identifies the element. State-driven attributes appear or disappear automatically.
| Part | State Attributes |
|---|---|
trigger | data-positioner-open |
popup | data-open |
indicator | data-open, data-closed |
Options rendered via useListboxOption use data-scope="listbox" with the following state attributes.
| Part | State Attributes |
|---|---|
option | data-selected, data-unselected, data-focused, data-disabled |
optionindicator | data-selected, data-unselected |
/* Input focus ring */
[data-scope='autocomplete'][data-part='input']:focus {
outline: 2px solid var(--p-primary-color);
outline-offset: -2px;
}
/* Open/close indicator rotation */
[data-scope='autocomplete'][data-part='indicator'][data-open] {
transform: rotate(180deg);
}
/* Popup entrance */
[data-scope='autocomplete'][data-part='popup'][data-open] {
border: 1px solid var(--p-content-border-color);
border-radius: 6px;
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}
/* Option states */
[data-scope='listbox'][data-part='option'][data-focused] {
background: var(--p-surface-100);
}
[data-scope='listbox'][data-part='option'][data-selected] {
background: var(--p-primary-color);
color: var(--p-primary-contrast-color);
}
[data-scope='listbox'][data-part='option'][data-disabled] {
opacity: 0.5;
pointer-events: none;
}API#
useAutoComplete#
| Name | Type | Default |
|---|---|---|
value | unknown | — |
| The current selected value of the autocomplete. | ||
defaultValue | unknown | — |
| The default selected value when used in uncontrolled mode. | ||
inputValue | string | — |
| The current input text value (controlled). | ||
defaultInputValue | string | — |
| The default input text value when used in uncontrolled mode. | ||
open | boolean | — |
| Controlled open state of the input tags overlay. | ||
defaultOpen | boolean | — |
| Default open state for uncontrolled mode. | ||
options | unknown[] | — |
| An array of options to display. | ||
optionKey | string | — |
| Unique key for each option. | ||
optionLabel | string | — |
| Label field for each option. | ||
optionValue | string | — |
| Value field for each option. | ||
optionDisabled | string | — |
| Field to check if an option is disabled. | ||
optionGroupLabel | string | — |
| Label field for option groups. | ||
optionGroupChildren | string | — |
| Field that contains the children options in a group. | ||
disabled | boolean | — |
| When present, it specifies that the component should be disabled. | ||
locale | string | — |
| The locale to use for localization. | ||
multiple | boolean | — |
| When present, allows selecting multiple options. | ||
metaKeySelection | boolean | — |
| When enabled, requires holding the meta key to select/deselect. | ||
autoOptionFocus | boolean | — |
| When enabled, the focused option is automatically highlighted. | ||
selectOnFocus | boolean | — |
| When enabled, the focused option is automatically selected. | ||
focusOnHover | boolean | — |
| When enabled, the focused option changes on hover. | ||
minLength | number | — |
| Minimum number of characters to initiate a search. | ||
delay | number | — |
| Delay between keystrokes to wait before sending a query. | ||
forceSelection | boolean | — |
| When present, it specifies that an input field must be filled out before submitting the form. | ||
tabIndex | number | — |
| Index of the element in tabbing order. | ||
invalid | boolean | — |
| When present, it specifies that the component is in an invalid state. | ||
name | string | — |
| Name of the input element. | ||
required | boolean | — |
| When present, it specifies that an input field is required. | ||
closeOnEscape | boolean | — |
| When enabled, the overlay closes when Escape key is pressed. | ||
autoFocus | boolean | — |
| When enabled, the overlay receives focus automatically when opened. | ||
trapped | boolean | — |
| When enabled, focus is trapped within the overlay. | ||
onValueChange | (event: useAutoCompleteValueChangeEvent) => void | — |
| Callback to invoke when the selected value changes. | ||
onInputValueChange | (event: useAutoCompleteInputValueChangeEvent) => void | — |
| Callback when the input value changes. | ||
onComplete | (event: useAutoCompleteCompleteEvent) => void | — |
| Callback to invoke to search for suggestions. | ||
onOpenChange | (event: useAutoCompleteOpenChangeEvent) => void | — |
| Callback to invoke when the open state changes. | ||
Accessibility#
See AutoComplete Primitive for WAI-ARIA compliance details and keyboard support.