PPolaris ComponentsConnect an agent

Filters

A search field plus a set of filter controls and removable applied-filter chips, used above a list or table so merchants can narrow down orders, products or customers.

Preview

Code

Example.jsx
import { useState, useCallback, useMemo } from 'react';
import { Card, Box, BlockStack, InlineStack, Text, Filters, ChoiceList, RangeSlider, Badge, Divider } from '@shopify/polaris';

const PRODUCTS = [
  { id: '1', title: 'Aurora wool coat', status: 'active', price: 189 },
  { id: '2', title: 'Bermuda linen shirt', status: 'draft', price: 64 },
  { id: '3', title: 'Cedar leather boots', status: 'active', price: 145 },
  { id: '4', title: 'Dune canvas tote', status: 'archived', price: 38 },
  { id: '5', title: 'Everest down parka', status: 'active', price: 320 },
  { id: '6', title: 'Fjord knit beanie', status: 'draft', price: 22 },
];

const STATUS_CHOICES = [
  { label: 'Active', value: 'active' },
  { label: 'Draft', value: 'draft' },
  { label: 'Archived', value: 'archived' },
];

const STATUS_TONES = { active: 'success', draft: 'info', archived: 'warning' };

const PRICE_BOUNDS = [0, 350];

const isDefaultRange = (range) => range[0] === PRICE_BOUNDS[0] && range[1] === PRICE_BOUNDS[1];

export const Example = () => {
  const [queryValue, setQueryValue] = useState('');
  const [status, setStatus] = useState(['active']);
  const [price, setPrice] = useState(PRICE_BOUNDS);

  const handleClearAll = useCallback(() => {
    setQueryValue('');
    setStatus([]);
    setPrice(PRICE_BOUNDS);
  }, []);

  const filters = [
    {
      key: 'status',
      label: 'Status',
      shortcut: true,
      pinned: true,
      filter: (
        <ChoiceList
          title="Status"
          titleHidden
          allowMultiple
          choices={STATUS_CHOICES}
          selected={status}
          onChange={setStatus}
        />
      ),
    },
    {
      key: 'price',
      label: 'Price',
      shortcut: true,
      filter: (
        <RangeSlider
          label="Price is between"
          labelHidden
          value={price}
          onChange={setPrice}
          min={PRICE_BOUNDS[0]}
          max={PRICE_BOUNDS[1]}
          step={1}
          prefix="$"
          output
        />
      ),
    },
  ];

  const appliedFilters = [];
  if (status.length > 0) {
    appliedFilters.push({
      key: 'status',
      label: `Status: ${status.map((value) => STATUS_CHOICES.find((choice) => choice.value === value).label).join(', ')}`,
      onRemove: () => setStatus([]),
    });
  }
  if (!isDefaultRange(price)) {
    appliedFilters.push({
      key: 'price',
      label: `Price between $${price[0]} and $${price[1]}`,
      onRemove: () => setPrice(PRICE_BOUNDS),
    });
  }

  const results = useMemo(() => {
    const needle = queryValue.trim().toLowerCase();
    return PRODUCTS.filter((product) => {
      const matchesQuery = needle === '' || product.title.toLowerCase().includes(needle);
      const matchesStatus = status.length === 0 || status.includes(product.status);
      const matchesPrice = product.price >= price[0] && product.price <= price[1];
      return matchesQuery && matchesStatus && matchesPrice;
    });
  }, [queryValue, status, price]);

  return (
    <Card padding="0">
      <Filters
        queryValue={queryValue}
        queryPlaceholder="Search products"
        filters={filters}
        appliedFilters={appliedFilters}
        onQueryChange={setQueryValue}
        onQueryClear={() => setQueryValue('')}
        onClearAll={handleClearAll}
      />
      <Divider />
      <Box padding="400">
        {results.length === 0 ? (
          <Text as="p" tone="subdued">
            No products match these filters.
          </Text>
        ) : (
          <BlockStack gap="300">
            {results.map((product) => (
              <InlineStack key={product.id} align="space-between" blockAlign="center">
                <InlineStack gap="200" blockAlign="center">
                  <Text as="span" fontWeight="medium">
                    {product.title}
                  </Text>
                  <Badge tone={STATUS_TONES[product.status]}>
                    {STATUS_CHOICES.find((choice) => choice.value === product.status).label}
                  </Badge>
                </InlineStack>
                <Text as="span" tone="subdued">
                  ${product.price}
                </Text>
              </InlineStack>
            ))}
          </BlockStack>
        )}
      </Box>
    </Card>
  );
};