Skip to content

Commit

Permalink
fix(chips): fix chip list focus and keyboard behaviors
Browse files Browse the repository at this point in the history
  • Loading branch information
tinayuangao committed Sep 27, 2017
1 parent 3571f68 commit b385300
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 11 deletions.
12 changes: 6 additions & 6 deletions src/lib/chips/chip-list.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ describe('MdChipList', () => {

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

it('should focus the last chip when press DELETE', () => {
it('should not focus the last chip when press DELETE', () => {
let nativeInput = fixture.nativeElement.querySelector('input');
let DELETE_EVENT: KeyboardEvent =
createKeyboardEvent('keydown', DELETE, nativeInput);
Expand All @@ -292,8 +292,8 @@ describe('MdChipList', () => {
chipListInstance._keydown(DELETE_EVENT);
fixture.detectChanges();

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

it('should focus the last chip when press BACKSPACE', () => {
Expand Down Expand Up @@ -786,7 +786,7 @@ describe('MdChipList', () => {

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

it('should focus the last chip when press DELETE', () => {
it('should not focus the last chip when press DELETE', () => {
let nativeInput = fixture.nativeElement.querySelector('input');
let DELETE_EVENT: KeyboardEvent =
createKeyboardEvent('keydown', DELETE, nativeInput);
Expand All @@ -799,8 +799,8 @@ describe('MdChipList', () => {
chipListInstance._keydown(DELETE_EVENT);
fixture.detectChanges();

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

it('should focus the last chip when press BACKSPACE', () => {
Expand Down
8 changes: 3 additions & 5 deletions src/lib/chips/chip-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {FocusKeyManager} from '@angular/cdk/a11y';
import {Directionality} from '@angular/cdk/bidi';
import {coerceBooleanProperty} from '@angular/cdk/coercion';
import {SelectionModel} from '@angular/cdk/collections';
import {BACKSPACE, DELETE, LEFT_ARROW, RIGHT_ARROW, UP_ARROW} from '@angular/cdk/keycodes';
import {BACKSPACE, LEFT_ARROW, RIGHT_ARROW} from '@angular/cdk/keycodes';
import {startWith} from '@angular/cdk/rxjs';
import {
AfterContentInit,
Expand Down Expand Up @@ -432,8 +432,8 @@ export class MdChipList implements MdFormFieldControl<any>, ControlValueAccessor

let isPrevKey = (code === (isRtl ? RIGHT_ARROW : LEFT_ARROW));
let isNextKey = (code === (isRtl ? LEFT_ARROW : RIGHT_ARROW));
let isBackKey = (code === BACKSPACE || code == DELETE || code == UP_ARROW || isPrevKey);
// If they are on an empty input and hit backspace/delete/left arrow, focus the last chip
let isBackKey = code === BACKSPACE;
// If they are on an empty input and hit backspace, focus the last chip
if (isInputEmpty && isBackKey) {
this._keyManager.setLastItemActive();
event.preventDefault();
Expand Down Expand Up @@ -504,8 +504,6 @@ export class MdChipList implements MdFormFieldControl<any>, ControlValueAccessor
if (focusChip) {
focusChip.focus();
}
} else if (chipsArray.length === 0) {
this._focusInput();
}

// Reset our destroyed index
Expand Down

0 comments on commit b385300

Please sign in to comment.