@@ -1013,6 +1013,48 @@ describe('MatChipGrid', () => {
1013
1013
} ) ) ;
1014
1014
} ) ;
1015
1015
1016
+ describe ( 'chip with form control' , ( ) => {
1017
+ let fixture : ComponentFixture < ChipsFormControlUpdate > ;
1018
+ let component : ChipsFormControlUpdate ;
1019
+ let nativeInput : HTMLInputElement ;
1020
+ let nativeButton : HTMLButtonElement ;
1021
+ let nativeChipGrid : HTMLElement ;
1022
+
1023
+ beforeEach ( ( ) => {
1024
+ fixture = createComponent ( ChipsFormControlUpdate ) ;
1025
+ component = fixture . componentInstance ;
1026
+
1027
+ nativeChipGrid = fixture . debugElement . query ( By . css ( 'mat-chip-grid' ) ) ! . nativeElement ;
1028
+ nativeInput = fixture . nativeElement . querySelector ( 'input' ) ;
1029
+ nativeButton = fixture . nativeElement . querySelector ( 'button[id="save"]' ) ;
1030
+ } ) ;
1031
+
1032
+ it ( 'should update the form control value when pressed enter' , fakeAsync ( ( ) => {
1033
+ const nativeInput = fixture . nativeElement . querySelector ( 'input' ) ;
1034
+ nativeInput . value = 'hello' ;
1035
+ nativeInput . focus ( ) ;
1036
+ fixture . detectChanges ( ) ;
1037
+
1038
+ dispatchKeyboardEvent ( document . activeElement ! , 'keydown' , ENTER ) ;
1039
+ fixture . detectChanges ( ) ;
1040
+ flush ( ) ;
1041
+
1042
+ expect ( component . keywordChipControl . value ) . not . toBeNull ( ) ;
1043
+ expect ( component . keywordChipControl . value . length ) . toBe ( 1 ) ;
1044
+ expect ( nativeButton . disabled ) . toBeFalsy ( ) ;
1045
+
1046
+ nativeInput . value = 'how are you ?' ;
1047
+ nativeInput . focus ( ) ;
1048
+ fixture . detectChanges ( ) ;
1049
+
1050
+ dispatchKeyboardEvent ( document . activeElement ! , 'keydown' , ENTER ) ;
1051
+ fixture . detectChanges ( ) ;
1052
+ flush ( ) ;
1053
+
1054
+ expect ( component . keywordChipControl . value . length ) . toBe ( 2 ) ;
1055
+ } ) ) ;
1056
+ } ) ;
1057
+
1016
1058
function createComponent < T > (
1017
1059
component : Type < T > ,
1018
1060
direction : Direction = 'ltr' ,
@@ -1218,3 +1260,37 @@ class ChipGridWithRemove {
1218
1260
this . chips . splice ( event . chip . value , 1 ) ;
1219
1261
}
1220
1262
}
1263
+
1264
+ @Component ( {
1265
+ template : `
1266
+ <mat-form-field>
1267
+ <mat-label>Keywords</mat-label>
1268
+ <mat-chip-grid #chipGrid [formControl]="keywordChipControl">
1269
+ @for (keyword of keywords; track keyword) {
1270
+ <mat-chip-row>{{keyword}}</mat-chip-row>
1271
+ }
1272
+ </mat-chip-grid>
1273
+ <input placeholder="New keyword..." [matChipInputFor]="chipGrid" (matChipInputTokenEnd)="add($event)">
1274
+ </mat-form-field>
1275
+ <button id="save" [disabled]="!keywordChipControl.valid">Save</button>
1276
+ <button >Cancel</button>` ,
1277
+ standalone : false ,
1278
+ } )
1279
+ class ChipsFormControlUpdate {
1280
+ public keywords = new Array < string > ( ) ;
1281
+ public keywordChipControl = new FormControl ( ) ;
1282
+
1283
+ constructor ( ) {
1284
+ this . keywordChipControl . setValidators ( Validators . required ) ;
1285
+ }
1286
+
1287
+ public add ( event : MatChipInputEvent ) : void {
1288
+ const value = ( event . value || '' ) . trim ( ) ;
1289
+
1290
+ if ( value ) {
1291
+ this . keywords . push ( value ) ;
1292
+ }
1293
+
1294
+ event . chipInput . clear ( ) ;
1295
+ }
1296
+ }
0 commit comments