Skip to content

fix(chips): losing focus if active chip is deleted #11910

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
Jul 20, 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
27 changes: 24 additions & 3 deletions src/lib/chips/chip-list.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,20 @@ describe('MatChipList', () => {
manager = chipListInstance._keyManager;
});

it('should maintain focus if the active chip is deleted', () => {
const secondChip = fixture.nativeElement.querySelectorAll('.mat-chip')[1];

secondChip.focus();
fixture.detectChanges();

expect(chipListInstance.chips.toArray().findIndex(chip => chip._hasFocus)).toBe(1);

dispatchKeyboardEvent(secondChip, 'keydown', DELETE);
fixture.detectChanges();

expect(chipListInstance.chips.toArray().findIndex(chip => chip._hasFocus)).toBe(1);
});

describe('when the input has focus', () => {

it('should not focus the last chip when press DELETE', () => {
Expand Down Expand Up @@ -1073,15 +1087,22 @@ class StandardChipList {
<mat-form-field>
<mat-label>Add a chip</mat-label>
<mat-chip-list #chipList>
<mat-chip>Chip 1</mat-chip>
<mat-chip>Chip 1</mat-chip>
<mat-chip>Chip 1</mat-chip>
<mat-chip *ngFor="let chip of chips" (removed)="remove(chip)">{{chip}}</mat-chip>
</mat-chip-list>
<input name="test" [matChipInputFor]="chipList"/>
</mat-form-field>
`
})
class FormFieldChipList {
chips = ['Chip 0', 'Chip 1', 'Chip 2'];

remove(chip: string) {
const index = this.chips.indexOf(chip);

if (index > -1) {
this.chips.splice(index, 1);
}
}
}


Expand Down
10 changes: 5 additions & 5 deletions src/lib/chips/chip-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,8 +238,7 @@ export class MatChipList extends _MatChipListMixinBase implements MatFormFieldCo

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

/**
Expand Down Expand Up @@ -515,13 +514,14 @@ export class MatChipList extends _MatChipListMixinBase implements MatFormFieldCo
* one.
*/
protected _updateFocusForDestroyedChips() {
let chipsArray = this.chips;
const chipsArray = this.chips.toArray();

if (this._lastDestroyedIndex != null && chipsArray.length > 0 && this.focused) {
if (this._lastDestroyedIndex != null && chipsArray.length > 0 && (this.focused ||
(this._keyManager.activeItem && chipsArray.indexOf(this._keyManager.activeItem) === -1))) {
// Check whether the destroyed chip was the last item
const newFocusIndex = Math.min(this._lastDestroyedIndex, chipsArray.length - 1);
this._keyManager.setActiveItem(newFocusIndex);
let focusChip = this._keyManager.activeItem;
const focusChip = this._keyManager.activeItem;
// Focus the chip
if (focusChip) {
focusChip.focus();
Expand Down