PPolaris ComponentsConnect an agent

ResourceList

Shows a collection of records such as customers as scannable rows with media, selection, bulk actions and filtering, when each row needs more layout than a table cell allows.

Preview

Code

Example.jsx
import { useState, useCallback, useMemo } from 'react';
import { LegacyCard, ResourceList, ResourceItem, Avatar, Text, Badge, InlineStack, BlockStack, Filters, ChoiceList } from '@shopify/polaris';

const CUSTOMERS = [
  { id: '101', name: 'Mae Jemison', location: 'Decatur, AL', orders: 12, spent: '$1,204.00', segment: 'vip' },
  { id: '102', name: 'Jaydon Stanton', location: 'Ottawa, ON', orders: 4, spent: '$412.75', segment: 'returning' },
  { id: '103', name: 'Ellen Ochoa', location: 'Los Angeles, CA', orders: 27, spent: '$3,986.20', segment: 'vip' },
  { id: '104', name: 'Kimiko Ishii', location: 'Vancouver, BC', orders: 1, spent: '$76.20', segment: 'new' },
  { id: '105', name: 'Rafael Núñez', location: 'Austin, TX', orders: 6, spent: '$688.40', segment: 'returning' },
];

const SEGMENT_BADGES = {
  vip: { tone: 'success', label: 'VIP' },
  returning: { tone: 'info', label: 'Returning' },
  new: { tone: 'attention', label: 'New' },
};

const SEGMENT_CHOICES = [
  { label: 'VIP', value: 'vip' },
  { label: 'Returning', value: 'returning' },
  { label: 'New', value: 'new' },
];

const SORT_OPTIONS = [
  { label: 'Most spent', value: 'spent' },
  { label: 'Most orders', value: 'orders' },
  { label: 'Name A–Z', value: 'name' },
];

const RESOURCE_NAME = { singular: 'customer', plural: 'customers' };

const toAmount = (value) => Number(value.replace(/[$,]/g, ''));

export const Example = () => {
  const [selectedItems, setSelectedItems] = useState([]);
  const [queryValue, setQueryValue] = useState('');
  const [segments, setSegments] = useState([]);
  const [sortValue, setSortValue] = useState('spent');

  const handleQueryClear = useCallback(() => setQueryValue(''), []);
  const handleSegmentsRemove = useCallback(() => setSegments([]), []);

  const handleClearAll = useCallback(() => {
    handleQueryClear();
    handleSegmentsRemove();
  }, [handleQueryClear, handleSegmentsRemove]);

  const items = useMemo(() => {
    const query = queryValue.trim().toLowerCase();
    const filtered = CUSTOMERS.filter((customer) => {
      const matchesQuery = !query || customer.name.toLowerCase().includes(query);
      const matchesSegment = segments.length === 0 || segments.includes(customer.segment);
      return matchesQuery && matchesSegment;
    });

    return [...filtered].sort((first, second) => {
      if (sortValue === 'name') return first.name.localeCompare(second.name);
      if (sortValue === 'orders') return second.orders - first.orders;
      return toAmount(second.spent) - toAmount(first.spent);
    });
  }, [queryValue, segments, sortValue]);

  const appliedFilters = segments.length
    ? [
        {
          key: 'segment',
          label: `Segment: ${segments.map((value) => SEGMENT_BADGES[value].label).join(', ')}`,
          onRemove: handleSegmentsRemove,
        },
      ]
    : [];

  const filterControl = (
    <Filters
      queryValue={queryValue}
      queryPlaceholder="Search customers"
      onQueryChange={setQueryValue}
      onQueryClear={handleQueryClear}
      onClearAll={handleClearAll}
      appliedFilters={appliedFilters}
      filters={[
        {
          key: 'segment',
          label: 'Segment',
          shortcut: true,
          filter: (
            <ChoiceList
              title="Segment"
              titleHidden
              allowMultiple
              choices={SEGMENT_CHOICES}
              selected={segments}
              onChange={setSegments}
            />
          ),
        },
      ]}
    />
  );

  const promotedBulkActions = [
    { content: 'Add tags', onAction: () => setSelectedItems([]) },
    { content: 'Send email', onAction: () => setSelectedItems([]) },
  ];

  return (
    <LegacyCard>
      <ResourceList
        resourceName={RESOURCE_NAME}
        items={items}
        selectedItems={selectedItems}
        onSelectionChange={setSelectedItems}
        selectable
        filterControl={filterControl}
        sortValue={sortValue}
        sortOptions={SORT_OPTIONS}
        onSortChange={setSortValue}
        promotedBulkActions={promotedBulkActions}
        bulkActions={[{ content: 'Delete customers', destructive: true, onAction: () => setSelectedItems([]) }]}
        renderItem={(customer) => {
          const { id, name, location, orders, spent, segment } = customer;
          const badge = SEGMENT_BADGES[segment];

          return (
            <ResourceItem
              id={id}
              url="#"
              media={<Avatar customer size="md" name={name} />}
              accessibilityLabel={`View details for ${name}`}
              name={name}
              shortcutActions={[
                { content: 'View orders', url: '#' },
                { content: 'Email customer', url: '#' },
              ]}
              persistActions
            >
              <BlockStack gap="050">
                <InlineStack gap="200" blockAlign="center">
                  <Text as="span" variant="bodyMd" fontWeight="semibold">
                    {name}
                  </Text>
                  <Badge tone={badge.tone}>{badge.label}</Badge>
                </InlineStack>
                <Text as="span" tone="subdued">
                  {location} — {orders} orders, {spent} spent
                </Text>
              </BlockStack>
            </ResourceItem>
          );
        }}
      />
    </LegacyCard>
  );
};