Skip to content

Commit b10fff4

Browse files
tinayuangaojelbourn
authored andcommitted
fix(chip-list): set key manager active index to -1 when blurred (#10335)
1 parent 7a42c35 commit b10fff4

File tree

4 files changed

+33
-12
lines changed

4 files changed

+33
-12
lines changed

src/lib/chips/chip-list.spec.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,22 @@ describe('MatChipList', () => {
169169
// It focuses the next-to-last item
170170
expect(manager.activeItemIndex).toEqual(lastIndex - 1);
171171
});
172+
173+
it('should not focus if chip list is not focused', () => {
174+
let array = chips.toArray();
175+
let midItem = array[2];
176+
177+
// Focus and blur the middle item
178+
midItem.focus();
179+
midItem._blur();
180+
181+
// Destroy the middle item
182+
testComponent.remove = 2;
183+
fixture.detectChanges();
184+
185+
// Should not have focus
186+
expect(chipListInstance._keyManager.activeItemIndex).toEqual(-1);
187+
});
172188
});
173189
});
174190

src/lib/chips/chip-list.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -518,7 +518,7 @@ export class MatChipList extends _MatChipListMixinBase implements MatFormFieldCo
518518
protected _updateFocusForDestroyedChips() {
519519
let chipsArray = this.chips;
520520

521-
if (this._lastDestroyedIndex != null && chipsArray.length > 0) {
521+
if (this._lastDestroyedIndex != null && chipsArray.length > 0 && this.focused) {
522522
// Check whether the destroyed chip was the last item
523523
const newFocusIndex = Math.min(this._lastDestroyedIndex, chipsArray.length - 1);
524524
this._keyManager.setActiveItem(newFocusIndex);
@@ -567,8 +567,6 @@ export class MatChipList extends _MatChipListMixinBase implements MatFormFieldCo
567567
if (correspondingChip) {
568568
if (isUserInput) {
569569
this._keyManager.setActiveItem(correspondingChip);
570-
} else {
571-
this._keyManager.updateActiveItem(correspondingChip);
572570
}
573571
}
574572
}
@@ -652,6 +650,7 @@ export class MatChipList extends _MatChipListMixinBase implements MatFormFieldCo
652650

653651
/** When blurred, mark the field as touched when focus moved outside the chip list. */
654652
_blur() {
653+
this._keyManager.setActiveItem(-1);
655654
if (!this.disabled) {
656655
if (this._chipInput) {
657656
// If there's a chip input, we should check whether the focus moved to chip input.

src/lib/chips/chip.spec.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,17 @@ describe('Chips', () => {
7979
expect(chipNativeElement.classList).not.toContain('mat-basic-chip');
8080
});
8181

82-
it('emits focus on click', () => {
83-
spyOn(chipInstance, 'focus').and.callThrough();
82+
it('emits focus only once for multiple clicks', () => {
83+
let counter = 0;
84+
chipInstance._onFocus.subscribe(() => {
85+
counter ++ ;
86+
});
8487

85-
chipNativeElement.click();
88+
chipNativeElement.focus();
89+
chipNativeElement.focus();
90+
fixture.detectChanges();
8691

87-
expect(chipInstance.focus).toHaveBeenCalledTimes(1);
92+
expect(counter).toBe(1);
8893
});
8994

9095
it('emits destroy on destruction', () => {

src/lib/chips/chip.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ export class MatChipTrailingIcon {}
108108
'[attr.aria-selected]': 'ariaSelected',
109109
'(click)': '_handleClick($event)',
110110
'(keydown)': '_handleKeydown($event)',
111-
'(focus)': '_hasFocus = true',
111+
'(focus)': 'focus()',
112112
'(blur)': '_blur()',
113113
},
114114
})
@@ -310,8 +310,11 @@ export class MatChip extends _MatChipMixinBase implements FocusableOption, OnDes
310310

311311
/** Allows for programmatic focusing of the chip. */
312312
focus(): void {
313-
this._elementRef.nativeElement.focus();
314-
this._onFocus.next({chip: this});
313+
if (!this._hasFocus) {
314+
this._elementRef.nativeElement.focus();
315+
this._onFocus.next({chip: this});
316+
}
317+
this._hasFocus = true;
315318
}
316319

317320
/**
@@ -335,8 +338,6 @@ export class MatChip extends _MatChipMixinBase implements FocusableOption, OnDes
335338

336339
event.preventDefault();
337340
event.stopPropagation();
338-
339-
this.focus();
340341
}
341342

342343
/** Handle custom key presses. */

0 commit comments

Comments
 (0)