Skip to content

Commit f166468

Browse files
tinayuangaoandrewseguin
authored andcommitted
fix(chips): fix chip list focus and keyboard behaviors (#7319)
1 parent 6b70ca6 commit f166468

File tree

2 files changed

+9
-11
lines changed

2 files changed

+9
-11
lines changed

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ describe('MatChipList', () => {
279279

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

282-
it('should focus the last chip when press DELETE', () => {
282+
it('should not focus the last chip when press DELETE', () => {
283283
let nativeInput = fixture.nativeElement.querySelector('input');
284284
let DELETE_EVENT: KeyboardEvent =
285285
createKeyboardEvent('keydown', DELETE, nativeInput);
@@ -292,8 +292,8 @@ describe('MatChipList', () => {
292292
chipListInstance._keydown(DELETE_EVENT);
293293
fixture.detectChanges();
294294

295-
// It focuses the last chip
296-
expect(manager.activeItemIndex).toEqual(chips.length - 1);
295+
// It doesn't focus the last chip
296+
expect(manager.activeItemIndex).toEqual(-1);
297297
});
298298

299299
it('should focus the last chip when press BACKSPACE', () => {
@@ -786,7 +786,7 @@ describe('MatChipList', () => {
786786

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

789-
it('should focus the last chip when press DELETE', () => {
789+
it('should not focus the last chip when press DELETE', () => {
790790
let nativeInput = fixture.nativeElement.querySelector('input');
791791
let DELETE_EVENT: KeyboardEvent =
792792
createKeyboardEvent('keydown', DELETE, nativeInput);
@@ -799,8 +799,8 @@ describe('MatChipList', () => {
799799
chipListInstance._keydown(DELETE_EVENT);
800800
fixture.detectChanges();
801801

802-
// It focuses the last chip
803-
expect(manager.activeItemIndex).toEqual(chips.length - 1);
802+
// It doesn't focus the last chip
803+
expect(manager.activeItemIndex).toEqual(-1);
804804
});
805805

806806
it('should focus the last chip when press BACKSPACE', () => {

src/lib/chips/chip-list.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {FocusKeyManager} from '@angular/cdk/a11y';
1010
import {Directionality} from '@angular/cdk/bidi';
1111
import {coerceBooleanProperty} from '@angular/cdk/coercion';
1212
import {SelectionModel} from '@angular/cdk/collections';
13-
import {BACKSPACE, DELETE, LEFT_ARROW, RIGHT_ARROW, UP_ARROW} from '@angular/cdk/keycodes';
13+
import {BACKSPACE, LEFT_ARROW, RIGHT_ARROW} from '@angular/cdk/keycodes';
1414
import {startWith} from '@angular/cdk/rxjs';
1515
import {
1616
AfterContentInit,
@@ -432,8 +432,8 @@ export class MatChipList implements MatFormFieldControl<any>, ControlValueAccess
432432

433433
let isPrevKey = (code === (isRtl ? RIGHT_ARROW : LEFT_ARROW));
434434
let isNextKey = (code === (isRtl ? LEFT_ARROW : RIGHT_ARROW));
435-
let isBackKey = (code === BACKSPACE || code == DELETE || code == UP_ARROW || isPrevKey);
436-
// If they are on an empty input and hit backspace/delete/left arrow, focus the last chip
435+
let isBackKey = code === BACKSPACE;
436+
// If they are on an empty input and hit backspace, focus the last chip
437437
if (isInputEmpty && isBackKey) {
438438
this._keyManager.setLastItemActive();
439439
event.preventDefault();
@@ -504,8 +504,6 @@ export class MatChipList implements MatFormFieldControl<any>, ControlValueAccess
504504
if (focusChip) {
505505
focusChip.focus();
506506
}
507-
} else if (chipsArray.length === 0) {
508-
this._focusInput();
509507
}
510508

511509
// Reset our destroyed index

0 commit comments

Comments
 (0)