RadioButton is an extension to standard radio button element with theming.
import { RadioButton } from 'primeng/radiobutton';
RadioButton is used as a controlled input with value and ngModel properties.
<div class="flex flex-wrap gap-4">
<div class="flex items-center">
<p-radiobutton name="pizza" value="Cheese" [(ngModel)]="ingredient" inputId="ingredient1" />
<label for="ingredient1" class="ml-2">Cheese</label>
</div>
<div class="flex items-center">
<p-radiobutton name="pizza" value="Mushroom" [(ngModel)]="ingredient" inputId="ingredient2" />
<label for="ingredient2" class="ml-2">Mushroom</label>
</div>
<div class="flex items-center">
<p-radiobutton name="pizza" value="Pepper" [(ngModel)]="ingredient" inputId="ingredient3" />
<label for="ingredient3" class="ml-2">Pepper</label>
</div>
<div class="flex items-center">
<p-radiobutton name="pizza" value="Onion" [(ngModel)]="ingredient" inputId="ingredient4" />
<label for="ingredient4" class="ml-2">Onion</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-col gap-4" [formGroup]="formGroup">
<div *ngFor="let category of categories" class="field-checkbox">
<p-radiobutton [inputId]="category.key" [value]="category" formControlName="selectedCategory" />
<label [for]="category.key" class="ml-2">{{ category.name }}</label>
</div>
</form>
RadioButtons can be generated using a list of values.
<div class="flex flex-col gap-4">
<div *ngFor="let category of categories" class="field-checkbox">
<p-radiobutton [inputId]="category.key"name="category" [value]="category" [(ngModel)]="selectedCategory" />
<label [for]="category.key" class="ml-2">{{ category.name }}</label>
</div>
</div>
Specify the variant property as filled to display the component with a higher visual emphasis than the default outlined style.
<p-radiobutton [(ngModel)]="checked" variant="filled" />
RadioButton provides small and large sizes as alternatives to the base.
<div class="flex flex-wrap gap-4">
<div class="flex items-center gap-2">
<p-radiobutton [(ngModel)]="size" inputId="size_small" name="size" value="Small" size="small" />
<label for="size_small" class="text-sm">Small</label>
</div>
<div class="flex items-center gap-2">
<p-radiobutton [(ngModel)]="size" inputId="size_normal" name="size" value="Normal" />
<label for="size_normal">Normal</label>
</div>
<div class="flex items-center gap-2">
<p-radiobutton [(ngModel)]="size" inputId="size_large" name="size" value="Large" size="large" />
<label for="size_large" class="text-lg">Large</label>
</div>
</div>
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" />
When disabled is present, the element cannot be edited and focused.
<p-radiobutton [(ngModel)]="value" [value]="1" [disabled]="true" />
<p-radiobutton [(ngModel)]="value" [value]="2" [disabled]="true" />
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" />
<span id="rb2">Two</span>
<p-radiobutton ariaLabelledBy="rb2" />
<p-radiobutton ariaLabel="Three" />
Key | Function |
---|---|
tab | Moves focus to the checked radio button, if there is none within the group then first radio button receives the focus. |
left arrowup arrow | Moves focus to the previous radio button, if there is none then last radio button receives the focus. |
right arrowdown arrow | Moves focus to the next radio button, if there is none then first radio button receives the focus. |
space | If the focused radio button is unchecked, changes the state to checked. |