useAutocomplete API
Demos
For examples and details on the usage of this React hook, visit the demo pages:
Import
import useAutocomplete from '@mui/base/useAutocomplete';
Parameters
Name | Type | Default | Description |
---|---|---|---|
options* | ReadonlyArray<T> | Array of options. | |
autoComplete | boolean | false | 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. |
autoHighlight | boolean | false | If true , the first option is automatically highlighted. |
autoSelect | boolean | false | 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. |
blurOnSelect | 'touch' | 'mouse' | true | false | false | 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. |
clearOnBlur | boolean | !props.freeSolo | 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. |
clearOnEscape | boolean | false | If true , clear all values when the user presses escape and the popup is closed. |
componentName | string | The component name that is using this hook. Used for warnings. | |
defaultValue | AutocompleteValue<T, Multiple, DisableClearable, FreeSolo> | props.multiple ? [] : null | The default value. Use when the component is not controlled. |
disableClearable | DisableClearable | false | If true , the input can't be cleared. |
disableCloseOnSelect | boolean | false | If true , the popup won't close when a value is selected. |
disabled | boolean | false | If true , the component is disabled. |
disabledItemsFocusable | boolean | false | If true , will allow focus on disabled items. |
disableListWrap | boolean | false | If true , the list box in the popup will not wrap focus. |
filterOptions | (options: T[], state: FilterOptionsState<T>) => T[] | A function that determines the filtered options to be rendered on search. | |
filterSelectedOptions | boolean | false | If true , hide the selected options from the list box. |
freeSolo | FreeSolo | false | If true , the Autocomplete is free solo, meaning that the user input is not bound to provided options. |
getOptionDisabled | (option: T) => boolean | Used to determine the disabled state for a given option. | |
getOptionLabel | (option: T | 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: T) => 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. | |
handleHomeEndKeys | boolean | !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. |
id | string | 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. | |
includeInputInList | boolean | false | If true , the highlight can move to the input. |
inputValue | string | The input value. | |
isOptionEqualToValue | (option: T, value: T) => 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. | |
multiple | Multiple | false | If true , value must be an array and the menu will support multiple selections. |
onChange | (event: React.SyntheticEvent, value: AutocompleteValue<T, Multiple, DisableClearable, FreeSolo>, reason: AutocompleteChangeReason, details?: AutocompleteChangeDetails<T>) => 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: T | 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). | |
open | boolean | If true , the component is shown. | |
openOnFocus | boolean | false | If true , the popup will open on input focus. |
readOnly | boolean | false | If true , the component becomes readonly. It is also supported for multiple tags where the tag cannot be deleted. |
selectOnFocus | boolean | !props.freeSolo | If true , the input's text is selected on focus.
It helps the user clear the selected value. |
unstable_classNamePrefix | string | 'Mui' | |
unstable_isActiveElementInListbox | (listbox: React.RefObject<HTMLElement>) => boolean | ||
value | AutocompleteValue<T, 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
Name | Type | Default | Description |
---|