Chips
Import
Import TableauUiChipModule from tableau-ui-angular/chip, and TableauUiChipControlModule and the ChipData type from tableau-ui-angular/chip-control.
Chips
tab-chip shows its content. [selected] highlights it, and its font size sets its size. [allowRemove] adds a remove button, with [removeTooltip] for its tooltip. The button emits (removed), and the app takes the chip away.
import { ChangeDetectionStrategy, Component, signal } from '@angular/core';
import { TableauUiButtonModule } from 'tableau-ui-angular/button';
import { TableauUiChipModule } from 'tableau-ui-angular/chip';
import { TableauUiIconModule } from 'tableau-ui-angular/icon';
import type { TooltipArgs } from 'tableau-ui-angular/tooltip';
@Component({
selector: 'app-chip-basic-example',
imports: [TableauUiButtonModule, TableauUiChipModule, TableauUiIconModule],
templateUrl: './chip-basic.example.html',
styleUrl: './chip-basic.example.scss',
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class ChipBasicExampleComponent {
// A chip's remove button only emits removed: the app takes the chip away
protected readonly $removable = signal(true);
protected readonly $withTooltip = signal(true);
// The remove button's tooltip: a text, or a template with its context
protected readonly removeTooltip: TooltipArgs<undefined> = { template: 'Remove this chip', context: undefined };
protected reset() {
this.$removable.set(true);
this.$withTooltip.set(true);
}
}Chip control
tab-chip-control shows a chip for each key, drawn by the template marked tabChipTemplate. The template's context has the chip's data, whether it is selected, removable or disabled, and the control, whose remove() takes a chip away.
| Input | What it does |
|---|---|
[(keys)] | The chips' keys, which identify them |
[(values)] | Each chip's key and the value it shows, as ChipData; setting the values sets the keys, and the other way round |
getValuesByKeys | Required. Looks up the values of keys set without them; a spinner shows until it answers |
[disabled] | Disables the control and its chips |
From the control's text box, Backspace and the left arrow select a chip, and Backspace or Delete removes it.
import { ChangeDetectionStrategy, Component, computed, signal } from '@angular/core';
import { TableauUiButtonModule } from 'tableau-ui-angular/button';
import { TableauUiCheckboxModule } from 'tableau-ui-angular/checkbox';
import { TableauUiChipModule } from 'tableau-ui-angular/chip';
import type { ChipData } from 'tableau-ui-angular/chip-control';
import { TableauUiChipControlModule } from 'tableau-ui-angular/chip-control';
import { TableauUiCommonModule } from 'tableau-ui-angular/common';
import { TableauUiFormFieldModule } from 'tableau-ui-angular/form-field';
interface City {
id: number;
name: string;
}
const CITIES: readonly City[] = ['Amsterdam', 'Berlin', 'Budapest', 'Copenhagen', 'Dublin', 'Helsinki', 'Lisbon', 'Madrid', 'Oslo', 'Paris', 'Prague', 'Vienna'].map((name, index) => ({
id: index + 1,
name,
}));
@Component({
selector: 'app-chip-control-example',
imports: [TableauUiButtonModule, TableauUiCheckboxModule, TableauUiChipModule, TableauUiChipControlModule, TableauUiCommonModule, TableauUiFormFieldModule],
templateUrl: './chip-control.example.html',
styleUrl: './chip-control.example.scss',
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class ChipControlExampleComponent {
// The chips by key, and by key and value: setting either one sets the other
protected readonly $keys = signal<number[]>([]);
protected readonly $values = signal<ChipData<number, City>[]>([]);
protected readonly $disabled = signal(false);
protected readonly $keysText = computed(() => this.$keys().join(', ') || 'none');
// What a key shows, for keys set without their value: a lookup, as a server call would be. The chips wait for it
protected readonly getValuesByKeys = async (keys: number[]): Promise<ChipData<number, City>[]> => {
await new Promise(resolve => setTimeout(resolve, 1500));
return CITIES.filter(city => keys.includes(city.id)).map(city => ({ key: city.id, value: city }));
};
protected addByKey() {
const keys = this.nextCities().map(city => city.id);
this.$keys.update(current => [...current, ...keys]);
}
protected addByValue() {
const values = this.nextCities().map(city => ({ key: city.id, value: city }));
this.$values.update(current => [...current, ...values]);
}
protected removeFirstByKey() {
this.$keys.update(current => current.slice(1));
}
protected removeLastByValue() {
this.$values.update(current => current.slice(0, -1));
}
// The next two cities without a chip
private nextCities() {
const keys = this.$keys();
return CITIES.filter(city => !keys.includes(city.id)).slice(0, 2);
}
}Reactive forms
Bind [formControl] or formControlName: the form's value is the keys, and the control looks up what each one shows. The control is touched when its text box loses focus, and a disabled control disables it.
import { ChangeDetectionStrategy, Component, computed, inject } from '@angular/core';
import { Validators } from '@angular/forms';
import { TableauUiButtonModule } from 'tableau-ui-angular/button';
import { TableauUiCheckboxModule } from 'tableau-ui-angular/checkbox';
import { TableauUiChipModule } from 'tableau-ui-angular/chip';
import type { ChipData } from 'tableau-ui-angular/chip-control';
import { TableauUiChipControlModule } from 'tableau-ui-angular/chip-control';
import { TableauUiCommonModule } from 'tableau-ui-angular/common';
import { FB, TableauUiFormModule } from 'tableau-ui-angular/form';
import { TableauUiFormFieldModule } from 'tableau-ui-angular/form-field';
interface City {
id: number;
name: string;
}
const CITIES: readonly City[] = ['Amsterdam', 'Berlin', 'Budapest', 'Copenhagen', 'Dublin', 'Helsinki', 'Lisbon', 'Madrid', 'Oslo', 'Paris', 'Prague', 'Vienna'].map((name, index) => ({
id: index + 1,
name,
}));
@Component({
selector: 'app-chip-forms-example',
imports: [TableauUiButtonModule, TableauUiCheckboxModule, TableauUiChipModule, TableauUiChipControlModule, TableauUiCommonModule, TableauUiFormModule, TableauUiFormFieldModule],
templateUrl: './chip-forms.example.html',
styleUrl: './chip-forms.example.scss',
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class ChipFormsExampleComponent {
private readonly fb = inject(FB);
// The form holds the keys. At least one chip and at most five: each error waits for the control to be touched, as
// formHasError does, once its text box loses focus
protected readonly cities = this.fb.control<number[]>([1, 2], [Validators.required, Validators.maxLength(5)]);
protected readonly $keysText = computed(() => this.cities.$value().join(', '));
// What each key shows, which the control looks up for the keys the form gives it
protected readonly getValuesByKeys = async (keys: number[]): Promise<ChipData<number, City>[]> => {
await new Promise(resolve => setTimeout(resolve, 1500));
return CITIES.filter(city => keys.includes(city.id)).map(city => ({ key: city.id, value: city }));
};
protected addTwo() {
const keys = this.cities.$value();
const next = CITIES.filter(city => !keys.includes(city.id)).slice(0, 2);
this.cities.setValue([...keys, ...next.map(city => city.id)]);
}
protected removeFirst() {
this.cities.setValue(this.cities.$value().slice(1));
}
// A disabled control disables the chip control
protected setDisabled(disabled: boolean | 'partial') {
if (disabled === true) {
this.cities.metaFn.disable();
} else {
this.cities.metaFn.enable();
}
}
}Accessibility
- A chip's remove button is named Remove and the chip's content, such as Remove Berlin. Its content's decorative icons are left out.
- In the chip control, focus stays in the text box. Backspace and the left arrow highlight a chip, the left and right arrows move the highlight, and Backspace or Delete removes the highlighted chip.
- A hidden polite live region in the control says what those keys do:
Berlin, 2 of 3, highlighted. Backspace or Delete removes it
when a key highlights a chip, with its place among the chips, andBerlin removed
when one removes it. A chip's words are its text and an image's alt, without decorative icons or its buttons, as in its remove button's name. - The app names the control: a
tab-labelin itstab-form-fieldnames its text box.