跳转到内容

Autocomplete API

API documentation for the React Autocomplete component. Learn about the available props, and the CSS API.

Import

import Autocomplete from '@material-ui/core/Autocomplete';
// or
import { Autocomplete } from '@material-ui/core';
You can learn about the difference by reading this guide on minimizing bundle size.

Component name

The name MuiAutocomplete can be used when providing default props or style overrides in the theme.

Props

NameTypeDefaultDescription
autoCompleteboolfalseIf 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.
autoHighlightboolfalseIf true, the first option is automatically highlighted.
autoSelectboolfalseIf 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.
blurOnSelect'mouse'
| 'touch'
| bool
falseControl 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.
ChipPropsobjectProps applied to the Chip element.
classesobjectOverride or extend the styles applied to the component. See CSS API below for more details.
clearIconnode<ClearIcon fontSize="small" />The icon to display in place of the default clear icon.
clearOnBlurbool!props.freeSoloIf 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 his search.
clearOnEscapeboolfalseIf true, clear all values when the user presses escape and the popup is closed.
clearTextstring'Clear'Override the default text for the clear icon button.
For localization purposes, you can use the provided translations.
closeTextstring'Close'Override the default text for the close popup icon button.
For localization purposes, you can use the provided translations.
defaultValueanyprops.multiple ? [] : nullThe default input value. Use when the component is not controlled.
disableClearableboolfalseIf true, the input can't be cleared.
disableCloseOnSelectboolfalseIf true, the popup won't close when a value is selected.
disabledboolfalseIf true, the input is disabled.
disabledItemsFocusableboolfalseIf true, will allow focus on disabled items.
disableListWrapboolfalseIf true, the list box in the popup will not wrap focus.
disablePortalboolfalseThe Popper content will be inside the DOM hierarchy of the parent component.
filterOptionsfuncA filter function that determines the options that are eligible.

Signature:
function(options: T[], state: object) => T[]
options: The options to render.
state: The state of the component.
filterSelectedOptionsboolfalseIf true, hide the selected options from the list box.
forcePopupIcon'auto'
| bool
'auto'Force the visibility display of the popup icon.
freeSoloboolfalseIf true, the Autocomplete is free solo, meaning that the user input is not bound to provided options.
fullWidthboolfalseIf true, the input will take up the full width of its container.
getLimitTagsTextfunc(more) => `+${more}`The label to display when the tags are truncated (limitTags).

Signature:
function(more: number) => ReactNode
more: The number of truncated tags.
getOptionDisabledfuncUsed to determine the disabled state for a given option.

Signature:
function(option: T) => boolean
option: The option to test.
getOptionLabelfunc(option) => option.label ?? optionUsed 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).

Signature:
function(option: T) => string
getOptionSelectedfuncUsed to determine if an option is selected, considering the current value(s). Uses strict equality by default. ⚠️ Both arguments need to be handled, an option can only match with one value.

Signature:
function(option: T, value: T) => boolean
option: The option to test.
value: The value to test against.
groupByfuncIf 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.

Signature:
function(options: T) => string
options: The options to group.
handleHomeEndKeysbool!props.freeSoloIf 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.
idstringThis prop is used to help implement the accessibility logic. If you don't provide this prop. It falls back to a randomly generated id.
includeInputInListboolfalseIf true, the highlight can move to the input.
inputValuestringThe input value.
limitTagsnumber-1The maximum number of tags that will be visible when not focused. Set -1 to disable the limit.
ListboxComponentelementType'ul'The component used to render the listbox.
ListboxPropsobjectProps applied to the Listbox element.
loadingboolfalseIf true, the component is in a loading state.
loadingTextnode'Loading…'Text to display when in a loading state.
For localization purposes, you can use the provided translations.
multipleboolfalseIf true, value must be an array and the menu will support multiple selections.
noOptionsTextnode'No options'Text to display when there are no options.
For localization purposes, you can use the provided translations.
onChangefuncCallback fired when the value changes.

Signature:
function(event: object, value: T \| T[], reason: string) => void
event: The event source of the callback.
value: The new value of the component.
reason: One of "create-option", "select-option", "remove-option", "blur" or "clear".
onClosefuncCallback fired when the popup requests to be closed. Use in controlled mode (see open).

Signature:
function(event: object, reason: string) => void
event: The event source of the callback.
reason: Can be: "toggleInput", "escape", "select-option", "remove-option", "blur".
onHighlightChangefuncCallback fired when the highlight option changes.

Signature:
function(event: object, option: T, reason: string) => void
event: The event source of the callback.
option: The highlighted option.
reason: Can be: "keyboard", "auto", "mouse".
onInputChangefuncCallback fired when the input value changes.

Signature:
function(event: object, value: string, reason: string) => void
event: The event source of the callback.
value: The new value of the text input.
reason: Can be: "input" (user input), "reset" (programmatic change), "clear".
onOpenfuncCallback fired when the popup requests to be opened. Use in controlled mode (see open).

Signature:
function(event: object) => void
event: The event source of the callback.
openboolfalseControl the popup` open state.
openOnFocusboolfalseIf true, the popup will open on input focus.
openTextstring'Open'Override the default text for the open popup icon button.
For localization purposes, you can use the provided translations.
options*arrayArray of options.
PaperComponentelementTypePaperThe component used to render the body of the popup.
PopperComponentelementTypePopperThe component used to position the popup.
popupIconnode<ArrowDropDownIcon />The icon to display in place of the default popup icon.
renderGroupfuncRender the group.

Signature:
function(option: any) => ReactNode
option: The group to render.
renderInput*funcRender the input.

Signature:
function(params: object) => ReactNode
renderOptionfuncRender the option, use getOptionLabel by default.

Signature:
function(props: object, option: T, state: object) => ReactNode
props: The props to apply on the li element.
option: The option to render.
state: The state of the component.
renderTagsfuncRender the selected value.

Signature:
function(value: T[], getTagProps: function) => ReactNode
value: The value provided to the component.
getTagProps: A tag props getter.
selectOnFocusbool!props.freeSoloIf true, the input's text is selected on focus. It helps the user clear the selected value.
size'medium'
| 'small'
'medium'The size of the autocomplete.
valueanyThe 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 getOptionSelected prop.

The ref is forwarded to the root element.
Any other props supplied will be provided to the root element (native element).

CSS

Rule nameGlobal classDescription
root.MuiAutocomplete-rootStyles applied to the root element.
fullWidth.MuiAutocomplete-fullWidthStyles applied to the root element if fullWidth={true}.
focused.Mui-focusedPseudo-class applied to the root element if focused.
tag.MuiAutocomplete-tagStyles applied to the tag elements, e.g. the chips.
tagSizeSmall.MuiAutocomplete-tagSizeSmallStyles applied to the tag elements, e.g. the chips if size="small".
hasPopupIcon.MuiAutocomplete-hasPopupIconStyles applied when the popup icon is rendered.
hasClearIcon.MuiAutocomplete-hasClearIconStyles applied when the clear icon is rendered.
inputRoot.MuiAutocomplete-inputRootStyles applied to the Input element.
input.MuiAutocomplete-inputStyles applied to the input element.
inputFocused.MuiAutocomplete-inputFocusedStyles applied to the input element if tag focused.
endAdornment.MuiAutocomplete-endAdornmentStyles applied to the endAdornment element.
clearIndicator.MuiAutocomplete-clearIndicatorStyles applied to the clear indicator.
popupIndicator.MuiAutocomplete-popupIndicatorStyles applied to the popup indicator.
popupIndicatorOpen.MuiAutocomplete-popupIndicatorOpenStyles applied to the popup indicator if the popup is open.
popper.MuiAutocomplete-popperStyles applied to the popper element.
popperDisablePortal.MuiAutocomplete-popperDisablePortalStyles applied to the popper element if disablePortal={true}.
paper.MuiAutocomplete-paperStyles applied to the `Paper` component.
listbox.MuiAutocomplete-listboxStyles applied to the `listbox` component.
loading.MuiAutocomplete-loadingStyles applied to the loading wrapper.
noOptions.MuiAutocomplete-noOptionsStyles applied to the no option wrapper.
option.MuiAutocomplete-optionStyles applied to the option elements.
groupLabel.MuiAutocomplete-groupLabelStyles applied to the group's label elements.
groupUl.MuiAutocomplete-groupUlStyles applied to the group's ul elements.

You can override the style of the component using one of these customization options: If that isn't sufficient, you can check the implementation of the component for more detail.

Demos