Skip to content

fix(chip-list): set key manager active index to -1 when blurred #10335

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -521,7 +521,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 @@ -570,8 +570,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 @@ -655,6 +653,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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unit test?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added unit tests and fixed chip emits multiple _onFocus events

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