Radio Groups
Import
Import TableauUiRadioGroupModule from tableau-ui-angular/radio-group. The options and the errors, tab-option and tab-error, come from TableauUiCommonModule in tableau-ui-angular/common.
Value binding
Bind [(value)], and listen to (valueChange) for the user's picks; each one here shows a snack. Each tab-option has a value:
textandhintare strings or templates;iconadds an icon, which[iconParams]styles;[disabled]keeps it from being picked.
import { ChangeDetectionStrategy, Component, inject, signal } from '@angular/core';
import { TableauUiCommonModule } from 'tableau-ui-angular/common';
import { TableauUiRadioGroupModule } from 'tableau-ui-angular/radio-group';
import { SnackService } from 'tableau-ui-angular/snack';
@Component({
selector: 'app-radio-value-example',
imports: [TableauUiCommonModule, TableauUiRadioGroupModule],
templateUrl: './radio-value.example.html',
styleUrl: './radio-value.example.scss',
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class RadioValueExampleComponent {
private readonly snacks = inject(SnackService);
protected readonly $plan = signal<number | undefined>(1);
// valueChange fires on each pick by the user, never when the app sets the value
protected changed(value: number | undefined) {
this.snacks.openSnack(`Radio group set to: ${value}`, 3000, 'info');
}
}Reactive forms
Bind [formControl] or formControlName. A tab-error in the group shows under its options. The control is touched once an option loses focus, at the second pick or on leaving the group, and each error waits for that; a disabled control disables the group. Each change here shows a snack, an error one while the value is invalid.
import { AsyncPipe } from '@angular/common';
import { ChangeDetectionStrategy, Component, inject } from '@angular/core';
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
import { ReactiveFormsModule, Validators } from '@angular/forms';
import { skip } from 'rxjs';
import { TableauUiCommonModule } from 'tableau-ui-angular/common';
import { FB, TableauUiFormModule } from 'tableau-ui-angular/form';
import { TableauUiRadioGroupModule } from 'tableau-ui-angular/radio-group';
import { SnackService } from 'tableau-ui-angular/snack';
@Component({
selector: 'app-radio-forms-example',
imports: [AsyncPipe, ReactiveFormsModule, TableauUiCommonModule, TableauUiFormModule, TableauUiRadioGroupModule],
templateUrl: './radio-forms.example.html',
styleUrl: './radio-forms.example.scss',
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class RadioFormsExampleComponent {
private readonly fb = inject(FB);
private readonly snacks = inject(SnackService);
// Only 3 is valid: 1 is below the minimum, 2 is disabled, 4 is above the maximum. Each error waits for the control
// to be touched, as formHasError does: once an option loses focus, at the second pick or on leaving the group
protected readonly size = this.fb.control<number | null>(null, [Validators.required, Validators.min(2), Validators.max(3)]);
constructor() {
// value$ gives the current value first, which it skips; by the time it emits, the meta holds the new validity
this.size.value$.pipe(skip(1), takeUntilDestroyed()).subscribe(value => {
this.snacks.openSnack(`Size set to: ${value}`, 3000, this.size.$meta().validity === 'VALID' ? 'info' : 'error');
});
}
}A disabled value
The app can set a disabled option as the value: it shows selected and greyed out.
import { ChangeDetectionStrategy, Component } from '@angular/core';
import { TableauUiCommonModule } from 'tableau-ui-angular/common';
import { TableauUiRadioGroupModule } from 'tableau-ui-angular/radio-group';
@Component({
selector: 'app-radio-disabled-example',
imports: [TableauUiCommonModule, TableauUiRadioGroupModule],
templateUrl: './radio-disabled.example.html',
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class RadioDisabledExampleComponent {}Accessibility
The group is a radiogroup of native radios; the app names it.
- Name it with
aria-labeloraria-labelledbyontab-radio-group, as the samples here do, or put it in atab-form-fieldwith atab-label. - Its
tab-errordescribes the group while it shows, througharia-describedby, and the group hasaria-invalid="true"then. Ids the app puts in the group's ownaria-describedbystay. - In a
tab-form-field, put thetab-erroron the field, not in the group: the field counts the group's own error as its own, and shows an empty one. - The arrow keys move to the next or previous option and pick it, past disabled ones; Space or Enter picks the focused option.