Skip to content

Commit

Permalink
refactor(chips): use key manager for horizontal keyboard controls (#9600
Browse files Browse the repository at this point in the history
)

Switches to using the horizontal mode from the `ListKeyManager` for the left/right controls, rather than reimplementing them.
  • Loading branch information
crisbeto authored and jelbourn committed Jan 26, 2018
1 parent fd17cf2 commit 5a055a7
Showing 1 changed file with 11 additions and 28 deletions.
39 changes: 11 additions & 28 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, LEFT_ARROW, RIGHT_ARROW} from '@angular/cdk/keycodes';
import {BACKSPACE} from '@angular/cdk/keycodes';
import {startWith} from 'rxjs/operators/startWith';
import {
AfterContentInit,
Expand Down Expand Up @@ -332,8 +332,10 @@ export class MatChipList extends _MatChipListMixinBase implements MatFormFieldCo
}

ngAfterContentInit(): void {

this._keyManager = new FocusKeyManager<MatChip>(this.chips).withWrap();
this._keyManager = new FocusKeyManager<MatChip>(this.chips)
.withWrap()
.withVerticalOrientation()
.withHorizontalOrientation(this._dir ? this._dir.value : 'ltr');

// Prevents the chip list from capturing focus and redirecting
// it back to the first chip when the user tabs out.
Expand Down Expand Up @@ -451,35 +453,16 @@ export class MatChipList extends _MatChipListMixinBase implements MatFormFieldCo
* Pass events to the keyboard manager. Available here for tests.
*/
_keydown(event: KeyboardEvent) {
let code = event.keyCode;
let target = event.target as HTMLElement;
let isInputEmpty = this._isInputEmpty(target);
let isRtl = this._dir && this._dir.value == 'rtl';

let isPrevKey = (code === (isRtl ? RIGHT_ARROW : LEFT_ARROW));
let isNextKey = (code === (isRtl ? LEFT_ARROW : RIGHT_ARROW));
let isBackKey = code === BACKSPACE;
const target = event.target as HTMLElement;

// If they are on an empty input and hit backspace, focus the last chip
if (isInputEmpty && isBackKey) {
if (event.keyCode === BACKSPACE && this._isInputEmpty(target)) {
this._keyManager.setLastItemActive();
event.preventDefault();
return;
}

// If they are on a chip, check for space/left/right, otherwise pass to our key manager (like
// up/down keys)
if (target && target.classList.contains('mat-chip')) {
if (isPrevKey) {
this._keyManager.setPreviousItemActive();
event.preventDefault();
} else if (isNextKey) {
this._keyManager.setNextItemActive();
event.preventDefault();
} else {
this._keyManager.onKeydown(event);
}
} else {
this._keyManager.onKeydown(event);
this.stateChanges.next();
}
this.stateChanges.next();
}


Expand Down

0 comments on commit 5a055a7

Please sign in to comment.