# Radio group

One choice from a few options that all stay visible.

- Family: selection
- Import: `import { RadioGroup, Radio } from "@senvenli/react/selection";`
- Page: https://design.senvenli.com/docs/components/radio
- JSON: https://design.senvenli.com/docs/components/radio.json

## Example: Options as data

A few choices that all stay visible.

```tsx
import { RadioGroup } from "@senvenli/react/selection";

export default function Example() {
  return (
    <RadioGroup
      label="Billing period"
      defaultValue="yearly"
      items={[
        { value: "monthly", label: "Monthly" },
        { value: "yearly", label: "Yearly", description: "Two months free." },
      ]}
    />
  );
}
```

## Examples

### With descriptions

Radio children, each with a line of detail; one is unavailable.

```tsx
import { Radio, RadioGroup } from "@senvenli/react/selection";

export default function Example() {
  return (
    <RadioGroup label="Delivery" description="Applies to every order in this shipment." defaultValue="standard">
      <Radio value="standard" description="3–5 business days, free.">
        Standard
      </Radio>
      <Radio value="express" description="Next business day, $12.">
        Express
      </Radio>
      <Radio value="pickup" description="Not available for this address." isDisabled>
        Store pickup
      </Radio>
    </RadioGroup>
  );
}
```

### Horizontal

Short options on one line; they wrap when space runs out.

```tsx
import { RadioGroup } from "@senvenli/react/selection";
import { useState } from "react";

export default function Example() {
  const [priority, setPriority] = useState("normal");
  return (
    <RadioGroup
      label="Priority"
      description={`Saved as “${priority}”.`}
      orientation="horizontal"
      value={priority}
      onChange={setPriority}
      items={[
        { value: "low", label: "Low" },
        { value: "normal", label: "Normal" },
        { value: "high", label: "High" },
      ]}
    />
  );
}
```

### Cards

`variant="card"`: each option a card, its content yours (a name, a Badge, a price).

```tsx
import { Badge, Text } from "@senvenli/react/display";
import { Stack } from "@senvenli/react/layout";
import { Radio, RadioGroup } from "@senvenli/react/selection";

// A card's content is yours: here a name with a "Recommended" Badge, a line of detail and a price.
export default function Example() {
  return (
    <RadioGroup label="Plan" variant="card" defaultValue="team">
      <Radio value="starter" description="For one person.">
        <Stack as="span" space="space-050">
          <span>Starter</span>
          <Text size="body-large" weight="medium" isNumeric>$0</Text>
        </Stack>
      </Radio>
      <Radio value="team" description="Up to 20 people.">
        <Stack as="span" space="space-050">
          <Stack direction="horizontal" as="span" space="space-075" align="center">
            <span>Team</span>
            <Badge tone="brand">Recommended</Badge>
          </Stack>
          <Text size="body-large" weight="medium" isNumeric>$12 / month</Text>
        </Stack>
      </Radio>
      <Radio value="business" description="Audit log and single sign-on.">
        <Stack as="span" space="space-050">
          <span>Business</span>
          <Text size="body-large" weight="medium" isNumeric>$29 / month</Text>
        </Stack>
      </Radio>
    </RadioGroup>
  );
}
```

### Time slots

Cards as a time picker; a taken time stays visible, disabled, with its reason.

```tsx
import { RadioGroup } from "@senvenli/react/selection";

// A time-slot picker is a card group: one time per card; a taken time stays visible, disabled, and says so.
const TIMES = ["9:00", "9:30", "10:00", "10:30", "11:00", "11:30"];
const TAKEN = new Set(["9:30", "11:00"]);

export default function Example() {
  return (
    <RadioGroup
      label="Available times · March 18"
      variant="card"
      defaultValue="10:30"
      items={TIMES.map((time) => ({ value: time, label: time, description: TAKEN.has(time) ? "Taken" : undefined, isDisabled: TAKEN.has(time) }))}
    />
  );
}
```

## Guidelines

### When to use it

- One choice from 2 to 5 options people should compare.

### Use something else when

- [select](https://design.senvenli.com/docs/components/select.md): There are many options.
- [list-box](https://design.senvenli.com/docs/components/list-box.md): Options need icons or descriptions, or several may be picked, in a visible list.

### Do and avoid

- Use variant="card" when each option needs a line of detail (a plan, a delivery option, a time slot); its content is yours.

## Accessibility

Keyboard:

- Arrow keys: Moves and selects within the group.

Built in:

- One tab stop for the group.
- Required fields show "Fill in this field." after a blur or a submit; your own `errorMessage` replaces it.

You provide:

- A label for the group.

## API reference

### RadioGroup

| Prop | Type | Default | Required | Description |
| --- | --- | --- | --- | --- |
| size | `ComponentSize` |  | no | The size of every Radio in the group. |
| label | `React.ReactNode` |  | yes | The group legend, required. |
| items | `readonly RadioItem[]` |  | no | The options as data, or `Radio` children. |
| children | `React.ReactNode` |  | no |  |
| description | `React.ReactNode` |  | no |  |
| errorMessage | `React.ReactNode` |  | no |  |
| requiredValidation | `RequiredValidation` |  | no | Who says a required group with no option chosen is missing: `field` (default) shows "Fill in this field." when focus leaves the group and on a DS Form submit, which it then blocks; `app` only marks the group. See {@link RequiredValidation}. The app's `isInvalid` (`true` with its `errorMessage`, or `false`) overrides the check, as on TextField. Disabled, empty and required, it still blocks a DS Form's submit and cannot take focus (as TextField). |
| variant | `RadioGroupVariant` | `"default"` | no | `default`: rows with a dot. `card`: each option a card in a grid of equal columns (no narrower than 180px), for choices that need a line of detail each (a plan, a delivery option, a time slot). |

Behavior props from React Aria:

| Prop | Type | Default | Required | Description |
| --- | --- | --- | --- | --- |
| defaultValue | `string` |  | no | The default value (uncontrolled). |
| onFocus | `(e: FocusEvent<Element, Element>) => void` |  | no | Handler that is called when the element receives focus. |
| onBlur | `(e: FocusEvent<Element, Element>) => void` |  | no | Handler that is called when the element loses focus. |
| onChange | `(value: string) => void` |  | no | Handler that is called when the value changes. |
| isDisabled | `boolean` |  | no | Whether the input is disabled. |
| onFocusChange | `(isFocused: boolean) => void` |  | no | Handler that is called when the element's focus status changes. |
| form | `string` |  | no | The `<form>` element to associate the input with. The value of this attribute must be the id of a `<form>` in the same document. See [MDN](https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Elements/input#form). |
| name | `string` |  | no | The name of the input element, used when submitting an HTML form. See [MDN](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#htmlattrdefname). |
| value | `string` |  | no | The current value (controlled). |
| isRequired | `boolean` |  | no | Whether user input is required on the input before form submission. |
| isReadOnly | `boolean` |  | no | Whether the input can be selected but not changed by the user. |
| isInvalid | `boolean` |  | no | Whether the input value is invalid. |
| validate | `(value: string) => true \| ValidationError \| null \| undefined` |  | no | A function that returns an error message if a given value is invalid. Validation errors are displayed to the user when the form is submitted if `validationBehavior="native"`. For realtime validation, use the `isInvalid` prop instead. |
| validationBehavior | `"aria" \| "native"` |  | no | Whether to use native HTML form validation to prevent form submission when the value is missing or invalid, or mark the field as required or invalid via ARIA. |

Also takes 79 standard DOM and ARIA attributes (id, aria-label, event handlers…).

### Radio

| Prop | Type | Default | Required | Description |
| --- | --- | --- | --- | --- |
| value | `string` |  | yes |  |
| size | `ComponentSize` |  | no | `small`, `medium` (default) or `large`; a group's size, then the provider default, otherwise. |
| children | `React.ReactNode` |  | yes |  |
| description | `React.ReactNode` |  | no |  |
| isDisabled | `boolean` |  | no |  |

### Tokens it reads

`--ds-background-brand-bold`, `--ds-background-disabled`, `--ds-background-input`, `--ds-background-neutral-pressed`, `--ds-background-selected`, `--ds-border`, `--ds-border-bold`, `--ds-border-danger`, `--ds-border-focused`, `--ds-border-selected`, `--ds-easing-standard`, `--ds-radius-full`, `--ds-radius-large`, `--ds-radius-xsmall`, `--ds-shadow-raised`, `--ds-space-050`, `--ds-space-075`, `--ds-space-100`, `--ds-space-150`, `--ds-space-200`, `--ds-space-300`, `--ds-surface-raised`, `--ds-text`, `--ds-text-disabled`, `--ds-text-on-brand`, `--ds-text-subtlest`

## Related

- [select](https://design.senvenli.com/docs/components/select.md)
- [checkbox](https://design.senvenli.com/docs/components/checkbox.md)
