Skip to content

Commit

Permalink
fix(a11y): don't handle disallowed modifier keys in typeahead mode (#…
Browse files Browse the repository at this point in the history
…14301)

Recently we switched to skip handling keyboard events with modifiers, unless the consumer opted into them explicitly. The changes weren't applied when in typeahead mode.

Fixes #14274.
  • Loading branch information
crisbeto authored and jelbourn committed Dec 3, 2018
1 parent 498a3d8 commit 700f20f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
24 changes: 24 additions & 0 deletions src/cdk/a11y/key-manager/list-key-manager.spec.ts
Expand Up @@ -664,6 +664,30 @@ describe('Key managers', () => {
expect(keyManager.activeItem).toBe(itemList.items[1]);
}));

it('should not move focus if a modifier, that is not allowed, is pressed', fakeAsync(() => {
const tEvent = createKeyboardEvent('keydown', 84, undefined, 't');
Object.defineProperty(tEvent, 'ctrlKey', {get: () => true});

expect(keyManager.activeItem).toBeFalsy();

keyManager.onKeydown(tEvent); // types "t"
tick(debounceInterval);

expect(keyManager.activeItem).toBeFalsy();
}));

it('should always allow the shift key', fakeAsync(() => {
const tEvent = createKeyboardEvent('keydown', 84, undefined, 't');
Object.defineProperty(tEvent, 'shiftKey', {get: () => true});

expect(keyManager.activeItem).toBeFalsy();

keyManager.onKeydown(tEvent); // types "t"
tick(debounceInterval);

expect(keyManager.activeItem).toBeTruthy();
}));

it('should focus the first item that starts with sequence of letters', fakeAsync(() => {
keyManager.onKeydown(createKeyboardEvent('keydown', 84, undefined, 't')); // types "t"
keyManager.onKeydown(createKeyboardEvent('keydown', 72, undefined, 'h')); // types "h"
Expand Down
15 changes: 9 additions & 6 deletions src/cdk/a11y/key-manager/list-key-manager.ts
Expand Up @@ -18,6 +18,7 @@ import {
Z,
ZERO,
NINE,
hasModifierKey,
} from '@angular/cdk/keycodes';
import {debounceTime, filter, map, tap} from 'rxjs/operators';

Expand Down Expand Up @@ -244,12 +245,14 @@ export class ListKeyManager<T extends ListKeyManagerOption> {
}

default:
// Attempt to use the `event.key` which also maps it to the user's keyboard language,
// otherwise fall back to resolving alphanumeric characters via the keyCode.
if (event.key && event.key.length === 1) {
this._letterKeyStream.next(event.key.toLocaleUpperCase());
} else if ((keyCode >= A && keyCode <= Z) || (keyCode >= ZERO && keyCode <= NINE)) {
this._letterKeyStream.next(String.fromCharCode(keyCode));
if (isModifierAllowed || hasModifierKey(event, 'shiftKey')) {
// Attempt to use the `event.key` which also maps it to the user's keyboard language,
// otherwise fall back to resolving alphanumeric characters via the keyCode.
if (event.key && event.key.length === 1) {
this._letterKeyStream.next(event.key.toLocaleUpperCase());
} else if ((keyCode >= A && keyCode <= Z) || (keyCode >= ZERO && keyCode <= NINE)) {
this._letterKeyStream.next(String.fromCharCode(keyCode));
}
}

// Note that we return here, in order to avoid preventing
Expand Down

0 comments on commit 700f20f

Please sign in to comment.