MCPcopy Create free account
hub / github.com/angular/components / _handleKeydown

Method _handleKeydown

src/material/chips/chip-grid.ts:448–495  ·  view source on GitHub ↗

Handles custom keyboard events.

(event: KeyboardEvent)

Source from the content-addressed store, hash-verified

446
447 /** Handles custom keyboard events. */
448 override _handleKeydown(event: KeyboardEvent) {
449 const keyCode = event.keyCode;
450 const activeItem = this._keyManager.activeItem;
451
452 if (keyCode === TAB) {
453 if (
454 this._chipInput?.focused &&
455 hasModifierKey(event, 'shiftKey') &&
456 this._chips.length &&
457 !this._chips.last.disabled
458 ) {
459 event.preventDefault();
460
461 if (activeItem) {
462 this._keyManager.setActiveItem(activeItem);
463 } else {
464 this._focusLastChip();
465 }
466 } else {
467 // Use the super method here since it doesn't check for the input
468 // focused state. This allows focus to escape if there's only one
469 // disabled chip left in the list.
470 super._allowFocusEscape();
471 }
472 } else if (!this._chipInput?.focused) {
473 // The up and down arrows are supposed to navigate between the individual rows in the grid.
474 // We do this by filtering the actions down to the ones that have the same `_isPrimary`
475 // flag as the active action and moving focus between them ourseles instead of delegating
476 // to the key manager. For more information, see #29359 and:
477 // https://www.w3.org/WAI/ARIA/apg/patterns/grid/examples/layout-grids/#ex2_label
478 if ((keyCode === UP_ARROW || keyCode === DOWN_ARROW) && activeItem) {
479 const eligibleActions = this._chipActions.filter(
480 action => action._isPrimary === activeItem._isPrimary && !this._skipPredicate(action),
481 );
482 const currentIndex = eligibleActions.indexOf(activeItem);
483 const delta = event.keyCode === UP_ARROW ? -1 : 1;
484
485 event.preventDefault();
486 if (currentIndex > -1 && this._isValidIndex(currentIndex + delta)) {
487 this._keyManager.setActiveItem(eligibleActions[currentIndex + delta]);
488 }
489 } else {
490 super._handleKeydown(event);
491 }
492 }
493
494 this.stateChanges.next();
495 }
496
497 protected override _redirectDestroyedChipFocus() {
498 if (this._lastDestroyedFocusedChipIndex === null) {

Callers

nothing calls this directly

Calls 8

_focusLastChipMethod · 0.95
hasModifierKeyFunction · 0.90
setActiveItemMethod · 0.45
_allowFocusEscapeMethod · 0.45
filterMethod · 0.45
_skipPredicateMethod · 0.45
_isValidIndexMethod · 0.45
nextMethod · 0.45

Tested by

no test coverage detected