# Radio group

- **[Atoms](/components/)**

One choice from a few options that are all visible. Built on native radio inputs.

## When to use

- **Use for**: Two to five mutually exclusive options that are better seen together.
- **Not for**: Many options. Use Select. Independent yes/no choices. Use Checkbox.
- **Pairs with**: Field, Button, forms.

## Preview

radio-group.tsx

```
import { Theme } from '@/components/klarhimmel/theme/style';
import { RadioGroup, Radio } from '@/components/klarhimmel/ui/radio-group';

export default function Example() {
  return (
    <Theme theme="brand">
      <RadioGroup legend="Breakfast" name="meal" defaultValue="buffet">
        <Radio value="buffet" label="Buffet" />
        <Radio value="tray" label="Tray to the room" />
      </RadioGroup>
    </Theme>
  );
}
```

## Import

- **CLI**

  ```
  npx shadcn add @klarhimmel/radio-group
  ```
- **Import**

  ```
  import { RadioGroup, Radio } from '@/components/klarhimmel/ui/radio-group';
  ```

- **Files**: radio-group.tsx, in components/klarhimmel/ui/. With it: theme, utils.
- **Render in**: Render it inside <Theme>, once near the root of your app. Tokens and fonts are scoped to it, so outside it the component is unstyled.

## Examples

- **States**

  ```
  import { Radio, RadioGroup } from '@/components/klarhimmel/ui/radio-group';
  
  export default function RadioGroupStates() {
    return (
      <div className="grid w-[min(40rem,calc(100cqw-3rem))] gap-8 @md:grid-cols-2">
        <RadioGroup legend="Room type" invalid hint="Choose a room to continue">
          <Radio value="single" label="Single" />
          <Radio value="double" label="Double" />
        </RadioGroup>
        <RadioGroup legend="Payment" defaultValue="card">
          <Radio value="card" label="Card" />
          <Radio value="invoice" label="Invoice" disabled hint="Only for company bookings" />
          <Radio value="cash" label="Pay at the desk" />
        </RadioGroup>
      </div>
    );
  }
  ```
- **Form**

  ```
  import { useState } from 'react';
  import { Button } from '@/components/klarhimmel/ui/button';
  import { Radio, RadioGroup } from '@/components/klarhimmel/ui/radio-group';
  
  export default function RadioGroupForm() {
    const [sent, setSent] = useState('');
    return (
      <form
        className="flex w-[min(24rem,calc(100cqw-3rem))] flex-col items-start gap-4"
        onSubmit={(e) => { e.preventDefault(); setSent(String(new FormData(e.currentTarget).get('bed') ?? 'nothing')); }}
      >
        <RadioGroup legend="Bed type" name="bed" defaultValue="double">
          <Radio value="double" label="Double bed" />
          <Radio value="twin" label="Two single beds" />
        </RadioGroup>
        <Button type="submit">Save choice</Button>
        <p role="status" className="text-kh-small text-kh-muted">{sent ? `The form sent bed=${sent}.` : 'Not sent yet.'}</p>
      </form>
    );
  }
  ```

## Props

- **RadioGroup.legend**: Required: the question the options answer. It names the group for assistive tech.
- **RadioGroup.name**: The form field name. Radios sharing a name are one group to the browser.
- **RadioGroup.value**: Controlled or uncontrolled state.
- **RadioGroup.defaultValue**: Controlled or uncontrolled state.
- **RadioGroup.onValueChange**: Controlled or uncontrolled state.
- **RadioGroup.disabled**: Helper or error text, error styling, switch the group off.
- **RadioGroup.invalid**: Helper or error text, error styling, switch the group off.
- **RadioGroup.hint**: Helper or error text, error styling, switch the group off.
- **RadioGroup.className**: Extra classes, merged after the component's own.
- **RadioGroup.children**
- **Radio.value**: Controlled or uncontrolled state.
- **Radio.label**: Value and label are required; the hint sits under the label.
- **Radio.hint**: Helper or error text, error styling, switch the group off.
- **native attributes**: Every native attribute of the element passes through, className included.

## Accessibility

- Tab / Shift+Tab · Enters and leaves the group as one stop, landing on the chosen option
- Down / Right · Moves to the next option and selects it, wrapping
- Up / Left · Moves to the previous option and selects it, wrapping
- Space · Selects the focused option

[Accessibility](/get-started/accessibility)

Native radio inputs: the browser provides the keys and the form value, also without script.

[Previous](/components/label)

[Next](/components/select)
