# Radio group

SvRadioGroup: a plan picker and a shipping selector with a disabled option, roving arrow-key focus and label + validation.

Uses `@svgrid/grid` (MIT). Category: Buttons & Toggles. Live demo: https://svgrid.com/demos/310-radio-group/

Install: `npm i @svgrid/grid` (Svelte 5).

## About this example

SvRadioGroup in Svelte 5 as a plan picker and a shipping-method selector with a disabled option. The group follows the WAI-ARIA radiogroup pattern with roving arrow-key focus, takes its choices from an options array, reports the value through onChange and supports a label and validation.

SvRadioGroup - a production plan picker and a shipping-method selector. Arrow-key navigable (WAI-ARIA radiogroup), with label + validation. Copy-paste ready.

## Imports, features and API used

- Imports: `@svgrid/grid`

## Frequently asked questions

### How do options get their labels and values?

options is an array of { value, label } objects, optionally with disabled or a description. The group renders one radio per entry and reports the chosen value.

### How does keyboard focus move?

One radio is in the tab order at a time; arrow keys move selection and focus together, matching native radio behaviour.

### Can the radios be laid out horizontally?

Yes. orientation='horizontal' places them in a row; the default is a vertical list.

## Related documentation

- [SvRadioGroup](https://svgrid.com/docs/help/ui-components/sv-radio-group.md)

## Related articles

- [Building a Project / Task Board with a Svelte Data Grid](https://svgrid.com/blog/project-task-board-grid/) - How to build a task management grid with grouping by status or assignee, inline edits, subtask tree rows, and saved views - without reaching for a dedicated project management tool.
- [Multi-Level (Grouped) Column Headers in SvGrid](https://svgrid.com/blog/multi-level-column-headers/) - Band related columns under a shared parent header using SvGrid's nested column definition - how to nest, pin, combine with sorting and filtering, and when NOT to use grouping.
- [SvGrid UI Components Tips and Tricks: Svelte 5 Buttons, Inputs, Tree, Forms](https://svgrid.com/blog/svgrid-ui-components-tips-and-tricks/) - Practical tips for the SvGrid UI component kit - SvButton states, a Cmd+K command palette, a virtualized searchable tree, schema-driven forms, a parsing date/time field, and the shared field contract - each with a code snippet.

## Source code (310-radio-group.svelte)

```svelte
<script lang="ts">
  /**
   * SvRadioGroup - a production plan picker and a shipping-method selector.
   * Arrow-key navigable (WAI-ARIA radiogroup), with label + validation. Copy-paste
   * ready.
   */
  import { SvRadioGroup } from '@svgrid/grid'

  const plans = [
    { value: 'starter', label: 'Starter - $0 / mo' },
    { value: 'pro', label: 'Pro - $29 / mo' },
    { value: 'team', label: 'Team - $99 / mo' },
    { value: 'enterprise', label: 'Enterprise - contact us' },
  ]
  const shipping = [
    { value: 'standard', label: 'Standard (5-7 days)' },
    { value: 'express', label: 'Express (2 days)' },
    { value: 'overnight', label: 'Overnight', disabled: true },
  ]
  let plan = $state<string | number | null>('pro')
  let ship = $state<string | number | null>(null)
  let submitted = $state(false)
  const shipError = $derived(submitted && !ship ? 'Choose a shipping method' : undefined)
</script>

<div class="wrap">
  <header>
    <h2>Radio group</h2>
    <p>Single choice with roving arrow-key focus (WAI-ARIA radiogroup) - plans, shipping, payment.</p>
  </header>

  <div class="cols">
    <section>
      <h3>Choose a plan</h3>
      <SvRadioGroup options={plans} value={plan} onChange={(v) => (plan = v)} />
    </section>

    <section>
      <SvRadioGroup
        options={shipping}
        value={ship}
        label="Shipping method"
        required
        invalid={!!shipError}
        error={shipError}
        onChange={(v) => (ship = v)}
      />
      <button class="btn" onclick={() => (submitted = true)}>Place order</button>
    </section>
  </div>
</div>

<style>
  .wrap { padding: 20px; max-width: 640px; display: flex; flex-direction: column; gap: 16px; }
  header h2 { margin: 0 0 4px; font-size: 20px; font-weight: 700; }
  header p { margin: 0; color: var(--sg-muted, #64748b); font-size: 13.5px; line-height: 1.5; }
  .cols { display: grid; grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); gap: 28px; align-items: start; }
  section h3 { margin: 0 0 10px; font-size: 12px; text-transform: uppercase; letter-spacing: 0.03em; color: var(--sg-muted, #64748b); }
  .btn { margin-top: 14px; font: inherit; font-size: 13px; font-weight: 600; padding: 7px 16px; border: 0; border-radius: 8px; background: var(--sg-accent, #2563eb); color: var(--sg-on-accent, #fff); cursor: pointer; }
</style>
```

[View this example on GitHub](https://github.com/sv-grid/sv-grid/blob/main/examples/src/demos/310-radio-group.svelte)
