Skip to content

Commit

Permalink
fix(chip-list): set key manager active index to -1 when blurred (#10335)
Browse files Browse the repository at this point in the history
  • Loading branch information
tinayuangao authored and jelbourn committed Mar 9, 2018
1 parent 7a42c35 commit b10fff4
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 12 deletions.
16 changes: 16 additions & 0 deletions src/lib/chips/chip-list.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,22 @@ describe('MatChipList', () => {
// It focuses the next-to-last item
expect(manager.activeItemIndex).toEqual(lastIndex - 1);
});

it('should not focus if chip list is not focused', () => {
let array = chips.toArray();
let midItem = array[2];

// Focus and blur the middle item
midItem.focus();
midItem._blur();

// Destroy the middle item
testComponent.remove = 2;
fixture.detectChanges();

// Should not have focus
expect(chipListInstance._keyManager.activeItemIndex).toEqual(-1);
});
});
});

Expand Down
5 changes: 2 additions & 3 deletions src/lib/chips/chip-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,7 @@ export class MatChipList extends _MatChipListMixinBase implements MatFormFieldCo
protected _updateFocusForDestroyedChips() {
let chipsArray = this.chips;

if (this._lastDestroyedIndex != null && chipsArray.length > 0) {
if (this._lastDestroyedIndex != null && chipsArray.length > 0 && this.focused) {
// Check whether the destroyed chip was the last item
const newFocusIndex = Math.min(this._lastDestroyedIndex, chipsArray.length - 1);
this._keyManager.setActiveItem(newFocusIndex);
Expand Down Expand Up @@ -567,8 +567,6 @@ export class MatChipList extends _MatChipListMixinBase implements MatFormFieldCo
if (correspondingChip) {
if (isUserInput) {
this._keyManager.setActiveItem(correspondingChip);
} else {
this._keyManager.updateActiveItem(correspondingChip);
}
}
}
Expand Down Expand Up @@ -652,6 +650,7 @@ export class MatChipList extends _MatChipListMixinBase implements MatFormFieldCo

/** When blurred, mark the field as touched when focus moved outside the chip list. */
_blur() {
this._keyManager.setActiveItem(-1);
if (!this.disabled) {
if (this._chipInput) {
// If there's a chip input, we should check whether the focus moved to chip input.
Expand Down
13 changes: 9 additions & 4 deletions src/lib/chips/chip.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,17 @@ describe('Chips', () => {
expect(chipNativeElement.classList).not.toContain('mat-basic-chip');
});

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

chipNativeElement.click();
chipNativeElement.focus();
chipNativeElement.focus();
fixture.detectChanges();

expect(chipInstance.focus).toHaveBeenCalledTimes(1);
expect(counter).toBe(1);
});

it('emits destroy on destruction', () => {
Expand Down
11 changes: 6 additions & 5 deletions src/lib/chips/chip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ export class MatChipTrailingIcon {}
'[attr.aria-selected]': 'ariaSelected',
'(click)': '_handleClick($event)',
'(keydown)': '_handleKeydown($event)',
'(focus)': '_hasFocus = true',
'(focus)': 'focus()',
'(blur)': '_blur()',
},
})
Expand Down Expand Up @@ -310,8 +310,11 @@ export class MatChip extends _MatChipMixinBase implements FocusableOption, OnDes

/** Allows for programmatic focusing of the chip. */
focus(): void {
this._elementRef.nativeElement.focus();
this._onFocus.next({chip: this});
if (!this._hasFocus) {
this._elementRef.nativeElement.focus();
this._onFocus.next({chip: this});
}
this._hasFocus = true;
}

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

event.preventDefault();
event.stopPropagation();

this.focus();
}

/** Handle custom key presses. */
Expand Down

0 comments on commit b10fff4

Please sign in to comment.