Skip to content

Commit 3b47546

Browse files
committed
refactor(chips): use key manager for horizontal keyboard controls
Switches to using the horizontal mode from the `ListKeyManager` for the left/right controls, rather than reimplementing them.
1 parent 3bc4cd3 commit 3b47546

File tree

1 file changed

+11
-28
lines changed

1 file changed

+11
-28
lines changed

src/lib/chips/chip-list.ts

Lines changed: 11 additions & 28 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, LEFT_ARROW, RIGHT_ARROW} from '@angular/cdk/keycodes';
13+
import {BACKSPACE} from '@angular/cdk/keycodes';
1414
import {startWith} from 'rxjs/operators/startWith';
1515
import {
1616
AfterContentInit,
@@ -331,8 +331,10 @@ export class MatChipList extends _MatChipListMixinBase implements MatFormFieldCo
331331
}
332332

333333
ngAfterContentInit(): void {
334-
335-
this._keyManager = new FocusKeyManager<MatChip>(this.chips).withWrap();
334+
this._keyManager = new FocusKeyManager<MatChip>(this.chips)
335+
.withWrap()
336+
.withVerticalOrientation()
337+
.withHorizontalOrientation(this._dir ? this._dir.value : 'ltr');
336338

337339
// Prevents the chip list from capturing focus and redirecting
338340
// it back to the first chip when the user tabs out.
@@ -450,35 +452,16 @@ export class MatChipList extends _MatChipListMixinBase implements MatFormFieldCo
450452
* Pass events to the keyboard manager. Available here for tests.
451453
*/
452454
_keydown(event: KeyboardEvent) {
453-
let code = event.keyCode;
454-
let target = event.target as HTMLElement;
455-
let isInputEmpty = this._isInputEmpty(target);
456-
let isRtl = this._dir && this._dir.value == 'rtl';
457-
458-
let isPrevKey = (code === (isRtl ? RIGHT_ARROW : LEFT_ARROW));
459-
let isNextKey = (code === (isRtl ? LEFT_ARROW : RIGHT_ARROW));
460-
let isBackKey = code === BACKSPACE;
455+
const target = event.target as HTMLElement;
456+
461457
// If they are on an empty input and hit backspace, focus the last chip
462-
if (isInputEmpty && isBackKey) {
458+
if (this._isInputEmpty(target) && event.keyCode === BACKSPACE) {
463459
this._keyManager.setLastItemActive();
464460
event.preventDefault();
465-
return;
466-
}
467-
468-
// If they are on a chip, check for space/left/right, otherwise pass to our key manager (like
469-
// up/down keys)
470-
if (target && target.classList.contains('mat-chip')) {
471-
if (isPrevKey) {
472-
this._keyManager.setPreviousItemActive();
473-
event.preventDefault();
474-
} else if (isNextKey) {
475-
this._keyManager.setNextItemActive();
476-
event.preventDefault();
477-
} else {
478-
this._keyManager.onKeydown(event);
479-
}
461+
} else {
462+
this._keyManager.onKeydown(event);
463+
this.stateChanges.next();
480464
}
481-
this.stateChanges.next();
482465
}
483466

484467

0 commit comments

Comments
 (0)