@@ -1026,6 +1026,29 @@ describe('MatChipGrid', () => {
1026
1026
} ) ) ;
1027
1027
} ) ;
1028
1028
1029
+ it ( 'should update the form control immediately when remove button is clicked' , fakeAsync ( ( ) => {
1030
+ const fixture = createComponent ( ChipGridWithRemoveAndFormControl , undefined , [
1031
+ NoopAnimationsModule ,
1032
+ ] ) ;
1033
+
1034
+ const component = fixture . componentRef . instance ;
1035
+
1036
+ flush ( ) ;
1037
+ const trailingActions = chipGridNativeElement . querySelectorAll < HTMLElement > (
1038
+ '.mdc-evolution-chip__action--secondary' ,
1039
+ ) ;
1040
+ const chip = chips . get ( 2 ) ! ;
1041
+ chip . focus ( ) ;
1042
+ fixture . detectChanges ( ) ;
1043
+
1044
+ trailingActions [ 2 ] . click ( ) ;
1045
+ fixture . detectChanges ( ) ;
1046
+ flush ( ) ;
1047
+
1048
+ expect ( component . formControl . value ?. length ) . toBe ( 3 ) ;
1049
+ expect ( component . formControl . value ?. indexOf ( 'tutorial' ) ) . toBe ( - 1 ) ;
1050
+ } ) ) ;
1051
+
1029
1052
function createComponent < T > (
1030
1053
component : Type < T > ,
1031
1054
direction : Direction = 'ltr' ,
@@ -1234,3 +1257,50 @@ class ChipGridWithRemove {
1234
1257
this . chips . splice ( event . chip . value , 1 ) ;
1235
1258
}
1236
1259
}
1260
+
1261
+ @Component ( {
1262
+ template : `
1263
+ <mat-form-field>
1264
+ <mat-chip-grid #chipGrid [formControl]="formControl">
1265
+ @for (keyword of keywords(); track keyword) {
1266
+ <mat-chip-row (removed)="removeKeyword(keyword)">
1267
+ {{keyword}}
1268
+ <span matChipRemove>Remove</span>
1269
+ </mat-chip-row>
1270
+ }
1271
+ </mat-chip-grid>
1272
+ <input
1273
+ placeholder="New keyword..."
1274
+ [matChipInputFor]="chipGrid"
1275
+ />
1276
+ </mat-form-field>
1277
+ ` ,
1278
+ imports : [
1279
+ MatChipGrid ,
1280
+ MatChipRow ,
1281
+ MatChipInput ,
1282
+ MatFormField ,
1283
+ MatChipRemove ,
1284
+ ReactiveFormsModule ,
1285
+ ] ,
1286
+ } )
1287
+ class ChipGridWithRemoveAndFormControl {
1288
+ readonly keywords = signal ( [ 'angular' , 'how-to' , 'tutorial' , 'accessibility' ] ) ;
1289
+ readonly formControl = new FormControl ( [ ...this . keywords ( ) ] ) ;
1290
+
1291
+ constructor ( ) {
1292
+ this . formControl . setValidators ( Validators . required ) ;
1293
+ }
1294
+
1295
+ removeKeyword ( keyword : string ) {
1296
+ this . keywords . update ( keywords => {
1297
+ const index = keywords . indexOf ( keyword ) ;
1298
+ if ( index < 0 ) {
1299
+ return keywords ;
1300
+ }
1301
+
1302
+ keywords . splice ( index , 1 ) ;
1303
+ return [ ...keywords ] ;
1304
+ } ) ;
1305
+ }
1306
+ }
0 commit comments