PPolaris ComponentsConnect an agent

Combobox

An accessible text input paired with a Listbox popover, used to build custom autocomplete experiences such as a multi-select collection picker with tags.

Preview

Code

Example.jsx
import { useState, useCallback, useMemo } from 'react';
import { Card, BlockStack, InlineStack, Text, Combobox, Listbox, Tag, Icon } from '@shopify/polaris';
import { SearchIcon } from '@shopify/polaris-icons';

const COLLECTIONS = [
  { value: 'winter-2026', label: 'Winter 2026' },
  { value: 'outerwear', label: 'Outerwear' },
  { value: 'footwear', label: 'Footwear' },
  { value: 'accessories', label: 'Accessories' },
  { value: 'home-goods', label: 'Home goods' },
  { value: 'sale', label: 'Sale' },
  { value: 'gift-cards', label: 'Gift cards' },
];

const labelFor = (value) => COLLECTIONS.find((option) => option.value === value)?.label ?? value;

export const Example = () => {
  const [query, setQuery] = useState('');
  const [selected, setSelected] = useState(['outerwear']);

  const options = useMemo(() => {
    const needle = query.trim().toLowerCase();
    if (needle === '') return COLLECTIONS;
    return COLLECTIONS.filter((option) => option.label.toLowerCase().includes(needle));
  }, [query]);

  const toggleSelection = useCallback((value) => {
    setSelected((current) =>
      current.includes(value) ? current.filter((item) => item !== value) : [...current, value]
    );
    setQuery('');
  }, []);

  return (
    <Card>
      <BlockStack gap="300">
        <Combobox
          allowMultiple
          activator={
            <Combobox.TextField
              label="Collections"
              value={query}
              onChange={setQuery}
              prefix={<Icon source={SearchIcon} tone="base" />}
              placeholder="Search collections"
              autoComplete="off"
            />
          }
        >
          {options.length > 0 ? (
            <Listbox onSelect={toggleSelection}>
              {options.map((option) => (
                <Listbox.Option
                  key={option.value}
                  value={option.value}
                  selected={selected.includes(option.value)}
                  accessibilityLabel={option.label}
                >
                  {option.label}
                </Listbox.Option>
              ))}
            </Listbox>
          ) : null}
        </Combobox>
        {selected.length > 0 && (
          <InlineStack gap="200" wrap>
            {selected.map((value) => (
              <Tag key={value} onRemove={() => toggleSelection(value)}>
                {labelFor(value)}
              </Tag>
            ))}
          </InlineStack>
        )}
        <Text as="p" variant="bodySm" tone="subdued">
          This product will appear in {selected.length} collection{selected.length === 1 ? '' : 's'}.
        </Text>
      </BlockStack>
    </Card>
  );
};