Skip to content

Commit 7834956

Browse files
committed
fix(cdk-experimental/listbox): ignore spaces during typeahead
1 parent b1a5c61 commit 7834956

File tree

2 files changed

+10
-7
lines changed

2 files changed

+10
-7
lines changed

src/cdk-experimental/ui-patterns/behaviors/list-typeahead/list-typeahead.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export class ListTypeahead<T extends ListTypeaheadItem> {
3737
navigation: ListNavigation<T>;
3838

3939
/** Keeps track of the characters that typeahead search is being called with. */
40-
private _query = signal('');
40+
query = signal('');
4141

4242
/** The index where that the typeahead search was initiated from. */
4343
private _startIndex = signal<number | undefined>(undefined);
@@ -57,15 +57,15 @@ export class ListTypeahead<T extends ListTypeaheadItem> {
5757
}
5858

5959
clearTimeout(this.timeout);
60-
this._query.update(q => q + char.toLowerCase());
60+
this.query.update(q => q + char.toLowerCase());
6161
const item = this._getItem();
6262

6363
if (item) {
6464
this.navigation.goto(item);
6565
}
6666

6767
this.timeout = setTimeout(() => {
68-
this._query.set('');
68+
this.query.set('');
6969
this._startIndex.set(undefined);
7070
}, this.inputs.typeaheadDelay() * 1000);
7171
}
@@ -88,6 +88,6 @@ export class ListTypeahead<T extends ListTypeaheadItem> {
8888
}
8989
}
9090

91-
return focusableItems.find(i => i.searchTerm().toLowerCase().startsWith(this._query()));
91+
return focusableItems.find(i => i.searchTerm().toLowerCase().startsWith(this.query()));
9292
}
9393
}

src/cdk-experimental/ui-patterns/listbox/listbox.ts

+6-3
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,9 @@ export class ListboxPattern<V> {
9191
return this.inputs.textDirection() === 'rtl' ? 'ArrowLeft' : 'ArrowRight';
9292
});
9393

94+
/** Represents the space key. Does nothing when the user is actively using typeahead. */
95+
spaceKey = computed(() => (this.typeahead.query().length ? '' : ' '));
96+
9497
/** The regexp used to decide if a key should trigger typeahead. */
9598
typeaheadRegexp = /^.$/; // TODO: Ignore spaces?
9699

@@ -127,7 +130,7 @@ export class ListboxPattern<V> {
127130

128131
if (this.inputs.multi()) {
129132
manager
130-
.on(Modifier.Shift, ' ', () => this._updateSelection({selectFromAnchor: true}))
133+
.on(Modifier.Shift, this.spaceKey, () => this._updateSelection({selectFromAnchor: true}))
131134
.on(Modifier.Shift, 'Enter', () => this._updateSelection({selectFromAnchor: true}))
132135
.on(Modifier.Shift, this.prevKey, () => this.prev({toggle: true}))
133136
.on(Modifier.Shift, this.nextKey, () => this.next({toggle: true}))
@@ -137,12 +140,12 @@ export class ListboxPattern<V> {
137140
}
138141

139142
if (!this.followFocus() && this.inputs.multi()) {
140-
manager.on(' ', () => this._updateSelection({toggle: true}));
143+
manager.on(this.spaceKey, () => this._updateSelection({toggle: true}));
141144
manager.on('Enter', () => this._updateSelection({toggle: true}));
142145
}
143146

144147
if (!this.followFocus() && !this.inputs.multi()) {
145-
manager.on(' ', () => this._updateSelection({toggleOne: true}));
148+
manager.on(this.spaceKey, () => this._updateSelection({toggleOne: true}));
146149
manager.on('Enter', () => this._updateSelection({toggleOne: true}));
147150
}
148151

0 commit comments

Comments
 (0)