RadioButton

RadioButton is an extension to standard radio button element with theming.


import { RadioButtonModule } from 'primeng/radiobutton';

RadioButton is used as a controlled input with value and ngModel properties.


<div class="flex flex-wrap gap-3">
    <div class="flex align-items-center">
        <p-radioButton name="pizza" value="Cheese" [(ngModel)]="ingredient" inputId="ingredient1"></p-radioButton>
        <label for="ingredient1" class="ml-2">Cheese</label>
    </div>
    
    <div class="flex align-items-center">
        <p-radioButton name="pizza" value="Mushroom" [(ngModel)]="ingredient" inputId="ingredient2"></p-radioButton>
        <label for="ingredient2" class="ml-2">Mushroom</label>
    </div>
    
    <div class="flex align-items-center">
        <p-radioButton name="pizza" value="Pepper" [(ngModel)]="ingredient" inputId="ingredient3"></p-radioButton>
        <label for="ingredient3" class="ml-2">Pepper</label>
    </div>

    <div class="flex align-items-center">
        <p-radioButton name="pizza" value="Onion" [(ngModel)]="ingredient" inputId="ingredient4"></p-radioButton>
        <label for="ingredient4" class="ml-2">Onion</label>
    </div>
</div>

RadioButtons can be generated using a list of values.


<div class="flex flex-column gap-3">
    <div *ngFor="let category of categories" class="field-checkbox">
        <p-radioButton [inputId]="category.key" name="category" [value]="category" [(ngModel)]="selectedCategory"></p-radioButton>
        <label [for]="category.key" class="ml-2">{{ category.name }}</label>
    </div>
</div>

RadioButton can also be used with reactive forms. In this case, the formControlName property is used to bind the component to a form control.


<form class="flex flex-column gap-3" [formGroup]="formGroup">
    <div *ngFor="let category of categories" class="field-checkbox">
        <p-radioButton [inputId]="category.key" [value]="category" formControlName="selectedCategory"></p-radioButton>
        <label [for]="category.key" class="ml-2">{{ category.name }}</label>
    </div>
</form>

Invalid state style is added using the ng-invalid and ng-dirty class to indicate a failed validation.


<p-radioButton class="ng-invalid ng-dirty"></p-radioButton>

When disabled is present, the element cannot be edited and focused.


<p-radioButton [disabled]="true"></p-radioButton>

Following is the list of structural style classes, for theming classes visit theming page.

NameElement
p-radiobuttonContainer element
p-radiobutton-boxContainer of icon.
p-radiobutton-iconIcon element.
p-checkbox-labelLabel element.
p-label-activeLabel element of a checked state.
p-label-focusLabel element of a focused state.
p-label-disabledLabel element of a disabled state.

Screen Reader

RadioButton component uses a hidden native radio button element internally that is only visible to screen readers. Value to describe the component can either be provided via label tag combined with inputId prop or using ariaLabelledBy, ariaLabel props.


<label for="rb1">One</label>
<p-radioButton inputId="rb1"></p-radioButton>

<span id="rb2">Two</span>
<p-radioButton ariaLabelledBy="rb2"></p-radioButton>

<p-radioButton ariaLabel="Three"></p-radioButton>

Keyboard Support

KeyFunction
tabMoves focus to the checked radio button, if there is none within the group then first radio button receives the focus.
left arrowup arrowMoves focus to the previous radio button, if there is none then last radio button receives the focus.
right arrowdown arrowMoves focus to the next radio button, if there is none then first radio button receives the focus.
spaceIf the focused radio button is unchecked, changes the state to checked.