PPolaris ComponentsConnect an agent

Option list

A grouped list of selectable options with single or multiple selection, used inside popovers and side panels for things like sales channels and locations.

Preview

Code

Example.jsx
import { useState } from 'react';
import { Card, Box, BlockStack, InlineStack, Text, OptionList, Popover, Button, Badge } from '@shopify/polaris';

const CHANNEL_SECTIONS = [
  {
    title: 'Sales channels',
    options: [
      { value: 'online-store', label: 'Online store' },
      { value: 'pos', label: 'Point of Sale' },
      { value: 'shop', label: 'Shop' },
    ],
  },
  {
    title: 'Markets',
    options: [
      { value: 'north-america', label: 'North America' },
      { value: 'europe', label: 'Europe' },
      { value: 'asia-pacific', label: 'Asia Pacific', disabled: true },
    ],
  },
];

const LOCATIONS = [
  { value: 'toronto', label: 'Toronto warehouse' },
  { value: 'berlin', label: 'Berlin warehouse' },
  { value: 'osaka', label: 'Osaka warehouse' },
];

const labelForLocation = (value) => LOCATIONS.find((option) => option.value === value)?.label;

export const Example = () => {
  const [channels, setChannels] = useState(['online-store']);
  const [location, setLocation] = useState(['toronto']);
  const [popoverActive, setPopoverActive] = useState(false);

  return (
    <BlockStack gap="400">
      <Card padding="0">
        <Box padding="300" paddingBlockEnd="0">
          <InlineStack align="space-between" blockAlign="center">
            <Text as="h2" variant="headingMd">
              Availability
            </Text>
            <Badge tone={channels.length > 0 ? 'success' : 'attention'}>
              {`${channels.length} selected`}
            </Badge>
          </InlineStack>
        </Box>
        <OptionList
          title="Publish this product to"
          onChange={setChannels}
          sections={CHANNEL_SECTIONS}
          selected={channels}
          allowMultiple
        />
      </Card>

      <Card>
        <BlockStack gap="300">
          <Text as="h2" variant="headingMd">
            Ships from
          </Text>
          <Popover
            active={popoverActive}
            activator={
              <Button disclosure onClick={() => setPopoverActive((active) => !active)}>
                {labelForLocation(location[0]) ?? 'Select a location'}
              </Button>
            }
            onClose={() => setPopoverActive(false)}
          >
            <OptionList
              onChange={(selected) => {
                setLocation(selected);
                setPopoverActive(false);
              }}
              options={LOCATIONS}
              selected={location}
            />
          </Popover>
        </BlockStack>
      </Card>
    </BlockStack>
  );
};