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