Skip to content

Commit 847a469

Browse files
crisbetojelbourn
authored andcommitted
fix(material-experimental/mdc-chips): support home/end in chip grid (#18052)
Adds support for the "Home" and "End" keys to the `mat-chip-grid`, similarly to the `mat-chip-listbox`.
1 parent d05c695 commit 847a469

File tree

2 files changed

+67
-6
lines changed

2 files changed

+67
-6
lines changed

src/material-experimental/mdc-chips/chip-grid.spec.ts

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ import {
77
LEFT_ARROW,
88
RIGHT_ARROW,
99
SPACE,
10-
TAB
10+
TAB,
11+
HOME,
12+
END
1113
} from '@angular/cdk/keycodes';
1214
import {
1315
createFakeEvent,
@@ -443,6 +445,55 @@ describe('MDC-based MatChipGrid', () => {
443445
expect(manager.activeRowIndex).toBe(0);
444446
expect(manager.activeColumnIndex).toBe(0);
445447
});
448+
449+
it('should move focus to the first chip when pressing HOME', () => {
450+
setupStandardGrid();
451+
manager = chipGridInstance._keyManager;
452+
453+
const nativeChips = chipGridNativeElement.querySelectorAll('mat-chip-row');
454+
const lastNativeChip = nativeChips[nativeChips.length - 1] as HTMLElement;
455+
456+
const HOME_EVENT: KeyboardEvent =
457+
createKeyboardEvent('keydown', HOME, undefined, lastNativeChip);
458+
const array = chips.toArray();
459+
const lastItem = array[array.length - 1];
460+
461+
lastItem.focus();
462+
expect(manager.activeRowIndex).toBe(4);
463+
expect(manager.activeColumnIndex).toBe(0);
464+
465+
chipGridInstance._keydown(HOME_EVENT);
466+
fixture.detectChanges();
467+
468+
expect(HOME_EVENT.defaultPrevented).toBe(true);
469+
expect(manager.activeRowIndex).toBe(0);
470+
expect(manager.activeColumnIndex).toBe(0);
471+
});
472+
473+
it('should move focus to the last chip when pressing END', () => {
474+
setupStandardGrid();
475+
manager = chipGridInstance._keyManager;
476+
477+
const nativeChips = chipGridNativeElement.querySelectorAll('mat-chip-row');
478+
const firstNativeChip = nativeChips[0] as HTMLElement;
479+
480+
const END_EVENT: KeyboardEvent =
481+
createKeyboardEvent('keydown', END, undefined, firstNativeChip);
482+
const array = chips.toArray();
483+
const firstItem = array[0];
484+
485+
firstItem.focus();
486+
expect(manager.activeRowIndex).toBe(0);
487+
expect(manager.activeColumnIndex).toBe(0);
488+
489+
chipGridInstance._keydown(END_EVENT);
490+
fixture.detectChanges();
491+
492+
expect(END_EVENT.defaultPrevented).toBe(true);
493+
expect(manager.activeRowIndex).toBe(4);
494+
expect(manager.activeColumnIndex).toBe(0);
495+
});
496+
446497
});
447498
});
448499

src/material-experimental/mdc-chips/chip-grid.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
import {Directionality} from '@angular/cdk/bidi';
1010
import {BooleanInput, coerceBooleanProperty} from '@angular/cdk/coercion';
11-
import {BACKSPACE, TAB} from '@angular/cdk/keycodes';
11+
import {BACKSPACE, TAB, HOME, END} from '@angular/cdk/keycodes';
1212
import {
1313
AfterContentInit,
1414
AfterViewInit,
@@ -400,17 +400,27 @@ export class MatChipGrid extends _MatChipGridMixinBase implements AfterContentIn
400400
/** Handles custom keyboard events. */
401401
_keydown(event: KeyboardEvent) {
402402
const target = event.target as HTMLElement;
403+
const keyCode = event.keyCode;
404+
const manager = this._keyManager;
403405

404406
// If they are on an empty input and hit backspace, focus the last chip
405-
if (event.keyCode === BACKSPACE && this._isEmptyInput(target)) {
407+
if (keyCode === BACKSPACE && this._isEmptyInput(target)) {
406408
if (this._chips.length) {
407-
this._keyManager.setLastCellActive();
409+
manager.setLastCellActive();
408410
}
409411
event.preventDefault();
410-
} else if (event.keyCode === TAB && target.id !== this._chipInput!.id ) {
412+
} else if (keyCode === TAB && target.id !== this._chipInput!.id ) {
411413
this._allowFocusEscape();
412414
} else if (this._originatesFromChip(event)) {
413-
this._keyManager.onKeydown(event);
415+
if (keyCode === HOME) {
416+
manager.setFirstCellActive();
417+
event.preventDefault();
418+
} else if (keyCode === END) {
419+
manager.setLastCellActive();
420+
event.preventDefault();
421+
} else {
422+
manager.onKeydown(event);
423+
}
414424
}
415425
this.stateChanges.next();
416426
}

0 commit comments

Comments
 (0)