Skip to content

fix(cdk-experimental/listbox): ignore spaces during typeahead #30766

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,117 +27,65 @@ describe('List Typeahead', () => {
);
}

let items: SignalLike<TestItem[]>;
let typeahead: ListTypeahead<TestItem>;
let navigation: ListNavigation<TestItem>;

beforeEach(() => {
items = getItems(5);
navigation = new ListNavigation({
items,
wrap: signal(false),
activeIndex: signal(0),
skipDisabled: signal(false),
textDirection: signal('ltr'),
orientation: signal('vertical'),
});
typeahead = new ListTypeahead({
navigation,
typeaheadDelay: signal(0.5),
});
});

describe('#search', () => {
it('should navigate to an item', () => {
const items = getItems(5);
const activeIndex = signal(0);
const navigation = new ListNavigation({
items,
activeIndex,
wrap: signal(false),
skipDisabled: signal(false),
textDirection: signal('ltr'),
orientation: signal('vertical'),
});
const typeahead = new ListTypeahead({
navigation,
typeaheadDelay: signal(0.5),
});

typeahead.search('i');
expect(activeIndex()).toBe(1);
expect(navigation.inputs.activeIndex()).toBe(1);

typeahead.search('t');
typeahead.search('e');
typeahead.search('m');
typeahead.search(' ');
typeahead.search('3');
expect(activeIndex()).toBe(3);
expect(navigation.inputs.activeIndex()).toBe(3);
});

it('should reset after a delay', fakeAsync(() => {
const items = getItems(5);
const activeIndex = signal(0);
const navigation = new ListNavigation({
items,
activeIndex,
wrap: signal(false),
skipDisabled: signal(false),
textDirection: signal('ltr'),
orientation: signal('vertical'),
});
const typeahead = new ListTypeahead({
navigation,
typeaheadDelay: signal(0.5),
});

typeahead.search('i');
expect(activeIndex()).toBe(1);
expect(navigation.inputs.activeIndex()).toBe(1);

tick(500);

typeahead.search('i');
expect(activeIndex()).toBe(2);
expect(navigation.inputs.activeIndex()).toBe(2);
}));

it('should skip disabled items', () => {
const items = getItems(5);
const activeIndex = signal(0);
const navigation = new ListNavigation({
items,
activeIndex,
wrap: signal(false),
skipDisabled: signal(true),
textDirection: signal('ltr'),
orientation: signal('vertical'),
});
const typeahead = new ListTypeahead({
navigation,
typeaheadDelay: signal(0.5),
});
items()[1].disabled.set(true);

(navigation.inputs.skipDisabled as WritableSignalLike<boolean>).set(true);
typeahead.search('i');
expect(activeIndex()).toBe(2);
console.log(typeahead.inputs.navigation.inputs.items().map(i => i.disabled()));
expect(navigation.inputs.activeIndex()).toBe(2);
});

it('should not skip disabled items', () => {
const items = getItems(5);
const activeIndex = signal(0);
const navigation = new ListNavigation({
items,
activeIndex,
wrap: signal(false),
skipDisabled: signal(false),
textDirection: signal('ltr'),
orientation: signal('vertical'),
});
const typeahead = new ListTypeahead({
navigation,
typeaheadDelay: signal(0.5),
});
items()[1].disabled.set(true);

(navigation.inputs.skipDisabled as WritableSignalLike<boolean>).set(false);
typeahead.search('i');
expect(activeIndex()).toBe(1);
expect(navigation.inputs.activeIndex()).toBe(1);
});

it('should ignore keys like shift', () => {
const items = getItems(5);
const activeIndex = signal(0);
const navigation = new ListNavigation({
items,
activeIndex,
wrap: signal(false),
skipDisabled: signal(false),
textDirection: signal('ltr'),
orientation: signal('vertical'),
});
const typeahead = new ListTypeahead({
navigation,
typeaheadDelay: signal(0.5),
});

typeahead.search('i');
typeahead.search('t');
typeahead.search('e');
Expand All @@ -147,7 +95,18 @@ describe('List Typeahead', () => {
typeahead.search('m');
typeahead.search(' ');
typeahead.search('2');
expect(activeIndex()).toBe(2);
expect(navigation.inputs.activeIndex()).toBe(2);
});

it('should not allow a query to begin with a space', () => {
typeahead.search(' ');
typeahead.search('i');
typeahead.search('t');
typeahead.search('e');
typeahead.search('m');
typeahead.search(' ');
typeahead.search('3');
expect(navigation.inputs.activeIndex()).toBe(3);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* found in the LICENSE file at https://angular.dev/license
*/

import {signal} from '@angular/core';
import {computed, signal} from '@angular/core';
import {SignalLike} from '../signal-like/signal-like';
import {ListNavigationItem, ListNavigation} from '../list-navigation/list-navigation';

Expand Down Expand Up @@ -36,6 +36,9 @@ export class ListTypeahead<T extends ListTypeaheadItem> {
/** The navigation controller of the parent list. */
navigation: ListNavigation<T>;

/** Whether the user is actively typing a typeahead search query. */
isTyping = computed(() => this._query().length > 0);

/** Keeps track of the characters that typeahead search is being called with. */
private _query = signal('');

Expand All @@ -52,6 +55,10 @@ export class ListTypeahead<T extends ListTypeaheadItem> {
return;
}

if (!this.isTyping() && char === ' ') {
return;
}

if (this._startIndex() === undefined) {
this._startIndex.set(this.navigation.inputs.activeIndex());
}
Expand Down
13 changes: 9 additions & 4 deletions src/cdk-experimental/ui-patterns/listbox/listbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ export class ListboxPattern<V> {
return this.inputs.textDirection() === 'rtl' ? 'ArrowLeft' : 'ArrowRight';
});

/** Represents the space key. Does nothing when the user is actively using typeahead. */
dynamicSpaceKey = computed(() => (this.typeahead.isTyping() ? '' : ' '));

/** The regexp used to decide if a key should trigger typeahead. */
typeaheadRegexp = /^.$/; // TODO: Ignore spaces?

Expand Down Expand Up @@ -127,22 +130,24 @@ export class ListboxPattern<V> {

if (this.inputs.multi()) {
manager
.on(Modifier.Shift, ' ', () => this._updateSelection({selectFromAnchor: true}))
.on(Modifier.Shift, 'Enter', () => this._updateSelection({selectFromAnchor: true}))
.on(Modifier.Shift, this.prevKey, () => this.prev({toggle: true}))
.on(Modifier.Shift, this.nextKey, () => this.next({toggle: true}))
.on(Modifier.Ctrl | Modifier.Shift, 'Home', () => this.first({selectFromActive: true}))
.on(Modifier.Ctrl | Modifier.Shift, 'End', () => this.last({selectFromActive: true}))
.on(Modifier.Ctrl, 'A', () => this._updateSelection({selectAll: true}));
.on(Modifier.Ctrl, 'A', () => this._updateSelection({selectAll: true}))
.on(Modifier.Shift, this.dynamicSpaceKey, () =>
this._updateSelection({selectFromAnchor: true}),
);
}

if (!this.followFocus() && this.inputs.multi()) {
manager.on(' ', () => this._updateSelection({toggle: true}));
manager.on(this.dynamicSpaceKey, () => this._updateSelection({toggle: true}));
manager.on('Enter', () => this._updateSelection({toggle: true}));
}

if (!this.followFocus() && !this.inputs.multi()) {
manager.on(' ', () => this._updateSelection({toggleOne: true}));
manager.on(this.dynamicSpaceKey, () => this._updateSelection({toggleOne: true}));
manager.on('Enter', () => this._updateSelection({toggleOne: true}));
}

Expand Down
Loading