@@ -180,23 +180,23 @@ describe('MatChipGrid', () => {
180180 expect ( chipGridNativeElement . getAttribute ( 'tabindex' ) ) . toBe ( '-1' ) ;
181181 } ) ;
182182
183- // TODO: re-enable this test.
184- // it('should clear the active item in key manager when the last focused chip is destroyed', fakeAsync(() => {
185- // const fixture = createComponent(StandardChipGrid );
186- // fixture.detectChanges();
183+ it ( 'should clear the active item in key manager when the last focused chip is destroyed' , async ( ) => {
184+ const fixture = createComponent ( InputChipGrid ) ;
185+ patchElementFocus ( primaryActions [ 0 ] ) ;
186+ fixture . detectChanges ( ) ;
187187
188- // // Focus a chip
189- // chips.first.focus();
190- // fixture.detectChanges();
188+ // Focus a chip
189+ chips . first . focus ( ) ;
190+ fixture . detectChanges ( ) ;
191191
192- // // Remove ALL chips
193- // fixture.componentInstance.foods = [] ; // Clear the bound data array
194- // fixture.detectChanges();
195- // flush ();
192+ // Remove ALL chips
193+ fixture . componentInstance . foods . set ( [ ] ) ; // Clear the bound data array
194+ fixture . detectChanges ( ) ;
195+ await fixture . whenStable ( ) ;
196196
197- // // Verify key manager is reset to prevent stale references
198- // expect(chipGridInstance._keyManager.activeItemIndex).toBe(-1);
199- // }) );
197+ // Verify key manager is reset to prevent stale references
198+ expect ( ( chipGridInstance as any ) . _keyManager . activeItemIndex ) . toBe ( - 1 ) ;
199+ } ) ;
200200
201201 describe ( 'on chip destroy' , ( ) => {
202202 it ( 'should focus the next item' , ( ) => {
@@ -872,7 +872,7 @@ describe('MatChipGrid', () => {
872872 }
873873
874874 nativeInput . focus ( ) ;
875- expect ( fixture . componentInstance . foods )
875+ expect ( fixture . componentInstance . foods ( ) )
876876 . withContext ( 'Expected all chips to be removed.' )
877877 . toEqual ( [ ] ) ;
878878 expect ( document . activeElement ) . withContext ( 'Expected input to be focused.' ) . toBe ( nativeInput ) ;
@@ -1172,7 +1172,7 @@ class FormFieldChipGrid {
11721172 <mat-form-field>
11731173 <mat-label>New food...</mat-label>
11741174 <mat-chip-grid #chipGrid placeholder="Food" [formControl]="control">
1175- @for (food of foods; track food) {
1175+ @for (food of foods() ; track food) {
11761176 <mat-chip-row [value]="food.value" (removed)="remove(food)">
11771177 {{ food.viewValue }}
11781178 </mat-chip-row>
@@ -1189,7 +1189,7 @@ class FormFieldChipGrid {
11891189 changeDetection : ChangeDetectionStrategy . Eager ,
11901190} )
11911191class InputChipGrid {
1192- foods : any [ ] = [
1192+ readonly foods = signal ( [
11931193 { value : 'steak-0' , viewValue : 'Steak' } ,
11941194 { value : 'pizza-1' , viewValue : 'Pizza' } ,
11951195 { value : 'tacos-2' , viewValue : 'Tacos' , disabled : true } ,
@@ -1198,7 +1198,7 @@ class InputChipGrid {
11981198 { value : 'eggs-5' , viewValue : 'Eggs' } ,
11991199 { value : 'pasta-6' , viewValue : 'Pasta' } ,
12001200 { value : 'sushi-7' , viewValue : 'Sushi' } ,
1201- ] ;
1201+ ] ) ;
12021202 control = new FormControl < string | null > ( null ) ;
12031203
12041204 separatorKeyCodes = [ ENTER , SPACE ] ;
@@ -1209,21 +1209,28 @@ class InputChipGrid {
12091209
12101210 // Add our foods
12111211 if ( value ) {
1212- this . foods . push ( {
1213- value : `${ value . toLowerCase ( ) } -${ this . foods . length } ` ,
1214- viewValue : value ,
1215- } ) ;
1212+ this . foods . update ( current => [
1213+ ...current ,
1214+ {
1215+ value : `${ value . toLowerCase ( ) } -${ current . length } ` ,
1216+ viewValue : value ,
1217+ } ,
1218+ ] ) ;
12161219 }
12171220
12181221 // Reset the input value
12191222 event . chipInput ! . clear ( ) ;
12201223 }
12211224
12221225 remove ( food : any ) : void {
1223- const index = this . foods . indexOf ( food ) ;
1226+ const index = this . foods ( ) . indexOf ( food ) ;
12241227
12251228 if ( index > - 1 ) {
1226- this . foods . splice ( index , 1 ) ;
1229+ this . foods . update ( current => {
1230+ const newValue = current . slice ( ) ;
1231+ newValue . splice ( index , 1 ) ;
1232+ return newValue ;
1233+ } ) ;
12271234 }
12281235 }
12291236
0 commit comments