@@ -17,6 +17,7 @@ import {
17
17
dispatchFakeEvent ,
18
18
dispatchKeyboardEvent ,
19
19
dispatchMouseEvent ,
20
+ typeInElement ,
20
21
MockNgZone ,
21
22
} from '@angular/cdk/testing' ;
22
23
import {
@@ -993,6 +994,31 @@ describe('MatChipList', () => {
993
994
. not . toBeNull ( `Expected placeholder to have an asterisk, as control was required.` ) ;
994
995
} ) ;
995
996
997
+ it ( 'should keep focus on the input after adding the first chip' , fakeAsync ( ( ) => {
998
+ const nativeInput = fixture . nativeElement . querySelector ( 'input' ) ;
999
+ const chipEls = Array . from ( fixture . nativeElement . querySelectorAll ( '.mat-chip' ) ) . reverse ( ) ;
1000
+
1001
+ // Remove the chips via backspace to simulate the user removing them.
1002
+ chipEls . forEach ( ( chip : HTMLElement ) => {
1003
+ chip . focus ( ) ;
1004
+ dispatchKeyboardEvent ( chip , 'keydown' , BACKSPACE ) ;
1005
+ fixture . detectChanges ( ) ;
1006
+ tick ( ) ;
1007
+ } ) ;
1008
+
1009
+ nativeInput . focus ( ) ;
1010
+ expect ( fixture . componentInstance . foods ) . toEqual ( [ ] , 'Expected all chips to be removed.' ) ;
1011
+ expect ( document . activeElement ) . toBe ( nativeInput , 'Expected input to be focused.' ) ;
1012
+
1013
+ typeInElement ( '123' , nativeInput ) ;
1014
+ fixture . detectChanges ( ) ;
1015
+ dispatchKeyboardEvent ( nativeInput , 'keydown' , ENTER ) ;
1016
+ fixture . detectChanges ( ) ;
1017
+ tick ( ) ;
1018
+
1019
+ expect ( document . activeElement ) . toBe ( nativeInput , 'Expected input to remain focused.' ) ;
1020
+ } ) ) ;
1021
+
996
1022
describe ( 'keyboard behavior' , ( ) => {
997
1023
beforeEach ( ( ) => {
998
1024
chipListDebugElement = fixture . debugElement . query ( By . directive ( MatChipList ) ) ;
@@ -1322,7 +1348,7 @@ class MultiSelectionChipList {
1322
1348
<mat-form-field>
1323
1349
<mat-chip-list [multiple]="true"
1324
1350
placeholder="Food" [formControl]="control" [required]="isRequired" #chipList1>
1325
- <mat-chip *ngFor="let food of foods" [value]="food.value">
1351
+ <mat-chip *ngFor="let food of foods" [value]="food.value" (removed)="remove(food)" >
1326
1352
{{ food.viewValue }}
1327
1353
</mat-chip>
1328
1354
</mat-chip-list>
@@ -1369,6 +1395,14 @@ class InputChipList {
1369
1395
}
1370
1396
}
1371
1397
1398
+ remove ( food : any ) : void {
1399
+ const index = this . foods . indexOf ( food ) ;
1400
+
1401
+ if ( index > - 1 ) {
1402
+ this . foods . splice ( index , 1 ) ;
1403
+ }
1404
+ }
1405
+
1372
1406
@ViewChild ( MatChipList ) chipList : MatChipList ;
1373
1407
@ViewChildren ( MatChip ) chips : QueryList < MatChip > ;
1374
1408
}
0 commit comments