Skip to content

fix(chips): fix chip list focus and keyboard behaviors #7319

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
Sep 29, 2017
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
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