docs/documentation/components/autocomplete.mdx
The Autocomplete components renders a filterable list of options in a Popover — it does not render the text input. If you need a combination with a text input and trigger button, take a look at the Combobox component.
The autocomplete component combines a set of external libraries:
Downshift for autocompletereact-tiny-virtual-list for performant list renderingfuzzaldrin-plus for fuzzy filtering<Autocomplete
title="Fruits"
onChange={changedItem => console.log(changedItem)}
items={['Apple', 'Apricot', 'Banana', 'Cherry', 'Cucumber']}
>
{props => {
const { getInputProps, getRef, inputValue } = props
return (
<TextInput
placeholder="Fruits"
value={inputValue}
ref={getRef}
{...getInputProps()}
/>
)
}}
</Autocomplete>
Get the openMenu prop to manually open the menu on certain events such as onFocus.
<Autocomplete
title="Fruits"
onChange={changedItem => console.log(changedItem)}
items={['Apple', 'Apricot', 'Banana', 'Cherry', 'Cucumber']}
>
{props => {
const { getInputProps, getRef, inputValue, openMenu } = props
return (
<TextInput
placeholder="Open on focus"
value={inputValue}
ref={getRef}
{...getInputProps({
onFocus: () => {
openMenu()
}
})}
/>
)
}}
</Autocomplete>
<Autocomplete
title="Fruits"
onChange={changedItem => console.log(changedItem)}
items={['Apple', 'Apricot', 'Banana', 'Cherry', 'Cucumber']}
>
{({
getInputProps,
getToggleButtonProps,
getRef,
inputValue,
toggleMenu
}) => (
<Pane ref={getRef} display="flex">
<TextInput
placeholder="Trigger with button"
value={inputValue}
{...getInputProps()}
/>
<Button onClick={toggleMenu} {...getToggleButtonProps()}>
Trigger
</Button>
</Pane>
)}
</Autocomplete>
Full width with flex and options title, onFocus, filtering, and button to trigger the autocomplete. Note that this is very similar to the Combobox component. Consider using that component before using the Autocomplete component directly.
<Autocomplete
title="Custom title"
onChange={changedItem => console.log(changedItem)}
items={['Apple', 'Apricot', 'Banana', 'Cherry', 'Cucumber']}
>
{({
key,
getInputProps,
getToggleButtonProps,
getRef,
inputValue,
openMenu,
toggleMenu
}) => (
<Pane key={key} ref={getRef} display="flex">
<TextInput
flex="1"
placeholder="Many Options!"
value={inputValue}
onFocus={openMenu}
{...getInputProps()}
/>
<Button onClick={toggleMenu} {...getToggleButtonProps()}>
Trigger
</Button>
</Pane>
)}
</Autocomplete>