Search

Used to auto complete search results.

<Search id="unique-id" onChange={console.log} placeholder="Placeholder" />

Props

PropTypeDefaultDescription
valueanyThe value of the autocomplete. If the value is an empty array, the placeholder will not show. The value must have reference equality with the option in order to be selected. You can customize the equality behavior with the isOptionEqualToValue prop.
autoFocusboolfalseIf true, the input element is focused during the first mount.
errorboolfalseIf true, the label will be displayed in an error state.
helperTextstringThe helper text content.
labelstringThe label content.
placeholderstringThe short hint displayed in the input before the user enters a value.
inputDataTestIdstringdata-testid given to the input element
optionsarrayArray of options.
renderInputfuncRender the input.Signature:function(params: object) => ReactNode
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'| boolfalseControl 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.
componentsPropsobject{}The props used for each slot inside.
defaultValueanyprops.multiple ? [] : nullThe default 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 component is disabled.
disabledItemsFocusableboolfalseIf true, will allow focus on disabled items.
disableListWrapboolfalseIf true, the list box in the popup will not wrap focus.
disablePortalboolfalseIf true, the Popper content will be under the DOM hierarchy of the parent component.
filterOptionsfuncA filter function that determines the options that are eligible.Signature:function(options: Array<T>, state: object) => Array<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) => ReactNodemore: The number of truncated tags.
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
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) => stringoptions: 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 an id it will fall back to a randomly generated one.
includeInputInListboolfalseIf true, the highlight can move to the input.
inputValuestringThe input value.
isOptionEqualToValuefuncUsed 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.Signature:function(option: T, value: T) => booleanoption: The option to test.value: The value to test against.
limitTagsinteger-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. This shows the loadingText in place of suggestions (only if there are no suggestions to show, e.g. options are empty).
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: React.SyntheticEvent, value: T | Array<T>, reason: string, details?: string) => voidevent: The event source of the callback.value: The new value of the component.reason: One of "createOption", "selectOption", "removeOption", "blur" or "clear".
onClosefuncCallback fired when the popup requests to be closed. Use in controlled mode (see open).Signature:function(event: React.SyntheticEvent, reason: string) => voidevent: The event source of the callback.reason: Can be: "toggleInput", "escape", "selectOption", "removeOption", "blur".
onHighlightChangefuncCallback fired when the highlight option changes.Signature:function(event: React.SyntheticEvent, option: T, reason: string) => voidevent: 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: React.SyntheticEvent, value: string, reason: string) => voidevent: 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: React.SyntheticEvent) => voidevent: The event source of the callback.
openboolfalseIf true, the component is shown.
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.
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(params: AutocompleteRenderGroupParams) => ReactNodeparams: The group to render.
renderOptionfuncRender the option, use getOptionLabel by default.Signature:function(props: object, option: T, state: object) => ReactNodeprops: 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: Array<T>, getTagProps: function) => ReactNodevalue: 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'small'| 'medium'| string'medium'The size of the component.
sxArray<func| object>| func| objectThe system prop that allows defining system overrides as well as additional CSS styles. See the `sx` page for more details.

See MUI autocomplete for additional documentation.

The ref is forwarded to the root element.

Any other props supplied will be provided to the root element (native element).

// To supply the option type for TypeScript
<Search<Identity> options={options} ... />

Examples

() => {
const [loading, setLoading] = React.useState(false);
const [searchText, setSearchText] = React.useState('');
const debouncedText = useDebouncedValue(searchText);
const [options, setOptions] = React.useState(data.searchData);
const [values, setValues] = React.useState([]);
React.useEffect(() => {
if (debouncedText?.trim().length > 0) {
setLoading(true);
const timeoutId = setTimeout(() => {
setLoading(false);
setOptions(
data.searchData.filter((option) =>
new RegExp(searchText, 'ig').test(
`${option.firstName} ${option.lastName}`,
),
),
);
}, 500 + Math.floor(Math.random() * 2000));
return () => {
clearTimeout(timeoutId);
setLoading(false);
};
} else {
setOptions(data.searchData);
}
}, [debouncedText]);
return (
<Search
helperText="Enter a name to search"
id="people-search"
noOptionsText="We couldn't find anyone who matched your search"
loading={loading}
onChange={(event, option) => setValues(option)}
onInputChange={(e) => setSearchText(e.target.value)}
options={options}
multiple={true}
value={values}
placeholder="Search"
renderOption={(props, option) => (
<SearchOptionCard
{...props}
disabled={option.disabled}
disabledText={option.disabledText}
avatarUrl={option.avatar}
showAvatar
title={`${option.firstName} ${option.lastName}`}
subTitle={option.subTitle}
type={option.type}
/>
)}
/>
);
};
() => {
const [loading, setLoading] = React.useState(false);
const [searchText, setSearchText] = React.useState('');
const debouncedText = useDebouncedValue(searchText);
const [options, setOptions] = React.useState([]);
React.useEffect(() => {
if (searchText) {
setLoading(true);
const timeoutId = setTimeout(() => {
setLoading(false);
setOptions(
data.searchData.filter((option) =>
new RegExp(searchText, 'ig').test(
`${option.firstName} ${option.lastName}`,
),
),
);
}, 500 + Math.floor(Math.random() * 2000));
return () => {
clearTimeout(timeoutId);
setLoading(false);
};
} else {
setOptions([]);
}
}, [debouncedText]);
return (
<Search
helperText="Enter a name to search"
id="people-single-search"
multiple={false}
noOptionsText="We couldn't find anyone who matched your search"
loading={loading}
onChange={console.log}
onInputChange={(e) => setSearchText(e.target.value)}
options={options}
defaultValue={data.searchData[1]}
placeholder="Search"
variant="large"
type="single"
renderOption={(props, option) => (
<SearchOptionCard
{...props}
avatarUrl={option.avatar}
showAvatar
title={`${option.firstName} ${option.lastName}`}
subTitle={option.subTitle}
type={option.type}
/>
)}
/>
);
};

Page Filter

() => {
const [filteredOptions, setFilteredOptions] = React.useState(data.searchData);
const [searchText, setSearchText] = React.useState('');
React.useEffect(
() =>
setFilteredOptions(
data.searchData.filter((option) =>
new RegExp(searchText, 'ig').test(
`${option.firstName} ${option.lastName}`,
),
),
),
[searchText],
);
return (
<>
<Search
id="unique-id"
onInputChange={(e) => setSearchText(e.target.value)}
placeholder="Search"
size="small"
/>
{filteredOptions.length === 0 && (
<div
style={{
padding: 30,
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
}}
>
No results
</div>
)}
{filteredOptions.map((option) => (
<SearchOptionCard
key={option.id}
title={`${option.firstName} ${option.lastName}`}
/>
))}
</>
);
};
() => {
const [open, setOpen] = React.useState(false);
const [loading, setLoading] = React.useState(false);
const [searchText, setSearchText] = React.useState('');
const debouncedText = useDebouncedValue(searchText);
const [options, setOptions] = React.useState([]);
React.useEffect(() => {
if (searchText) {
setLoading(true);
const timeoutId = setTimeout(() => {
setLoading(false);
setOptions(
data.searchData.filter((option) =>
new RegExp(searchText, 'ig').test(
`${option.firstName} ${option.lastName}`,
),
),
);
}, 500 + Math.floor(Math.random() * 2000));
return () => {
clearTimeout(timeoutId);
setLoading(false);
};
} else {
setOptions([]);
}
}, [debouncedText]);
return (
<>
<Button color="primary" onClick={() => setOpen(true)}>
Open simple Modal
</Button>
<Modal
actions={
<>
<Button onClick={() => setOpen(false)} color="secondary">
Cancel
</Button>
</>
}
id="collapse-Modal"
onClose={() => setOpen(false)}
open={open}
title="Simple Modal"
>
<Search
helperText="Enter a name to search"
id="people-single-search-pop"
noOptionsText="We couldn't find anyone who matched your search"
loading={loading}
onChange={console.log}
onInputChange={(e) => setSearchText(e.target.value)}
options={options}
placeholder="Search"
multiple={false}
renderOption={(props, option) => (
<SearchOptionCard
{...props}
avatarUrl={option.avatar}
showAvatar
title={`${option.firstName} ${option.lastName}`}
subTitle={option.subTitle}
type={option.type}
/>
)}
/>
</Modal>
</>
);
};