Skip to content

Commit 1f4a962

Browse files
crisbetokara
authored andcommitted
fix(list-key-manager): increase typeahead range to include more characters (#6543)
1 parent 5bd655b commit 1f4a962

File tree

3 files changed

+34
-10
lines changed

3 files changed

+34
-10
lines changed

src/cdk/a11y/list-key-manager.spec.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,26 @@ describe('Key managers', () => {
461461
expect(keyManager.activeItem).toBe(itemList.items[1]);
462462
}));
463463

464+
it('should handle non-letter characters', fakeAsync(() => {
465+
itemList.items = [
466+
new FakeFocusable('[]'),
467+
new FakeFocusable('321'),
468+
new FakeFocusable('`!?')
469+
];
470+
471+
keyManager.onKeydown(createKeyboardEvent('keydown', 192, undefined, '`')); // types "`"
472+
tick(debounceInterval);
473+
expect(keyManager.activeItem).toBe(itemList.items[2]);
474+
475+
keyManager.onKeydown(createKeyboardEvent('keydown', 51, undefined, '3')); // types "3"
476+
tick(debounceInterval);
477+
expect(keyManager.activeItem).toBe(itemList.items[1]);
478+
479+
keyManager.onKeydown(createKeyboardEvent('keydown', 219, undefined, '[')); // types "["
480+
tick(debounceInterval);
481+
expect(keyManager.activeItem).toBe(itemList.items[0]);
482+
}));
483+
464484
});
465485

466486
});

src/cdk/a11y/list-key-manager.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import {QueryList} from '@angular/core';
1010
import {Subject} from 'rxjs/Subject';
1111
import {Subscription} from 'rxjs/Subscription';
12-
import {UP_ARROW, DOWN_ARROW, TAB, A, Z} from '@angular/cdk/keycodes';
12+
import {UP_ARROW, DOWN_ARROW, TAB, A, Z, ZERO, NINE} from '@angular/cdk/keycodes';
1313
import {RxChain, debounceTime, filter, map, doOperator} from '@angular/cdk/rxjs';
1414

1515
/**
@@ -107,17 +107,19 @@ export class ListKeyManager<T extends ListKeyManagerOption> {
107107
case UP_ARROW: this.setPreviousItemActive(); break;
108108
case TAB: this.tabOut.next(); return;
109109
default:
110-
if (event.keyCode >= A && event.keyCode <= Z) {
111-
// Attempt to use the `event.key` which also maps it to the user's keyboard language,
112-
// otherwise fall back to `keyCode` and `fromCharCode` which always resolve to English.
113-
this._letterKeyStream.next(event.key ?
114-
event.key.toLocaleUpperCase() :
115-
String.fromCharCode(event.keyCode));
110+
const keyCode = event.keyCode;
111+
112+
// Attempt to use the `event.key` which also maps it to the user's keyboard language,
113+
// otherwise fall back to resolving alphanumeric characters via the keyCode.
114+
if (event.key && event.key.length === 1) {
115+
this._letterKeyStream.next(event.key.toLocaleUpperCase());
116+
} else if ((keyCode >= A && keyCode <= Z) || (keyCode >= ZERO && keyCode <= NINE)) {
117+
this._letterKeyStream.next(String.fromCharCode(keyCode));
116118
}
117119

118-
// Note that we return here, in order to avoid preventing
119-
// the default action of non-navigational keys.
120-
return;
120+
// Note that we return here, in order to avoid preventing
121+
// the default action of non-navigational keys.
122+
return;
121123
}
122124

123125
this._pressedLetters = [];

src/cdk/keycodes/keycodes.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,5 @@ export const BACKSPACE = 8;
2222
export const DELETE = 46;
2323
export const A = 65;
2424
export const Z = 90;
25+
export const ZERO = 48;
26+
export const NINE = 91;

0 commit comments

Comments
 (0)