Skip to content
+

Autocomplete

An autocomplete component is a text input enhanced by a panel of suggested options.

useAutocomplete API

Import

import { useAutocomplete } from '@mui/base/useAutocomplete';
// or
import { useAutocomplete } from '@mui/base';

Learn about the difference by reading this guide on minimizing bundle size.

Parameters

NameTypeDefaultDescription
options*ReadonlyArray<Value>-

Array of options.

autoCompletebooleanfalse

If true, the portion of the selected suggestion that the user hasn't typed, known as the completion string, appears inline after the input cursor in the textbox. The inline completion string is visually highlighted and has a selected state.

autoHighlightbooleanfalse

If true, the first option is automatically highlighted.

autoSelectbooleanfalse

If true, the selected option becomes the value of the input when the Autocomplete loses focus unless the user chooses a different option or changes the character string in the input.
When using the freeSolo mode, the typed value will be the input value if the Autocomplete loses focus without highlighting an option.

blurOnSelect'touch' | 'mouse' | true | falsefalse

Control if the input should be blurred when an option is selected:

  • false the input is not blurred.
  • true the input is always blurred.
  • touch the input is blurred after a touch event.
  • mouse the input is blurred after a mouse event.
clearOnBlurboolean!props.freeSolo

If true, the input's text is cleared on blur if no value is selected.
Set it to true if you want to help the user enter a new value. Set it to false if you want to help the user resume their search.

clearOnEscapebooleanfalse

If true, clear all values when the user presses escape and the popup is closed.

componentNamestring-

The component name that is using this hook. Used for warnings.

defaultValueAutocompleteValue<Value, Multiple, DisableClearable, FreeSolo>props.multiple ? [] : null

The default value. Use when the component is not controlled.

disableClearableDisableClearablefalse

If true, the input can't be cleared.

disableCloseOnSelectbooleanfalse

If true, the popup won't close when a value is selected.

disabledbooleanfalse

If true, the component is disabled.

disabledItemsFocusablebooleanfalse

If true, will allow focus on disabled items.

disableListWrapbooleanfalse

If true, the list box in the popup will not wrap focus.

filterOptions(options: Value[], state: FilterOptionsState<Value>) => Value[]createFilterOptions()

A function that determines the filtered options to be rendered on search.

filterSelectedOptionsbooleanfalse

If true, hide the selected options from the list box.

freeSoloFreeSolofalse

If true, the Autocomplete is free solo, meaning that the user input is not bound to provided options.

getOptionDisabled(option: Value) => boolean-

Used to determine the disabled state for a given option.

getOptionKey(option: Value | AutocompleteFreeSoloValueMapping<FreeSolo>) => string | number-

Used to determine the key for a given option. This can be useful when the labels of options are not unique (since labels are used as keys by default).

getOptionLabel(option: Value | AutocompleteFreeSoloValueMapping<FreeSolo>) => string(option) => option.label ?? option

Used to determine the string value for a given option. It's used to fill the input (and the list box options if renderOption is not provided).
If used in free solo mode, it must accept both the type of the options and a string.

groupBy(option: Value) => string-

If provided, the options will be grouped under the returned string. The groupBy value is also used as the text for group headings when renderGroup is not provided.

handleHomeEndKeysboolean!props.freeSolo

If true, the component handles the "Home" and "End" keys when the popup is open. It should move focus to the first option and last option, respectively.

idstring-

This prop is used to help implement the accessibility logic. If you don't provide an id it will fall back to a randomly generated one.

includeInputInListbooleanfalse

If true, the highlight can move to the input.

inputValuestring-

The input value.

isOptionEqualToValue(option: Value, value: Value) => boolean-

Used to determine if the option represents the given value. Uses strict equality by default. ⚠️ Both arguments need to be handled, an option can only match with one value.

multipleMultiplefalse

If true, value must be an array and the menu will support multiple selections.

onChange(event: React.SyntheticEvent, value: AutocompleteValue<Value, Multiple, DisableClearable, FreeSolo>, reason: AutocompleteChangeReason, details?: AutocompleteChangeDetails<Value>) => void-

Callback fired when the value changes.

onClose(event: React.SyntheticEvent, reason: AutocompleteCloseReason) => void-

Callback fired when the popup requests to be closed. Use in controlled mode (see open).

onHighlightChange(event: React.SyntheticEvent, option: Value | null, reason: AutocompleteHighlightChangeReason) => void-

Callback fired when the highlight option changes.

onInputChange(event: React.SyntheticEvent, value: string, reason: AutocompleteInputChangeReason) => void-

Callback fired when the input value changes.

onOpen(event: React.SyntheticEvent) => void-

Callback fired when the popup requests to be opened. Use in controlled mode (see open).

openboolean-

If true, the component is shown.

openOnFocusbooleanfalse

If true, the popup will open on input focus.

readOnlybooleanfalse

If true, the component becomes readonly. It is also supported for multiple tags where the tag cannot be deleted.

selectOnFocusboolean!props.freeSolo

If true, the input's text is selected on focus. It helps the user clear the selected value.

unstable_classNamePrefixstring'Mui'
unstable_isActiveElementInListbox(listbox: React.RefObject<HTMLElement | null>) => boolean-
valueAutocompleteValue<Value, Multiple, DisableClearable, FreeSolo>-

The value of the autocomplete.
The value must have reference equality with the option in order to be selected. You can customize the equality behavior with the isOptionEqualToValue prop.

Return value

NameTypeDescription
anchorElnull | HTMLElement

An HTML element that is used to set the position of the component.

dirtyboolean

If true, the component input has some values.

expandedboolean

If true, the listbox is being displayed.

focusedboolean

If true, the component is focused.

focusedTagnumber

Index of the focused tag for the component.

getClearProps() => React.HTMLAttributes<HTMLButtonElement>

Resolver for the clear button element's props.

getInputLabelProps() => Omit<React.HTMLAttributes<HTMLLabelElement>, 'color'>

Resolver for the input label element's props.

getInputProps() => React.InputHTMLAttributes<HTMLInputElement> & { ref: React.Ref<HTMLInputElement> }

Resolver for the input element's props.

getListboxProps() => React.HTMLAttributes<HTMLUListElement>

Resolver for the listbox component's props.

getOptionProps(renderedOption: UseAutocompleteRenderedOption<Value>) => React.HTMLAttributes<HTMLLIElement> & { key: any }

Resolver for the rendered option element's props.

getPopupIndicatorProps() => React.HTMLAttributes<HTMLButtonElement>

Resolver for the popup icon's props.

getRootProps(externalProps?: any) => React.HTMLAttributes<HTMLDivElement>

Resolver for the root slot's props.

getTagPropsAutocompleteGetTagProps

A tag props getter.

groupedOptionsValue[] | Array<AutocompleteGroupedOption<Value>>

The options to render. It's either Value[] or AutocompleteGroupedOption<Value>[] if the groupBy prop is provided.

idstring

Id for the Autocomplete.

inputValuestring

The input value.

popupOpenboolean

If true, the popup is open on the component.

setAnchorEl() => void

Setter for the component anchorEl.

valueAutocompleteValue<Value, Multiple, DisableClearable, FreeSolo>

The value of the autocomplete.