radio group

A set of checkable buttons—known as radio buttons—where no more than one of the buttons can be checked at a time.

PreviousNext
import { Component } from '@angular/core';
import { FormsModule } from '@angular/forms';

import { ZardFieldImports } from '@/shared/components/field/field.imports';
import { ZardRadioGroupImports } from '@/shared/components/radio-group/radio-group.imports';

@Component({
  selector: 'z-demo-radio-group-default',
  imports: [...ZardRadioGroupImports, ...ZardFieldImports, FormsModule],
  template: `
    <z-radio-group [(value)]="selected" class="w-fit">
      <div class="flex items-center gap-3">
        <z-radio zId="r1" value="default" />
        <label z-field-label for="r1">Default</label>
      </div>
      <div class="flex items-center gap-3">
        <z-radio zId="r2" value="comfortable" />
        <label z-field-label for="r2">Comfortable</label>
      </div>
      <div class="flex items-center gap-3">
        <z-radio zId="r3" value="compact" />
        <label z-field-label for="r3">Compact</label>
      </div>
    </z-radio-group>
  `,
})
export class ZardDemoRadioGroupDefaultComponent {
  selected: unknown = 'comfortable';
}

Installation

Copy
npx zard-cli@latest add radio-group

Usage

import { ZardRadioGroupImports } from '@/shared/components/radio-group/radio-group.imports';
Copy
<z-radio-group [(value)]="selected">
  <z-radio value="one" />
  <z-radio value="two" />
</z-radio-group>
Copy

Examples

description

Radio group items with a description using the Field component.

Standard spacing for most use cases.

More space between elements.

Minimal spacing for dense layouts.

import { Component } from '@angular/core';
import { FormsModule } from '@angular/forms';

import { ZardFieldImports } from '@/shared/components/field/field.imports';
import { ZardRadioGroupImports } from '@/shared/components/radio-group/radio-group.imports';

@Component({
  selector: 'z-demo-radio-group-description',
  imports: [...ZardRadioGroupImports, ...ZardFieldImports, FormsModule],
  template: `
    <z-radio-group [(value)]="selected" class="w-fit">
      <div z-field zOrientation="horizontal">
        <z-radio zId="desc-r1" value="default" />
        <div z-field-content>
          <label z-field-label for="desc-r1">Default</label>
          <p z-field-description>Standard spacing for most use cases.</p>
        </div>
      </div>
      <div z-field zOrientation="horizontal">
        <z-radio zId="desc-r2" value="comfortable" />
        <div z-field-content>
          <label z-field-label for="desc-r2">Comfortable</label>
          <p z-field-description>More space between elements.</p>
        </div>
      </div>
      <div z-field zOrientation="horizontal">
        <z-radio zId="desc-r3" value="compact" />
        <div z-field-content>
          <label z-field-label for="desc-r3">Compact</label>
          <p z-field-description>Minimal spacing for dense layouts.</p>
        </div>
      </div>
    </z-radio-group>
  `,
})
export class ZardDemoRadioGroupDescriptionComponent {
  selected: unknown = 'comfortable';
}

choice card

Use FieldLabel to wrap the entire Field for a clickable card-style selection.

import { Component } from '@angular/core';
import { FormsModule } from '@angular/forms';

import { ZardFieldImports } from '@/shared/components/field/field.imports';
import { ZardRadioGroupImports } from '@/shared/components/radio-group/radio-group.imports';

@Component({
  selector: 'z-demo-radio-group-choice-card',
  imports: [...ZardRadioGroupImports, ...ZardFieldImports, FormsModule],
  template: `
    <z-radio-group [(value)]="selected" class="min-w-sm">
      <label z-field-label for="plus-plan">
        <div z-field zOrientation="horizontal">
          <div z-field-content>
            <div z-field-title>Plus</div>
            <p z-field-description>For individuals and small teams.</p>
          </div>
          <z-radio zId="plus-plan" value="plus" />
        </div>
      </label>
      <label z-field-label for="pro-plan">
        <div z-field zOrientation="horizontal">
          <div z-field-content>
            <div z-field-title>Pro</div>
            <p z-field-description>For growing businesses.</p>
          </div>
          <z-radio zId="pro-plan" value="pro" />
        </div>
      </label>
      <label z-field-label for="enterprise-plan">
        <div z-field zOrientation="horizontal">
          <div z-field-content>
            <div z-field-title>Enterprise</div>
            <p z-field-description>For large teams and enterprises.</p>
          </div>
          <z-radio zId="enterprise-plan" value="enterprise" />
        </div>
      </label>
    </z-radio-group>
  `,
})
export class ZardDemoRadioGroupChoiceCardComponent {
  selected: unknown = 'plus';
}

fieldset

Use FieldSet and FieldLegend to group radio items with a label and description.

Subscription Plan

Yearly and lifetime plans offer significant savings.

import { Component } from '@angular/core';
import { FormsModule } from '@angular/forms';

import { ZardFieldImports } from '@/shared/components/field/field.imports';
import { ZardRadioGroupImports } from '@/shared/components/radio-group/radio-group.imports';

@Component({
  selector: 'z-demo-radio-group-fieldset',
  imports: [...ZardRadioGroupImports, ...ZardFieldImports, FormsModule],
  template: `
    <fieldset z-field-set class="w-full max-w-xs">
      <legend z-field-legend zVariant="label">Subscription Plan</legend>
      <p z-field-description>Yearly and lifetime plans offer significant savings.</p>
      <z-radio-group [(value)]="plan">
        <div z-field zOrientation="horizontal">
          <z-radio zId="plan-monthly" value="monthly" />
          <label z-field-label for="plan-monthly" class="font-normal">Monthly ($9.99/month)</label>
        </div>
        <div z-field zOrientation="horizontal">
          <z-radio zId="plan-yearly" value="yearly" />
          <label z-field-label for="plan-yearly" class="font-normal">Yearly ($99.99/year)</label>
        </div>
        <div z-field zOrientation="horizontal">
          <z-radio zId="plan-lifetime" value="lifetime" />
          <label z-field-label for="plan-lifetime" class="font-normal">Lifetime ($299.99)</label>
        </div>
      </z-radio-group>
    </fieldset>
  `,
})
export class ZardDemoRadioGroupFieldsetComponent {
  plan: unknown = 'monthly';
}

disabled

Use the disabled prop on RadioGroupItem to disable individual items.

import { Component } from '@angular/core';
import { FormsModule } from '@angular/forms';

import { ZardFieldImports } from '@/shared/components/field/field.imports';
import { ZardRadioGroupImports } from '@/shared/components/radio-group/radio-group.imports';

@Component({
  selector: 'z-demo-radio-group-disabled',
  imports: [...ZardRadioGroupImports, ...ZardFieldImports, FormsModule],
  template: `
    <z-radio-group [(value)]="selected" class="w-fit">
      <div z-field zOrientation="horizontal" data-disabled="true">
        <z-radio zId="disabled-1" value="option1" zDisabled />
        <label z-field-label for="disabled-1" class="font-normal">Disabled</label>
      </div>
      <div z-field zOrientation="horizontal">
        <z-radio zId="disabled-2" value="option2" />
        <label z-field-label for="disabled-2" class="font-normal">Option 2</label>
      </div>
      <div z-field zOrientation="horizontal">
        <z-radio zId="disabled-3" value="option3" />
        <label z-field-label for="disabled-3" class="font-normal">Option 3</label>
      </div>
    </z-radio-group>
  `,
})
export class ZardDemoRadioGroupDisabledComponent {
  selected: unknown = 'option2';
}

invalid

Use aria-invalid on RadioGroupItem and data-invalid on Field to show validation errors.

Notification Preferences

Choose how you want to receive notifications.

import { Component } from '@angular/core';
import { FormsModule } from '@angular/forms';

import { ZardFieldImports } from '@/shared/components/field/field.imports';
import { ZardRadioGroupImports } from '@/shared/components/radio-group/radio-group.imports';

@Component({
  selector: 'z-demo-radio-group-invalid',
  imports: [...ZardRadioGroupImports, ...ZardFieldImports, FormsModule],
  template: `
    <fieldset z-field-set class="w-full max-w-xs">
      <legend z-field-legend zVariant="label">Notification Preferences</legend>
      <p z-field-description>Choose how you want to receive notifications.</p>
      <z-radio-group [(value)]="selected">
        <div z-field zOrientation="horizontal" data-invalid="true">
          <z-radio zId="invalid-email" value="email" zInvalid />
          <label z-field-label for="invalid-email" class="font-normal">Email only</label>
        </div>
        <div z-field zOrientation="horizontal" data-invalid="true">
          <z-radio zId="invalid-sms" value="sms" zInvalid />
          <label z-field-label for="invalid-sms" class="font-normal">SMS only</label>
        </div>
        <div z-field zOrientation="horizontal" data-invalid="true">
          <z-radio zId="invalid-both" value="both" zInvalid />
          <label z-field-label for="invalid-both" class="font-normal">Both Email & SMS</label>
        </div>
      </z-radio-group>
    </fieldset>
  `,
})
export class ZardDemoRadioGroupInvalidComponent {
  selected: unknown = 'email';
}

API Reference

z-radio-groupComponent

Wrapper that groups radio items and manages the selected value.

PropertyDescriptionTypeDefault
[class] Additional CSS classes ClassValue ''
[(value)] Selected value (two-way binding) unknown null
[zDisabled] Disables every item in the group boolean false
[name] Optional name for native form submission string ''

z-radioComponent

Individual radio button. Must be a descendant of z-radio-group.

PropertyDescriptionTypeDefault
[class] Additional CSS classes ClassValue ''
[value] Item value, compared against the group value unknown null
[zDisabled] Disables this item only boolean false
[zInvalid] Invalid state (sets aria-invalid) boolean false
[zId] Item id string -
github iconwhatsapp icondiscord iconX icon

Made with in Brazil. Open source and available on GitHub .