Autocomplete
A text field that filters a list of options as the merchant types and lets them pick one or several, used for tagging, vendor pickers and other long known-value lists.
Preview
Code
Example.jsx
import { useState, useCallback, useMemo } from 'react';
import { Card, BlockStack, InlineStack, Text, Autocomplete, Tag, Icon, EmptySearchResult } from '@shopify/polaris';
import { SearchIcon } from '@shopify/polaris-icons';
const TAG_OPTIONS = [
{ value: 'summer-sale', label: 'Summer sale' },
{ value: 'best-seller', label: 'Best seller' },
{ value: 'back-in-stock', label: 'Back in stock' },
{ value: 'clearance', label: 'Clearance' },
{ value: 'gift-guide', label: 'Gift guide' },
{ value: 'new-arrival', label: 'New arrival' },
{ value: 'limited-edition', label: 'Limited edition' },
{ value: 'staff-pick', label: 'Staff pick' },
{ value: 'eco-friendly', label: 'Eco friendly' },
{ value: 'bundle', label: 'Bundle' },
];
const labelFor = (value) => TAG_OPTIONS.find((option) => option.value === value)?.label ?? value;
export const Example = () => {
const [inputValue, setInputValue] = useState('');
const [selected, setSelected] = useState(['best-seller']);
const options = useMemo(() => {
const query = inputValue.trim().toLowerCase();
if (query === '') return TAG_OPTIONS;
return TAG_OPTIONS.filter((option) => option.label.toLowerCase().includes(query));
}, [inputValue]);
const handleSelect = useCallback((nextSelected) => {
setSelected(nextSelected);
setInputValue('');
}, []);
const removeTag = useCallback((value) => {
setSelected((current) => current.filter((item) => item !== value));
}, []);
const textField = (
<Autocomplete.TextField
label="Product tags"
value={inputValue}
onChange={setInputValue}
prefix={<Icon source={SearchIcon} tone="base" />}
placeholder="Search tags"
autoComplete="off"
helpText="Tags help buyers find this product in your storefront search."
/>
);
return (
<Card>
<BlockStack gap="300">
<Autocomplete
allowMultiple
options={options}
selected={selected}
onSelect={handleSelect}
textField={textField}
emptyState={<EmptySearchResult title="No tags found" description="Try a different tag name." />}
/>
{selected.length > 0 && (
<InlineStack gap="200" wrap>
{selected.map((value) => (
<Tag key={value} onRemove={() => removeTag(value)}>
{labelFor(value)}
</Tag>
))}
</InlineStack>
)}
<Text as="p" variant="bodySm" tone="subdued">
{selected.length} of {TAG_OPTIONS.length} tags applied
</Text>
</BlockStack>
</Card>
);
};