Skip to content

Commit 5a055a7

Browse files
crisbetojelbourn
authored andcommitted
refactor(chips): use key manager for horizontal keyboard controls (#9600)
Switches to using the horizontal mode from the `ListKeyManager` for the left/right controls, rather than reimplementing them.
1 parent fd17cf2 commit 5a055a7

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,
@@ -332,8 +332,10 @@ export class MatChipList extends _MatChipListMixinBase implements MatFormFieldCo
332332
}
333333

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

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

485468

0 commit comments

Comments
 (0)