# Radio

Picks one option from a few that are all worth seeing at once. Built on ui-sdk's RadioGroup: arrow keys move the choice, Tab leaves the group, and it submits with its form. Styled to match Checkbox.

- Beta: ready to use; its API can still change in a minor release.
- Category: Selection

```tsx
import { RadioGroup, RadioGroupItem } from "@leap-crm/design-system";
```

## Best practices

- Do: Use it for one choice from a few options that are all worth seeing at once, like whether a matter is billed fixed fee or hourly.
- Do: Name the group with `aria-label` or `aria-labelledby`, and each item with a `Label` pointing at its id, so pressing the label picks it.
- Do: Lay two or three short options in a row with a flex class on the group, and keep longer ones stacked.
- Do: Mark an invalid choice with `aria-invalid` on each item and say what to do in text, like Please choose a billing type. The red edge alone is colour alone.
- Don't: Use it for a long list, like matter types or lawyers in a firm. Use `Select`, which keeps the options in a popup.
- Don't: Use a single radio for yes or no. Use `Checkbox` in a form, or `Switch` for a setting that applies at once.

## Keyboard

| Keys | What they do |
| --- | --- |
| Tab | Lands on the checked item, or the first when none is; the group is one stop. |
| Arrow keys | Move between the radios and check them, skipping disabled ones and wrapping at both ends. |
| Space | Checks the focused item. |
| Enter | Submits the form, and never checks an item. |

## Related

- [Checkbox](./checkbox.md)
- [Segmented Control](./segmented-control.md)
- [Select](./select.md)

## Examples

What the examples import:

```tsx
import { type ReactNode } from "react";
import { Label, RadioGroup, RadioGroupItem } from "@leap-crm/design-system";
```

Scaffolding the examples call. It lives in the docs page, not in the library:

```tsx
function Option({
  id,
  value,
  children,
  disabled,
}: {
  id: string;
  value: string;
  children: ReactNode;
  disabled?: boolean;
}) {
  return (
    <div className="text-body flex items-center gap-2">
      <RadioGroupItem id={id} value={value} disabled={disabled} />
      <Label htmlFor={id}>{children}</Label>
    </div>
  );
}
```

### Group

Hover a circle for its navy ring, Tab into the group and use the arrow keys, and watch the dot grow into the new choice.

```tsx
<RadioGroup defaultValue="fixed" aria-label="Billing">
  <Option id="fixed" value="fixed">
    Fixed fee
  </Option>
  <Option id="hourly" value="hourly">
    Hourly rate
  </Option>
  <Option id="capped" value="capped">
    Capped fee
  </Option>
  <Option id="contingency" value="contingency" disabled>
    Contingency (not offered)
  </Option>
</RadioGroup>
```

### States

Unselected, selected, invalid (aria-invalid on each item), and a disabled group, unselected and selected.

```tsx
<div className="flex items-center gap-8">
  <RadioGroup
    defaultValue="b"
    aria-label="Rest and selected"
    className="flex gap-6"
  >
    <RadioGroupItem value="a" aria-label="Unselected" />
    <RadioGroupItem value="b" aria-label="Selected" />
  </RadioGroup>
  <RadioGroup aria-label="Invalid" required>
    <RadioGroupItem value="a" aria-label="Invalid" aria-invalid />
  </RadioGroup>
  <RadioGroup
    defaultValue="b"
    disabled
    aria-label="Disabled"
    className="flex gap-6"
  >
    <RadioGroupItem value="a" aria-label="Disabled" />
    <RadioGroupItem value="b" aria-label="Disabled selected" />
  </RadioGroup>
</div>
```

### In a row

Lay short options out in a row with a flex class on the group.

```tsx
<RadioGroup
  defaultValue="individual"
  aria-label="Contact type"
  className="flex gap-6"
>
  <Option id="individual" value="individual">
    Individual
  </Option>
  <Option id="organisation" value="organisation">
    Organisation
  </Option>
</RadioGroup>
```

## Props

RadioGroup also takes ui-sdk's value, defaultValue, onValueChange, disabled, readOnly, required, name, form and loop. RadioGroupItem takes value and disabled.

### RadioGroup

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `className` | `string` |  | Classes added to the root element, merged with its own. Tokens only. |

### RadioGroupItem

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `className` | `string` |  | Classes added to the root element, merged with its own. Tokens only. |
