Tableau UI for Angular
Checkbox
A checkbox selects or clears one option, with a value binding or a form control.
Import
Import TableauUiCheckboxModule from tableau-ui-angular/checkbox. Hints and errors, tab-hint and tab-error, come from TableauUiCommonModule in tableau-ui-angular/common.
Value binding
Bind [(value)], and listen to (valueChange) for the user's toggles; each one here shows a snack. [loading] shows a spinner in place of the box.
A hint for the value above
It shows a spinner for two seconds
import { ChangeDetectionStrategy, Component, inject, signal } from '@angular/core';
import { TableauUiCheckboxModule } from 'tableau-ui-angular/checkbox';
import { TableauUiCommonModule } from 'tableau-ui-angular/common';
import { SnackService } from 'tableau-ui-angular/snack';
@Component({
selector: 'app-checkbox-value-example',
imports: [TableauUiCheckboxModule, TableauUiCommonModule],
templateUrl: './checkbox-value.example.html',
styleUrl: './checkbox-value.example.scss',
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class CheckboxValueExampleComponent {
private readonly snacks = inject(SnackService);
protected readonly $simple = signal(true);
protected readonly $withHint = signal(false);
protected readonly $loadingValue = signal(false);
protected readonly $loading = signal(false);
// valueChange fires on each toggle by the user, never when the app sets the value
protected changed(name: string, value: boolean | 'partial') {
this.snacks.openSnack(`${name} set to: ${value}`, 3000, 'info');
}
// Shows the checkbox loading for two seconds after each toggle
protected load(value: boolean | 'partial') {
this.changed('Loading checkbox', value);
this.$loading.set(true);
setTimeout(() => {
this.$loading.set(false);
}, 2000);
}
}Partial
The value 'partial' comes only from code. A click leaves it for valueAfterPartial, false by default.
Only code sets partial; a click sets it to false
Only code sets partial; a click sets it to true
import { ChangeDetectionStrategy, Component, inject, signal } from '@angular/core';
import { TableauUiButtonModule } from 'tableau-ui-angular/button';
import { TableauUiCheckboxModule } from 'tableau-ui-angular/checkbox';
import { TableauUiCommonModule } from 'tableau-ui-angular/common';
import { SnackService } from 'tableau-ui-angular/snack';
@Component({
selector: 'app-checkbox-partial-example',
imports: [TableauUiButtonModule, TableauUiCheckboxModule, TableauUiCommonModule],
templateUrl: './checkbox-partial.example.html',
styleUrl: './checkbox-partial.example.scss',
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class CheckboxPartialExampleComponent {
private readonly snacks = inject(SnackService);
// Only the app sets 'partial': a click goes to valueAfterPartial, false unless it says true
protected readonly $toFalse = signal<boolean | 'partial'>('partial');
protected readonly $toTrue = signal<boolean | 'partial'>('partial');
protected changed(name: string, value: boolean | 'partial') {
this.snacks.openSnack(`${name} set to: ${value}`, 3000, 'info');
}
}Reactive forms
Bind formControlName or [formControl]. The control is touched when the user leaves the checkbox, and a disabled control disables it.
import { ChangeDetectionStrategy, Component, inject } from '@angular/core';
import { FormControl, FormGroup, ReactiveFormsModule, Validators } from '@angular/forms';
import { TableauUiCheckboxModule } from 'tableau-ui-angular/checkbox';
import { TableauUiCommonModule } from 'tableau-ui-angular/common';
import { SnackService } from 'tableau-ui-angular/snack';
@Component({
selector: 'app-checkbox-forms-example',
imports: [ReactiveFormsModule, TableauUiCheckboxModule, TableauUiCommonModule],
templateUrl: './checkbox-forms.example.html',
styleUrl: './checkbox-forms.example.scss',
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class CheckboxFormsExampleComponent {
private readonly snacks = inject(SnackService);
protected readonly form = new FormGroup({
byName: new FormControl(false),
byControl: new FormControl(true),
disabled: new FormControl({ value: true, disabled: true }),
required: new FormControl(false, Validators.requiredTrue),
disabledRequired: new FormControl({ value: true, disabled: true }, Validators.requiredTrue),
});
// valueChange fires before the form has the new value, so the form's state is read from the value itself
protected changed(name: string, value: boolean | 'partial', invalid = false) {
this.snacks.openSnack(`${name} set to: ${value}`, 3000, invalid ? 'error' : 'info');
}
}Accessibility
The checkbox is a native checkbox input, named by its text; the app adds nothing.
- Its
tab-hintdescribes it, and itstab-errorin the hint's place while the error shows, througharia-describedby. - It has
aria-invalid="true"while the error shows, and'partial'reads as mixed. - Space toggles it, and Enter too.