# Radio group

A list of native radio buttons for picking exactly one of a few options that should all stay visible. For a long list use a select; for a compact segmented look use a select button.

- Package: [@vigdx/ui](https://www.npmjs.com/package/@vigdx/ui)
- Selectors: `vig-radio-group`, `ng-template[vigSelectOption]`
- Source: [libs/ui/src/lib/molecules/radio-group/radio-group.ts](https://github.com/bakyurekk/vigdx/blob/main/libs/ui/src/lib/molecules/radio-group/radio-group.ts)

```typescript title="Import"
import { RadioGroup } from '@vigdx/ui/radio-group';
import { SelectOptionTemplate } from '@vigdx/ui/select';
```

## Basic

Pass `options` as `SelectOption` objects (`value`, `label`, optional `icon` and `disabled`) and bind `[(value)]`. Values are compared with `===`; the value is `null` while nothing is chosen.

```typescript title="radio-group-basic.ts"
import { ChangeDetectionStrategy, Component, signal } from '@angular/core';
import { RadioGroup } from '@vigdx/ui/radio-group';

import { PLANS, type Plan } from './plans';

@Component({
  selector: 'app-radio-group-basic',
  changeDetection: ChangeDetectionStrategy.OnPush,
  imports: [RadioGroup],
  template: `
    <vig-radio-group
      label="Plan"
      hint="You can change plans at any time."
      [options]="plans"
      [(value)]="plan"
    />
    <p class="text-sm text-muted-foreground">Selected: {{ plan() ?? 'none' }}</p>
  `,
})
export class RadioGroupBasicDemo {
  protected readonly plans = PLANS;
  protected readonly plan = signal<Plan | null>('team');
}
```

```typescript title="plans.ts"
import type { SelectOption } from '@vigdx/ui/select';

export type Plan = 'free' | 'team' | 'enterprise';

export const PLANS: SelectOption<Plan>[] = [
  { value: 'free', label: 'Free' },
  { value: 'team', label: 'Team' },
  { value: 'enterprise', label: 'Enterprise' },
];

export const PLAN_DETAILS: Record<Plan, string> = {
  free: 'Up to 3 projects, community support.',
  team: 'Unlimited projects, 10 seats, email support.',
  enterprise: 'SSO, audit log and a dedicated contact.',
};
```

## Horizontal

`orientation="horizontal"` lays the options out in a wrapping row. An option with `disabled: true` is skipped by the arrow keys.

```typescript title="radio-group-horizontal.ts"
import { ChangeDetectionStrategy, Component } from '@angular/core';
import { RadioGroup } from '@vigdx/ui/radio-group';
import { type SelectOption } from '@vigdx/ui/select';

@Component({
  selector: 'app-radio-group-horizontal',
  changeDetection: ChangeDetectionStrategy.OnPush,
  imports: [RadioGroup],
  template: `
    <vig-radio-group
      label="Shipping"
      orientation="horizontal"
      value="standard"
      [options]="shipping"
    />
  `,
})
export class RadioGroupHorizontalDemo {
  protected readonly shipping: SelectOption[] = [
    { value: 'standard', label: 'Standard' },
    { value: 'express', label: 'Express' },
    { value: 'pickup', label: 'Store pickup' },
    { value: 'drone', label: 'Drone', disabled: true },
  ];
}
```

## Custom option template

An `<ng-template vigSelectOption let-option>` replaces the default icon and label of each option, here with a second line of detail. The text stays inside the radio's label.

```typescript title="radio-group-custom-option.ts"
import { ChangeDetectionStrategy, Component } from '@angular/core';
import { RadioGroup } from '@vigdx/ui/radio-group';
import { SelectOptionTemplate } from '@vigdx/ui/select';

import { PLAN_DETAILS, PLANS, type Plan } from './plans';

@Component({
  selector: 'app-radio-group-custom-option',
  changeDetection: ChangeDetectionStrategy.OnPush,
  imports: [RadioGroup, SelectOptionTemplate],
  template: `
    <vig-radio-group label="Plan" value="free" [options]="plans">
      <ng-template vigSelectOption let-option>
        <span class="flex flex-col">
          <span class="font-medium">{{ option.label }}</span>
          <span class="text-muted-foreground">{{ detail(option.value) }}</span>
        </span>
      </ng-template>
    </vig-radio-group>
  `,
})
export class RadioGroupCustomOptionDemo {
  protected readonly plans = PLANS;

  // The template context types option values as unknown.
  protected detail(value: unknown): string {
    return PLAN_DETAILS[value as Plan];
  }
}
```

```typescript title="plans.ts"
import type { SelectOption } from '@vigdx/ui/select';

export type Plan = 'free' | 'team' | 'enterprise';

export const PLANS: SelectOption<Plan>[] = [
  { value: 'free', label: 'Free' },
  { value: 'team', label: 'Team' },
  { value: 'enterprise', label: 'Enterprise' },
];

export const PLAN_DETAILS: Record<Plan, string> = {
  free: 'Up to 3 projects, community support.',
  team: 'Unlimited projects, 10 seats, email support.',
  enterprise: 'SSO, audit log and a dedicated contact.',
};
```

## Disabled, read-only and error

`disabled` disables every radio. `readonly` keeps them focusable but puts the checked state back after each change. Errors show once the group is `touched` and `invalid`.

```typescript title="radio-group-states.ts"
import { ChangeDetectionStrategy, Component } from '@angular/core';
import { RadioGroup } from '@vigdx/ui/radio-group';

import { PLANS } from './plans';

@Component({
  selector: 'app-radio-group-states',
  changeDetection: ChangeDetectionStrategy.OnPush,
  imports: [RadioGroup],
  template: `
    <div class="grid gap-6 sm:grid-cols-3">
      <vig-radio-group label="Disabled" value="team" [options]="plans" [disabled]="true" />
      <vig-radio-group label="Read-only" value="team" [options]="plans" [readonly]="true" />
      <vig-radio-group
        label="Plan"
        [options]="plans"
        [required]="true"
        [touched]="true"
        [invalid]="true"
        [errors]="[{ kind: 'required' }]"
        [errorMessages]="{ required: 'Choose a plan.' }"
      />
    </div>
  `,
})
export class RadioGroupStatesDemo {
  protected readonly plans = PLANS;
}
```

```typescript title="plans.ts"
import type { SelectOption } from '@vigdx/ui/select';

export type Plan = 'free' | 'team' | 'enterprise';

export const PLANS: SelectOption<Plan>[] = [
  { value: 'free', label: 'Free' },
  { value: 'team', label: 'Team' },
  { value: 'enterprise', label: 'Enterprise' },
];

export const PLAN_DETAILS: Record<Plan, string> = {
  free: 'Up to 3 projects, community support.',
  team: 'Unlimited projects, 10 seats, email support.',
  enterprise: 'SSO, audit log and a dedicated contact.',
};
```

## Reactive Forms

Import `VigValueAccessor` next to the component to use `formControlName`, `[formControl]` or `ngModel`. The group counts as touched once focus leaves all of its radios.

```typescript title="radio-group-reactive-forms.ts"
import { JsonPipe } from '@angular/common';
import { ChangeDetectionStrategy, Component } from '@angular/core';
import { FormControl, FormGroup, ReactiveFormsModule, Validators } from '@angular/forms';
import { Button } from '@vigdx/ui/button';
import { VigValueAccessor } from '@vigdx/ui/core';
import { RadioGroup } from '@vigdx/ui/radio-group';

import { PLANS, type Plan } from './plans';

@Component({
  selector: 'app-radio-group-reactive-forms',
  changeDetection: ChangeDetectionStrategy.OnPush,
  imports: [JsonPipe, ReactiveFormsModule, Button, RadioGroup, VigValueAccessor],
  template: `
    <form class="flex flex-col gap-4" [formGroup]="form" (ngSubmit)="submit()">
      <vig-radio-group label="Plan" formControlName="plan" [options]="plans" />
      <div class="flex gap-2">
        <vig-button type="submit" label="Continue" />
        <vig-button label="Reset" variant="secondary" (click)="form.reset()" />
      </div>
      <pre class="rounded-input bg-subtle p-3 font-mono text-xs">{{ form.value | json }}</pre>
    </form>
  `,
})
export class RadioGroupReactiveFormsDemo {
  protected readonly plans = PLANS;
  protected readonly form = new FormGroup({
    plan: new FormControl<Plan | null>(null, Validators.required),
  });

  protected submit(): void {
    this.form.markAllAsTouched();
  }
}
```

```typescript title="plans.ts"
import type { SelectOption } from '@vigdx/ui/select';

export type Plan = 'free' | 'team' | 'enterprise';

export const PLANS: SelectOption<Plan>[] = [
  { value: 'free', label: 'Free' },
  { value: 'team', label: 'Team' },
  { value: 'enterprise', label: 'Enterprise' },
];

export const PLAN_DETAILS: Record<Plan, string> = {
  free: 'Up to 3 projects, community support.',
  team: 'Unlimited projects, 10 seats, email support.',
  enterprise: 'SSO, audit log and a dedicated contact.',
};
```

## Signal Forms

`vig-radio-group` implements `FormValueControl<T | null>`, where `T` is the type of the option values.

```typescript title="radio-group-signal-forms.ts"
import { ChangeDetectionStrategy, Component, signal } from '@angular/core';
import { FormField, form, required } from '@angular/forms/signals';
import { RadioGroup } from '@vigdx/ui/radio-group';
import { type SelectOption } from '@vigdx/ui/select';

import { PLANS, type Plan } from './plans';

type Billing = 'monthly' | 'yearly';

@Component({
  selector: 'app-radio-group-signal-forms',
  changeDetection: ChangeDetectionStrategy.OnPush,
  imports: [FormField, RadioGroup],
  template: `
    <div class="grid gap-6 sm:grid-cols-2">
      <vig-radio-group label="Plan" [options]="plans" [formField]="order.plan" />
      <vig-radio-group label="Billing" [options]="billing" [formField]="order.billing" />
    </div>
    <p class="text-sm text-muted-foreground">
      Valid: {{ order().valid() ? 'yes' : 'no' }} · {{ model().plan ?? 'no plan' }},
      {{ model().billing }}
    </p>
  `,
})
export class RadioGroupSignalFormsDemo {
  protected readonly plans = PLANS;
  protected readonly billing: SelectOption<Billing>[] = [
    { value: 'monthly', label: 'Monthly' },
    { value: 'yearly', label: 'Yearly (2 months free)' },
  ];
  protected readonly model = signal<{ plan: Plan | null; billing: Billing }>({
    plan: null,
    billing: 'monthly',
  });
  protected readonly order = form(this.model, (path) => {
    required(path.plan);
  });
}
```

```typescript title="plans.ts"
import type { SelectOption } from '@vigdx/ui/select';

export type Plan = 'free' | 'team' | 'enterprise';

export const PLANS: SelectOption<Plan>[] = [
  { value: 'free', label: 'Free' },
  { value: 'team', label: 'Team' },
  { value: 'enterprise', label: 'Enterprise' },
];

export const PLAN_DETAILS: Record<Plan, string> = {
  free: 'Up to 3 projects, community support.',
  team: 'Unlimited projects, 10 seats, email support.',
  enterprise: 'SSO, audit log and a dedicated contact.',
};
```

## Styling hooks

Internal elements carry a stable `data-part` and the component's `data-scope`; both are public API, so removing one is a breaking change. Select a part by both, e.g. `[data-scope="radio-group"][data-part="group"] { … }`: the pair stays within this component when kit components nest, and also reaches parts in overlay panels, which render outside the host. State is exposed through `aria-*` and, where listed, a `data-state`; literal values are checked by the API gate, values computed from the component's state follow its documented types. Other `data-*` attributes listed below (such as `data-opening` while an overlay animates) are API too. See [Customizing](https://vigdx.com/docs/theming.md#customizing).

Scope: `[data-scope="radio-group"]`

| Part | Element | data-state |
| --- | --- | --- |
| `group` | The `<fieldset role="radiogroup">`. |   |
| `label` | The `<legend>`. |   |
| `options` | Layout wrapper of the options. |   |
| `option` | One option `<label>`. | `checked, unchecked` |
| `control` | Wrapper of the radio circle and its dot. |   |
| `input` | The native radio (the visible circle). |   |
| `indicator` | The dot of the checked option. |   |
| `option-label` | The option text (without a custom option template). |   |
| `hint` | The `vig-hint` (a `display: contents` host; style its `text` part). |   |
| `disabled-reason` | The `vig-hint` with the disabled reason (`display: contents`). |   |
| `error` | The `vig-field-error` (a `display: contents` host; style its `message` part). |   |

## Accessibility

- Native `<input type="radio">` elements share one `name` (generated when you do not set one), so the group is a single tab stop and the arrow keys move and select, mirrored under right-to-left.
- The options sit in a native `<fieldset>` whose `<legend>` is the visible `label`; it keeps `role="radiogroup"` for the group states below. Without a visible label, pass `ariaLabel`.
- `required`, `readonly` and an invalid state set `aria-required`, `aria-readonly` and `aria-invalid` on the group; hint and error are linked through `aria-describedby`.
- Each option label is a `<label>` around its radio, so clicking the text selects the option, also with a custom template.

## API

### RadioGroup

Selector: `vig-radio-group` · Kind: component

Classic radio list for an exclusive choice among a handful of visible options. Each option is a native `<input type="radio">` sharing one `name`, so arrow keys, the single tab stop and form semantics come from the browser. For a compact segmented look use `vig-select-button`.

#### Inputs

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `ariaLabel` | `string \| undefined` | `undefined` | — |
| `dirty` | `boolean` | `false` | — |
| `disabled` | `boolean` | `false` | — |
| `disabledReasons` | `readonly WithOptionalFieldTree<DisabledReason>[]` | `[]` | — |
| `errorMessages` | `Record<string, string>` | — | — |
| `errors` | `readonly ValidationError.WithOptionalFieldTree[]` | `[]` | — |
| `hidden` | `boolean` | `false` | — |
| `hint` | `string \| undefined` | `undefined` | — |
| `invalid` | `boolean` | `false` | — |
| `label` | `string` | `''` | Visible group label; omit it and pass `ariaLabel` when a heading names the group. |
| `name` | `string` | `''` | — |
| `options` | `readonly SelectOption<T>[]` | `[]` | — |
| `orientation` | `RadioGroupOrientation` | `'vertical'` | — |
| `pending` | `boolean` | `false` | — |
| `readonly` | `boolean` | `false` | — |
| `required` | `boolean` | `false` | — |
| `touched` | `boolean` | `false` | — |
| `value` (model) | `T \| null` | `null` | — |

#### Outputs

| Name | Payload | Description |
| --- | --- | --- |
| `touch` | `void` | — |
| `valueChange` | `T \| null` | — |

#### Methods

| Signature | Description |
| --- | --- |
| `focus(options?: FocusOptions): void` | Moves focus to the control's interactive element (the input, or the trigger of a popup control). Signal Forms calls it through `field().focusBoundControl()`; the host element itself cannot take focus. |
| `reset(): void` | Returns the control to its pristine look: drops in-progress text, closes an open popup, clears errors the component raised itself and the submitted state of `errorDisplay: 'interacted'`. Signal Forms calls it from `field().reset()`; `VigValueAccessor` from `control.reset()`. It never changes the value: the bound form owns that. |

### SelectOptionTemplate

Selector: `ng-template[vigSelectOption]` · Kind: directive

Custom rendering for options of `vig-select`, `vig-multi-select`, `vig-listbox` and `vig-select-button`, replacing the default icon + label. `vig-select` also uses it for the selected value shown in the trigger.

Bind the same options to `vigSelectOptionOf` (like `ngForOf`) to type `let-option` in the template; without it the option value is `unknown`.

**Example :**

```html
<vig-select label="Status" [options]="statuses">
  <ng-template vigSelectOption [vigSelectOptionOf]="statuses" let-option>
    <i [class]="option.value" aria-hidden="true"></i> {{ option.label }}
  </ng-template>
</vig-select>
```

#### Inputs

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `vigSelectOptionOf` | `readonly SelectOption<T>[] \| null \| undefined` | `undefined` | Type-only: the options the template renders, so the template type checker infers the option value type. Not read at runtime. |

### VigValueAccessor

Kind: directive

Bridges the vigdx form fields to Reactive Forms (`formControl`, `formControlName`) and template-driven forms (`ngModel`). Import it next to the component; Signal Forms (`[formField]`) need no accessor.

#### Methods

| Signature | Description |
| --- | --- |
| `registerOnChange(fn: (value: unknown) => void): void` | — |
| `registerOnTouched(fn: () => void): void` | — |
| `setDisabledState(disabled: boolean): void` | — |
| `writeValue(value: unknown): void` | — |
