Skip to content

Commit 74026b6

Browse files
devversionmmalerba
authored andcommitted
docs: add example for using chip-list with form control (#22814)
This clarifies how a chip-list can be used with a form control. It highlights how a chip list can be disabled. The form control is placed on the chip input is some cases while it should be on the chip list, as otherwise the disabled styling does not apply to the containing form field. Fixes #19118. (cherry picked from commit a59aaa3)
1 parent 4a0f02d commit 74026b6

File tree

5 files changed

+63
-0
lines changed

5 files changed

+63
-0
lines changed

src/components-examples/material/chips/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ ng_module(
1818
"//src/cdk/testing",
1919
"//src/cdk/testing/testbed",
2020
"//src/material/autocomplete",
21+
"//src/material/button",
2122
"//src/material/chips",
2223
"//src/material/chips/testing",
2324
"//src/material/form-field",
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
.example-chip-list {
2+
width: 100%;
3+
}
4+
5+
.example-button-container > button {
6+
margin: 0 12px;
7+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<div class="example-button-container">
2+
<button mat-raised-button (click)="formControl.disable()">Disable form control</button>
3+
<button mat-raised-button (click)="formControl.enable()">Enable form control</button>
4+
</div>
5+
6+
<p>
7+
Selected keywords: {{formControl.value}}
8+
</p>
9+
10+
<mat-form-field class="example-chip-list">
11+
<mat-label>Video keywords</mat-label>
12+
<mat-chip-list #chipList aria-label="Video keywords" multiple [formControl]="formControl">
13+
<mat-chip
14+
*ngFor="let keyword of keywords"
15+
(removed)="removeKeyword(keyword)">
16+
{{keyword}}
17+
</mat-chip>
18+
<input
19+
placeholder="New keyword..."
20+
[matChipInputFor]="chipList"
21+
(matChipInputTokenEnd)="addKeywordFromInput($event)">
22+
</mat-chip-list>
23+
</mat-form-field>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import {Component} from '@angular/core';
2+
import {FormControl} from '@angular/forms';
3+
import {MatChipInputEvent} from '@angular/material/chips';
4+
5+
/**
6+
* @title Chips with form control
7+
*/
8+
@Component({
9+
selector: 'chips-form-control-example',
10+
templateUrl: 'chips-form-control-example.html',
11+
styleUrls: ['chips-form-control-example.css'],
12+
})
13+
export class ChipsFormControlExample {
14+
keywords = new Set(['angular', 'how-to', 'tutorial']);
15+
formControl = new FormControl();
16+
17+
addKeywordFromInput(event: MatChipInputEvent) {
18+
if (event.value) {
19+
this.keywords.add(event.value);
20+
event.chipInput!.clear();
21+
}
22+
}
23+
24+
removeKeyword(keyword: string) {
25+
this.keywords.delete(keyword);
26+
}
27+
}

src/components-examples/material/chips/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import {ChipsInputExample} from './chips-input/chips-input-example';
1212
import {ChipsOverviewExample} from './chips-overview/chips-overview-example';
1313
import {ChipsStackedExample} from './chips-stacked/chips-stacked-example';
1414
import {ChipsHarnessExample} from './chips-harness/chips-harness-example';
15+
import {ChipsFormControlExample} from './chips-form-control/chips-form-control-example';
16+
import {MatButtonModule} from '@angular/material/button';
1517

1618
export {
1719
ChipsAutocompleteExample,
@@ -20,6 +22,7 @@ export {
2022
ChipsOverviewExample,
2123
ChipsStackedExample,
2224
ChipsHarnessExample,
25+
ChipsFormControlExample
2326
};
2427

2528
const EXAMPLES = [
@@ -29,13 +32,15 @@ const EXAMPLES = [
2932
ChipsOverviewExample,
3033
ChipsStackedExample,
3134
ChipsHarnessExample,
35+
ChipsFormControlExample,
3236
];
3337

3438
@NgModule({
3539
imports: [
3640
CommonModule,
3741
DragDropModule,
3842
MatAutocompleteModule,
43+
MatButtonModule,
3944
MatChipsModule,
4045
MatIconModule,
4146
MatFormFieldModule,

0 commit comments

Comments
 (0)