Skip to content
+

Autocomplete

The Autocomplete component is a text input enhanced by a panel of suggested options.

useAutocomplete API

Import

import useAutocomplete from '@mui';
// or
import { useAutocomplete } from '@mui/base';
Learn about the difference by reading this guide on minimizing bundle size.

Parameters

optionsRequired

Array of options.

Type:

ReadonlyArray<T>
autoComplete

If true, the portion of the selected suggestion that has not been typed by the user, 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.

Type:

boolean

Default:

false
autoHighlight

If true, the first option is automatically highlighted.

Type:

boolean

Default:

false
autoSelect

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 freeSolo mode, the typed value will be the input value if the Autocomplete loses focus without highlighting an option.

Type:

boolean

Default:

false
blurOnSelect

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.

Type:

'touch' | 'mouse' | true | false

Default:

false
clearOnBlur

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

Type:

boolean

Default:

!props.freeSolo
clearOnEscape

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

Type:

boolean

Default:

false
componentName

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

Type:

string
defaultValue

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

Type:

AutocompleteValue<T, Multiple, DisableClearable, FreeSolo>

Default:

props.multiple ? [] : null
disableClearable

If true, the input can't be cleared.

Type:

DisableClearable

Default:

false
disableCloseOnSelect

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

Type:

boolean

Default:

false
disabled

If true, the component is disabled.

Type:

boolean

Default:

false
disabledItemsFocusable

If true, will allow focus on disabled items.

Type:

boolean

Default:

false
disableListWrap

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

Type:

boolean

Default:

false
filterOptions

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

Type:

(options: T[], state: FilterOptionsState<T>) => T[]

Default:

createFilterOptions()
filterSelectedOptions

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

Type:

boolean

Default:

false
freeSolo

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

Type:

FreeSolo

Default:

false
getOptionDisabled

Used to determine the disabled state for a given option.

Type:

(option: T) => boolean
getOptionLabel

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.

Type:

(option: T | AutocompleteFreeSoloValueMapping<FreeSolo>) => string

Default:

(option) => option.label ?? option
groupBy

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.

Type:

(option: T) => string
handleHomeEndKeys

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.

Type:

boolean

Default:

!props.freeSolo
id

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.

Type:

string
includeInputInList

If true, the highlight can move to the input.

Type:

boolean

Default:

false
inputValue

The input value.

Type:

string
isOptionEqualToValue

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.

Type:

(option: T, value: T) => boolean
multiple

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

Type:

Multiple

Default:

false
onChange

Callback fired when the value changes.

Type:

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

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

Type:

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

Callback fired when the highlight option changes.

Type:

(event: React.SyntheticEvent, option: T | null, reason: AutocompleteHighlightChangeReason) => void
onInputChange

Callback fired when the input value changes.

Type:

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

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

Type:

(event: React.SyntheticEvent) => void
open

If true, the component is shown.

Type:

boolean
openOnFocus

If true, the popup will open on input focus.

Type:

boolean

Default:

false
readOnly

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

Type:

boolean

Default:

false
selectOnFocus

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

Type:

boolean

Default:

!props.freeSolo
unstable_classNamePrefix

Type:

string

Default:

'Mui'
unstable_isActiveElementInListbox

Type:

(listbox: React.RefObject<HTMLElement>) => boolean
value

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.

Type:

AutocompleteValue<T, Multiple, DisableClearable, FreeSolo>

Return value

anchorEl

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

Type:

null | HTMLElement
dirty

If true, the component input has some values.

Type:

boolean
expanded

If true, the listbox is being displayed.

Type:

boolean
focused

If true, the component is focused.

Type:

boolean
focusedTag

Index of the focused tag for the component.

Type:

number
getClearProps

Resolver for the clear button element's props.

Type:

() => React.HTMLAttributes<HTMLButtonElement>
getInputLabelProps

Resolver for the input label element's props.

Type:

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

Resolver for the input element's props.

Type:

() => React.InputHTMLAttributes<HTMLInputElement>
getListboxProps

Resolver for the listbox component's props.

Type:

() => React.HTMLAttributes<HTMLUListElement>
getOptionProps

Resolver for the rendered option element's props.

Type:

(renderedOption: UseAutocompleteRenderedOption<T>) => React.HTMLAttributes<HTMLLIElement>
getPopupIndicatorProps

Resolver for the popup icon's props.

Type:

() => React.HTMLAttributes<HTMLButtonElement>
getRootProps

Resolver for the root slot's props.

Type:

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

A tag props getter.

Type:

AutocompleteGetTagProps
groupedOptions

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

Type:

T[] | Array<AutocompleteGroupedOption<T>>
id

Id for the Autocomplete.

Type:

string
inputValue

The input value.

Type:

string
popupOpen

If true, the popup is open on the component.

Type:

boolean
setAnchorEl

Setter for the component anchorEl.

Type:

() => void
value

The value of the autocomplete.

Type:

AutocompleteValue<T, Multiple, DisableClearable, FreeSolo>