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
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 angular#14274.
  • Loading branch information
crisbeto committed Nov 27, 2018
1 parent 141afcf commit 4a647a5
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
12 changes: 12 additions & 0 deletions src/cdk/a11y/key-manager/list-key-manager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -664,6 +664,18 @@ 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 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
10 changes: 6 additions & 4 deletions src/cdk/a11y/key-manager/list-key-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,10 +246,12 @@ 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) {
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 4a647a5

Please sign in to comment.