PPolaris ComponentsConnect an agent

ChoiceList

A titled list of choices rendered as radio buttons, or as checkboxes with allowMultiple, with per-choice help text and nested fields.

Preview

Code

Example.jsx
import { useState, useCallback } from 'react';
import { Card, BlockStack, ChoiceList, TextField, Text, Divider } from '@shopify/polaris';

const DISCOUNT_TYPES = [
  { label: 'Percentage off', value: 'percentage', helpText: 'Take a share off the order total.' },
  { label: 'Fixed amount off', value: 'fixed', helpText: 'Take a set dollar amount off the order total.' },
  { label: 'Free shipping', value: 'shipping' },
];

const CHANNELS = [
  { label: 'Online Store', value: 'online-store' },
  { label: 'Point of Sale', value: 'pos', helpText: 'Staff can apply this discount at the register.' },
  { label: 'Shop app', value: 'shop' },
  { label: 'Draft orders', value: 'draft', disabled: true },
];

export const Example = () => {
  const [discountType, setDiscountType] = useState(['percentage']);
  const [channels, setChannels] = useState(['online-store', 'shop']);
  const [minimum, setMinimum] = useState(['none']);
  const [amount, setAmount] = useState('50.00');

  const requirementChoices = [
    { label: 'No minimum requirement', value: 'none' },
    {
      label: 'Minimum purchase amount',
      value: 'amount',
      renderChildren: (isSelected) =>
        isSelected && (
          <TextField
            label="Amount"
            labelHidden
            value={amount}
            onChange={setAmount}
            autoComplete="off"
            type="currency"
            prefix="$"
          />
        ),
    },
  ];

  const changeMinimum = useCallback((value) => setMinimum(value), []);

  return (
    <Card>
      <BlockStack gap="400">
        <ChoiceList
          title="Discount type"
          choices={DISCOUNT_TYPES}
          selected={discountType}
          onChange={setDiscountType}
        />
        <Divider />
        <ChoiceList
          allowMultiple
          title="Available on"
          choices={CHANNELS}
          selected={channels}
          onChange={setChannels}
          error={channels.length === 0 ? 'Pick at least one sales channel.' : undefined}
        />
        <Divider />
        <ChoiceList
          title="Minimum requirements"
          choices={requirementChoices}
          selected={minimum}
          onChange={changeMinimum}
        />
        <Text as="p" tone="subdued">
          {discountType[0] === 'shipping' ? 'Free shipping' : `A ${discountType[0]} discount`} on{' '}
          {channels.length} channel{channels.length === 1 ? '' : 's'}
          {minimum[0] === 'amount' ? `, for orders over $${amount}.` : ', with no minimum.'}
        </Text>
      </BlockStack>
    </Card>
  );
};