# Radio

> A radio group for choosing exactly one option, with arrow-key navigation.

Use a radio group when people must choose exactly one of a few options and should see them all at once. For longer lists, use a [select](/docs/components/select).

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

export default function RadioBasic() {
  return (
    <RadioGroup
      label="Plan"
      name="plan"
      defaultValue="team"
      description="You can change plans at any time."
    >
      <Radio value="personal" label="Personal, 1 seat" />
      <Radio value="team" label="Team, up to 10 seats" />
      <Radio value="enterprise" label="Enterprise, unlimited seats" />
    </RadioGroup>
  );
}
```

## Installation

```bash
npm i @aloxe/react @aloxe/tokens
```

Then add the CSS imports from [Installation](/docs/getting-started/installation).

## Usage

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

<RadioGroup label="Plan" name="plan" defaultValue="team">
  <Radio value="personal" label="Personal" />
  <Radio value="team" label="Team" />
</RadioGroup>
```

`RadioGroup` renders a `<fieldset>` whose legend is `label`. Each `Radio` needs a `value` and a `label`.

## Examples

### Horizontal

`orientation="horizontal"` lays the options out in a row that wraps on narrow screens. Keep it for short labels.

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

export default function RadioHorizontal() {
  return (
    <RadioGroup label="Density" defaultValue="comfortable" orientation="horizontal">
      <Radio value="compact" label="Compact" />
      <Radio value="comfortable" label="Comfortable" />
      <Radio value="spacious" label="Spacious" />
    </RadioGroup>
  );
}
```

### States

An error on the group, and a single disabled option.

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

export default function RadioStates() {
  return (
    <div className="flex flex-col gap-8">
      <RadioGroup label="Billing period" error="Choose a billing period.">
        <Radio value="monthly" label="Monthly" />
        <Radio value="yearly" label="Yearly" />
      </RadioGroup>
      <RadioGroup label="Region" defaultValue="eu">
        <Radio value="eu" label="Europe" />
        <Radio value="us" label="United States" />
        <Radio value="ap" label="Asia Pacific (full)" disabled />
      </RadioGroup>
    </div>
  );
}
```

### Controlled

Pass `value` and `onValueChange` to react to the choice, such as updating a price.

```tsx
"use client";

import { Radio, RadioGroup } from "@aloxe/react";
import { useState } from "react";

type Delivery = "standard" | "express";

export default function RadioControlled() {
  const [delivery, setDelivery] = useState<Delivery>("standard");

  return (
    <div className="flex flex-col gap-3">
      <RadioGroup<Delivery>
        label="Delivery"
        value={delivery}
        onValueChange={(value) => setDelivery(value)}
      >
        <Radio value="standard" label="Standard, 3 to 5 days" />
        <Radio value="express" label="Express, next day" />
      </RadioGroup>
      <p className="aloxe-text-body-sm text-ax-text-secondary">
        Shipping: {delivery === "express" ? "€12.00" : "Free"}
      </p>
    </div>
  );
}
```

## Accessibility

| Key | Action |
| --- | --- |
| Tab | Moves focus into the group, onto the checked radio (or the first one), and then out of the group. |
| Down or Right | Moves to the next option and checks it. |
| Up or Left | Moves to the previous option and checks it. |
| Space | Checks the focused option if it isn't checked. |

- Follows the WAI-ARIA radio group pattern: one tab stop for the whole group.
- The legend names the group, and each label names its radio.
- An error on the group sets `aria-invalid` and is linked to it.

## API reference

Every part accepts `className` and renders a `data-aloxe` attribute you can target in CSS, such as `[data-aloxe="radio"]`.

**RadioGroup** (extends Base UI RadioGroup)

| Prop | Type | Default | Required | Description |
| --- | --- | --- | --- | --- |
| `label` | `ReactNode` |  | Yes | The group's legend. It names the group for screen readers. |
| `value` | `Value` |  |  | Controlled value. Pair with onValueChange. |
| `defaultValue` | `Value` |  |  | Initial value when uncontrolled. |
| `onValueChange` | `(value: Value, details) => void` |  |  | Called when the user picks an option. |
| `orientation` | `"vertical" \| "horizontal"` | `"vertical"` |  | Stacks the options or lays them out in a wrapping row. |
| `showLabel` | `boolean` | `true` |  | false hides the legend visually only. |
| `description` | `ReactNode` |  |  | Supporting text under the legend. |
| `error` | `ReactNode` |  |  | Error message, e.g. "Choose a plan". Sets aria-invalid. |
| `disabled` | `boolean` | `false` |  | Disables every option. |
| `name` | `string` |  |  | Form field name. |
| `required` | `boolean` | `false` |  | Requires a choice before the form submits. |
| `className` | `string` |  |  | Classes for the wrapper. |

**Radio** (extends Base UI Radio.Root)

| Prop | Type | Default | Required | Description |
| --- | --- | --- | --- | --- |
| `value` | `Value` |  | Yes | The value this option selects. |
| `label` | `ReactNode` |  | Yes | The visible label and the radio's accessible name. |
| `showLabel` | `boolean` | `true` |  | false hides the label visually only. |
| `disabled` | `boolean` | `false` |  | Disables this option only. |
| `className` | `string` |  |  | Classes for the label row. |

## Figma

Matches the **Radio** component. Figma `Checked` (`Off`, `On`) is the group's `value`. `Label` and `Show label` map to `label` and `showLabel` on each `Radio`.

See [Figma](/docs/getting-started/figma) for where to get the file and how its names map to code.
