PPolaris ComponentsConnect an agent

RadioButton

A single button in a group where exactly one option can be selected at a time; use it when you need full control over the layout of each choice.

Preview

Code

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

const SHIPPING_RATES = [
  {
    id: 'standard',
    label: 'Standard shipping',
    helpText: 'Arrives in 5 to 7 business days.',
    price: 'Free',
  },
  {
    id: 'express',
    label: 'Express shipping',
    helpText: 'Arrives in 2 business days with tracking.',
    price: '$12.00',
  },
  {
    id: 'overnight',
    label: 'Overnight shipping',
    helpText: 'Not available for your delivery address.',
    price: '$29.00',
    disabled: true,
  },
];

export const Example = () => {
  const [rate, setRate] = useState('standard');
  const [billing, setBilling] = useState('monthly');

  const chooseRate = useCallback((_checked, id) => setRate(id), []);

  return (
    <Card>
      <BlockStack gap="400">
        <Text as="h2" variant="headingMd">
          Delivery method
        </Text>
        <BlockStack gap="200">
          {SHIPPING_RATES.map((option) => (
            <InlineStack key={option.id} align="space-between" blockAlign="start" wrap={false} gap="400">
              <RadioButton
                label={option.label}
                helpText={option.helpText}
                id={option.id}
                name="shipping-rate"
                checked={rate === option.id}
                disabled={option.disabled}
                onChange={chooseRate}
              />
              <Text as="span" tone={option.disabled ? 'subdued' : undefined} fontWeight="medium">
                {option.price}
              </Text>
            </InlineStack>
          ))}
        </BlockStack>
        <Divider />
        <BlockStack gap="200">
          <Text as="h3" variant="headingSm">
            Billing period
          </Text>
          <InlineStack gap="500">
            <RadioButton
              label="Monthly"
              id="monthly"
              name="billing"
              checked={billing === 'monthly'}
              onChange={(_checked, id) => setBilling(id)}
            />
            <InlineStack gap="200" blockAlign="center">
              <RadioButton
                label="Yearly"
                id="yearly"
                name="billing"
                checked={billing === 'yearly'}
                onChange={(_checked, id) => setBilling(id)}
              />
              <Badge tone="success">Save 20%</Badge>
            </InlineStack>
          </InlineStack>
          <Text as="p" tone="subdued">
            {billing === 'yearly' ? 'Billed $278.40 once a year.' : 'Billed $29.00 every month.'}
          </Text>
        </BlockStack>
      </BlockStack>
    </Card>
  );
};