Autocomplete
The autocomplete component is an input field that provides selectable suggestions as a merchant types into it. It allows merchants to quickly search through and select from large collections of options. It's a convenience wrapper around the Combobox and Listbox components with minor UI differences.
Autocomplete component examples
Use to help merchants complete text input quickly from a list of options.
import {Autocomplete, Icon} from '@shopify/polaris';
import {SearchIcon} from '@shopify/polaris-icons';
import {useState, useCallback, useMemo} from 'react';
function AutocompleteExample() {
const deselectedOptions = useMemo(
() => [
{value: 'rustic', label: 'Rustic'},
{value: 'antique', label: 'Antique'},
{value: 'vinyl', label: 'Vinyl'},
{value: 'vintage', label: 'Vintage'},
{value: 'refurbished', label: 'Refurbished'},
],
[],
);
const [selectedOptions, setSelectedOptions] = useState<string[]>([]);
const [inputValue, setInputValue] = useState('');
const [options, setOptions] = useState(deselectedOptions);
const updateText = useCallback(
(value: string) => {
setInputValue(value);
if (value === '') {
setOptions(deselectedOptions);
return;
}
const filterRegex = new RegExp(value, 'i');
const resultOptions = deselectedOptions.filter((option) =>
option.label.match(filterRegex),
);
setOptions(resultOptions);
},
[deselectedOptions],
);
const updateSelection = useCallback(
(selected: string[]) => {
const selectedValue = selected.map((selectedItem) => {
const matchedOption = options.find((option) => {
return option.value.match(selectedItem);
});
return matchedOption && matchedOption.label;
});
setSelectedOptions(selected);
setInputValue(selectedValue[0] || '');
},
[options],
);
const textField = (
<Autocomplete.TextField
onChange={updateText}
label="Tags"
value={inputValue}
prefix={<Icon source={SearchIcon} tone="base" />}
placeholder="Search"
autoComplete="off"
/>
);
return (
<div style={{height: '225px'}}>
<Autocomplete
options={options}
selected={selectedOptions}
onSelect={updateSelection}
textField={textField}
/>
</div>
);
}
Props
- id?string
A unique identifier for the Autocomplete.
- options[] | []
Collection of options to be listed.
- selectedstring[]
The selected options.
- textFieldReact.ReactElement
The text field component attached to the list of options.
- preferredPosition?'above' | 'below' | 'mostSpace' | 'cover'
The preferred direction to open the popover.
- listTitle?string
Title of the list of options.
- allowMultiple?boolean
Allow more than one option to be selected.
- actionBefore? & { wrapOverflow?: boolean; }
An action to render above the list of options.
- loading?boolean
Display loading state.
- willLoadMoreResults?boolean
Indicates if more results will load dynamically.
- emptyState?React.ReactNode
Is rendered when there are no options.
- onSelect(selected: string[]) => void
Callback when the selection of options is changed.
- onLoadMoreResults?() => void
Callback when the end of the list is reached.
Best practices
The autocomplete component should:
- Be clearly labeled so it’s obvious to the merchant what type of options will be available
- Limit the number of options displayed at once
- Not be used within a popover
- Indicate a loading state to the merchant while option data is being populated
Content guidelines
The input field for autocomplete should follow the content guidelines for text fields.
Related components
- For an input field without suggested options, use the text field component
- For a list of selectable options not linked to an input field, use the option list component
- For a text field that triggers a popover, use the combo box component
Accessibility
Structure
The autocomplete component is based on the ARIA 1.2 combobox pattern and the Aria 1.2 Listbox pattern.
The autocomplete list displays below the text field or other control by default so it is easy for merchants to discover and use. However, you can change the position with the preferredPosition prop.
Autocomplete features can be challenging for merchants with visual, motor, and cognitive disabilities. Even when they’re built using best practices, these features can be difficult to use with some assistive technologies. Merchants should always be able to search, enter data, or perform other activities without relying on the autocomplete.
Use autocomplete as progressive enhancement to make the interface easier to use for most merchants.
Require that merchants make a selection from the autocomplete to complete a task.
Keyboard support
- Give the autocomplete text input keyboard focus with the tab key (or shift + tab when tabbing backwards)
- Access the list of options with the up and down arrow keys
- Select an option that has focus with the enter/return key