TypeaheadSearch

Used to auto complete search results.

<TypeaheadSearch inputLabel="Search" />

Props

PropTypeDefaultDescription
containerStylesobjectstyles to apply to the components container.
getOptionLabel(option) => stringa function that returns the option label.
inputLabelstringlabel for the input
onChange(option) => voidaction to perform when option is selected
onSearch(searchText) => voidaction to perform when search text is changed
optionsarrayarray of options
renderOption(option, state: object) => NodeHow to render the option
adminColorboolfalseIf true, semantic blue color is considered as primary color
disabledboolfalseIf true, the component will be disabled
buttonAriaLabelstringSearcharia-label for the button

The ref is forwarded to the root element.

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

Examples

Basic

() => {
const unfilteredOptions = [
{ firstName: 'Joe', lastName: 'Bob' },
{ firstName: 'Jill', lastName: 'Jackson' },
{ firstName: 'Frank', lastName: 'Sinatra' },
{ firstName: 'Billy', lastName: 'Jean' },
{ firstName: 'Meryl', lastName: 'Streep' },
{ firstName: 'Bob', lastName: 'Saget' },
{ firstName: 'Zoe', lastName: 'Deschanel' },
{ firstName: 'Will', lastName: 'Ferrell' },
{ firstName: 'John', lastName: 'Candy' },
];
const [loading, setLoading] = React.useState(false);
const [searchText, setSearchText] = React.useState('');
const [options, setOptions] = React.useState([]);
React.useEffect(() => {
if (searchText) {
setLoading(true);
setOptions([]);
const timeoutId = setTimeout(() => {
setLoading(false);
setOptions(
unfilteredOptions.filter((option) =>
new RegExp(searchText, 'ig').test(
`${option.firstName} ${option.lastName}`,
),
),
);
}, Math.floor(Math.random() * 2500));
return () => {
clearTimeout(timeoutId);
setLoading(false);
};
}
}, [searchText]);
return (
<TypeaheadSearch
getOptionLabel={(option) => `${option.firstName} ${option.lastName}`}
inputLabel="Search"
loading={loading}
onChange={(option) =>
console.log(`You selected ${option.firstName} ${option.lastName}`)
}
onSearch={(searchText) => setSearchText(searchText)}
options={options}
/>
);
};

Disabled

<TypeaheadSearch inputLabel="Search" disabled />