PPolaris ComponentsConnect an agent

IndexTable

Lists a collection of records such as orders in a table, with row selection and bulk actions, for browsing and acting on many resources at once.

Preview

Code

Example.jsx
import { useState, useCallback } from 'react';
import { LegacyCard, IndexTable, useIndexResourceState, Badge, Text } from '@shopify/polaris';

const INITIAL_ORDERS = [
  { id: '1024', order: '#1024', date: 'Jul 12 at 4:34 pm', customer: 'Jaydon Stanton', total: '$969.44', payment: 'paid', fulfillment: 'unfulfilled' },
  { id: '1023', order: '#1023', date: 'Jul 12 at 11:02 am', customer: 'Mae Jemison', total: '$284.10', payment: 'pending', fulfillment: 'unfulfilled' },
  { id: '1022', order: '#1022', date: 'Jul 11 at 9:16 pm', customer: 'Ellen Ochoa', total: '$1,204.00', payment: 'paid', fulfillment: 'fulfilled' },
  { id: '1021', order: '#1021', date: 'Jul 11 at 2:48 pm', customer: 'Kimiko Ishii', total: '$76.20', payment: 'refunded', fulfillment: 'fulfilled' },
  { id: '1020', order: '#1020', date: 'Jul 10 at 8:05 am', customer: 'Tobias Lütke', total: '$412.75', payment: 'paid', fulfillment: 'unfulfilled' },
];

const PAYMENT_BADGES = {
  paid: { tone: 'success', progress: 'complete', label: 'Paid' },
  pending: { tone: 'attention', progress: 'incomplete', label: 'Payment pending' },
  refunded: { tone: 'critical', progress: 'complete', label: 'Refunded' },
};

const FULFILLMENT_BADGES = {
  fulfilled: { tone: 'success', progress: 'complete', label: 'Fulfilled' },
  unfulfilled: { tone: 'attention', progress: 'incomplete', label: 'Unfulfilled' },
};

const HEADINGS = [
  { title: 'Order' },
  { title: 'Date' },
  { title: 'Customer' },
  { title: 'Total', alignment: 'end' },
  { title: 'Payment status' },
  { title: 'Fulfillment status' },
];

const RESOURCE_NAME = { singular: 'order', plural: 'orders' };

const StatusBadge = ({ config }) => (
  <Badge tone={config.tone} progress={config.progress}>
    {config.label}
  </Badge>
);

export const Example = () => {
  const [orders, setOrders] = useState(INITIAL_ORDERS);

  const { selectedResources, allResourcesSelected, handleSelectionChange, clearSelection } =
    useIndexResourceState(orders);

  const markFulfilled = useCallback(() => {
    setOrders((current) =>
      current.map((item) =>
        selectedResources.includes(item.id) ? { ...item, fulfillment: 'fulfilled' } : item
      )
    );
    clearSelection();
  }, [selectedResources, clearSelection]);

  const capturePayments = useCallback(() => {
    setOrders((current) =>
      current.map((item) =>
        selectedResources.includes(item.id) && item.payment === 'pending'
          ? { ...item, payment: 'paid' }
          : item
      )
    );
    clearSelection();
  }, [selectedResources, clearSelection]);

  const promotedBulkActions = [
    { content: 'Mark as fulfilled', onAction: markFulfilled },
    { content: 'Capture payments', onAction: capturePayments },
  ];

  const bulkActions = [
    { content: 'Print packing slips', onAction: clearSelection },
    { content: 'Cancel orders', destructive: true, onAction: clearSelection },
  ];

  const rowMarkup = orders.map((item, index) => (
    <IndexTable.Row
      id={item.id}
      key={item.id}
      position={index}
      selected={selectedResources.includes(item.id)}
    >
      <IndexTable.Cell>
        <Text as="span" fontWeight="semibold">
          {item.order}
        </Text>
      </IndexTable.Cell>
      <IndexTable.Cell>{item.date}</IndexTable.Cell>
      <IndexTable.Cell>{item.customer}</IndexTable.Cell>
      <IndexTable.Cell>
        <Text as="span" alignment="end" numeric>
          {item.total}
        </Text>
      </IndexTable.Cell>
      <IndexTable.Cell>
        <StatusBadge config={PAYMENT_BADGES[item.payment]} />
      </IndexTable.Cell>
      <IndexTable.Cell>
        <StatusBadge config={FULFILLMENT_BADGES[item.fulfillment]} />
      </IndexTable.Cell>
    </IndexTable.Row>
  ));

  return (
    <LegacyCard>
      <IndexTable
        resourceName={RESOURCE_NAME}
        itemCount={orders.length}
        selectedItemsCount={allResourcesSelected ? 'All' : selectedResources.length}
        onSelectionChange={handleSelectionChange}
        promotedBulkActions={promotedBulkActions}
        bulkActions={bulkActions}
        headings={HEADINGS}
      >
        {rowMarkup}
      </IndexTable>
    </LegacyCard>
  );
};