Skip to content

Commit e13bfe0

Browse files
crisbetojosephperrott
authored andcommitted
fix(chips): losing focus if active chip is deleted (#11910)
1 parent 275de51 commit e13bfe0

File tree

2 files changed

+29
-8
lines changed

2 files changed

+29
-8
lines changed

Diff for: src/lib/chips/chip-list.spec.ts

+24-3
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,20 @@ describe('MatChipList', () => {
320320
manager = chipListInstance._keyManager;
321321
});
322322

323+
it('should maintain focus if the active chip is deleted', () => {
324+
const secondChip = fixture.nativeElement.querySelectorAll('.mat-chip')[1];
325+
326+
secondChip.focus();
327+
fixture.detectChanges();
328+
329+
expect(chipListInstance.chips.toArray().findIndex(chip => chip._hasFocus)).toBe(1);
330+
331+
dispatchKeyboardEvent(secondChip, 'keydown', DELETE);
332+
fixture.detectChanges();
333+
334+
expect(chipListInstance.chips.toArray().findIndex(chip => chip._hasFocus)).toBe(1);
335+
});
336+
323337
describe('when the input has focus', () => {
324338

325339
it('should not focus the last chip when press DELETE', () => {
@@ -1073,15 +1087,22 @@ class StandardChipList {
10731087
<mat-form-field>
10741088
<mat-label>Add a chip</mat-label>
10751089
<mat-chip-list #chipList>
1076-
<mat-chip>Chip 1</mat-chip>
1077-
<mat-chip>Chip 1</mat-chip>
1078-
<mat-chip>Chip 1</mat-chip>
1090+
<mat-chip *ngFor="let chip of chips" (removed)="remove(chip)">{{chip}}</mat-chip>
10791091
</mat-chip-list>
10801092
<input name="test" [matChipInputFor]="chipList"/>
10811093
</mat-form-field>
10821094
`
10831095
})
10841096
class FormFieldChipList {
1097+
chips = ['Chip 0', 'Chip 1', 'Chip 2'];
1098+
1099+
remove(chip: string) {
1100+
const index = this.chips.indexOf(chip);
1101+
1102+
if (index > -1) {
1103+
this.chips.splice(index, 1);
1104+
}
1105+
}
10851106
}
10861107

10871108

Diff for: src/lib/chips/chip-list.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -238,8 +238,7 @@ export class MatChipList extends _MatChipListMixinBase implements MatFormFieldCo
238238

239239
/** Whether any chips or the matChipInput inside of this chip-list has focus. */
240240
get focused(): boolean {
241-
return this.chips.some(chip => chip._hasFocus) ||
242-
(this._chipInput && this._chipInput.focused);
241+
return (this._chipInput && this._chipInput.focused) || this.chips.some(chip => chip._hasFocus);
243242
}
244243

245244
/**
@@ -515,13 +514,14 @@ export class MatChipList extends _MatChipListMixinBase implements MatFormFieldCo
515514
* one.
516515
*/
517516
protected _updateFocusForDestroyedChips() {
518-
let chipsArray = this.chips;
517+
const chipsArray = this.chips.toArray();
519518

520-
if (this._lastDestroyedIndex != null && chipsArray.length > 0 && this.focused) {
519+
if (this._lastDestroyedIndex != null && chipsArray.length > 0 && (this.focused ||
520+
(this._keyManager.activeItem && chipsArray.indexOf(this._keyManager.activeItem) === -1))) {
521521
// Check whether the destroyed chip was the last item
522522
const newFocusIndex = Math.min(this._lastDestroyedIndex, chipsArray.length - 1);
523523
this._keyManager.setActiveItem(newFocusIndex);
524-
let focusChip = this._keyManager.activeItem;
524+
const focusChip = this._keyManager.activeItem;
525525
// Focus the chip
526526
if (focusChip) {
527527
focusChip.focus();

0 commit comments

Comments
 (0)