Skip to content

Commit 3f7c36b

Browse files
committed
feat(cdk/a11y): fix typeahead build errors
1 parent bbd71c6 commit 3f7c36b

File tree

1 file changed

+21
-30
lines changed

1 file changed

+21
-30
lines changed

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

Lines changed: 21 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,8 @@ import {
2424
Observable,
2525
Subject,
2626
Subscription,
27-
throwError,
2827
} from 'rxjs';
29-
import {debounceTime, filter, map, switchMap, take, tap} from 'rxjs/operators';
28+
import {debounceTime, filter, map, take, tap} from 'rxjs/operators';
3029

3130
const DEFAULT_TYPEAHEAD_DEBOUNCE_INTERVAL_MS = 200;
3231

@@ -322,44 +321,36 @@ export class TreeKeyManager<T extends TreeKeyManagerItem> {
322321
private _setTypeAhead(debounceInterval: number) {
323322
this._typeaheadSubscription.unsubscribe();
324323

324+
if (
325+
(typeof ngDevMode === 'undefined' || ngDevMode) &&
326+
this._items.length &&
327+
this._items.some(item => typeof item.getLabel !== 'function')
328+
) {
329+
throw new Error(
330+
'TreeKeyManager items in typeahead mode must implement the `getLabel` method.',
331+
);
332+
}
333+
325334
// Debounce the presses of non-navigational keys, collect the ones that correspond to letters
326335
// and convert those letters back into a string. Afterwards find the first item that starts
327336
// with that string and select it.
328-
this._typeaheadSubscription = this._getItems()
337+
this._typeaheadSubscription = this._letterKeyStream
329338
.pipe(
330-
switchMap(items => {
331-
if (
332-
(typeof ngDevMode === 'undefined' || ngDevMode) &&
333-
items.length &&
334-
items.some(item => typeof item.getLabel !== 'function')
335-
) {
336-
return throwError(
337-
new Error(
338-
'TreeKeyManager items in typeahead mode must implement the `getLabel` method.',
339-
),
340-
);
341-
}
342-
return observableOf(items) as Observable<Array<T & {getLabel(): string}>>;
343-
}),
344-
switchMap(items => {
345-
return this._letterKeyStream.pipe(
346-
tap(letter => this._pressedLetters.push(letter)),
347-
debounceTime(debounceInterval),
348-
filter(() => this._pressedLetters.length > 0),
349-
map(() => [this._pressedLetters.join(''), items] as const),
350-
);
351-
}),
339+
tap(letter => this._pressedLetters.push(letter)),
340+
debounceTime(debounceInterval),
341+
filter(() => this._pressedLetters.length > 0),
342+
map(() => this._pressedLetters.join('')),
352343
)
353-
.subscribe(([inputString, items]) => {
344+
.subscribe(inputString => {
354345
// Start at 1 because we want to start searching at the item immediately
355346
// following the current active item.
356-
for (let i = 1; i < items.length + 1; i++) {
357-
const index = (this._activeItemIndex + i) % items.length;
358-
const item = items[index];
347+
for (let i = 1; i < this._items.length + 1; i++) {
348+
const index = (this._activeItemIndex + i) % this._items.length;
349+
const item = this._items[index];
359350

360351
if (
361352
!this._skipPredicateFn(item) &&
362-
item.getLabel().toUpperCase().trim().indexOf(inputString) === 0
353+
item.getLabel?.().toUpperCase().trim().indexOf(inputString) === 0
363354
) {
364355
this._setActiveItem(index);
365356
break;

0 commit comments

Comments
 (0)