Skip to content

fix(list-key-manager): align matching logic with native listbox #7212

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 1 commit into from
Oct 3, 2017
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
56 changes: 56 additions & 0 deletions src/cdk/a11y/list-key-manager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,62 @@ describe('Key managers', () => {
expect(keyManager.activeItem).toBeFalsy();
}));

it('should start looking for matches after the active item', fakeAsync(() => {
itemList.items = [
new FakeFocusable('Bilbo'),
new FakeFocusable('Frodo'),
new FakeFocusable('Pippin'),
new FakeFocusable('Boromir'),
new FakeFocusable('Aragorn')
];

keyManager.setActiveItem(1);
keyManager.onKeydown(createKeyboardEvent('keydown', 66, undefined, 'b'));
tick(debounceInterval);

expect(keyManager.activeItem).toBe(itemList.items[3]);
}));

it('should wrap back around if there were no matches after the active item', fakeAsync(() => {
itemList.items = [
new FakeFocusable('Bilbo'),
new FakeFocusable('Frodo'),
new FakeFocusable('Pippin'),
new FakeFocusable('Boromir'),
new FakeFocusable('Aragorn')
];

keyManager.setActiveItem(3);
keyManager.onKeydown(createKeyboardEvent('keydown', 66, undefined, 'b'));
tick(debounceInterval);

expect(keyManager.activeItem).toBe(itemList.items[0]);
}));

it('should wrap back around if the last item is active', fakeAsync(() => {
keyManager.setActiveItem(2);
keyManager.onKeydown(createKeyboardEvent('keydown', 79, undefined, 'o'));
tick(debounceInterval);

expect(keyManager.activeItem).toBe(itemList.items[0]);
}));

it('should be able to select the first item', fakeAsync(() => {
keyManager.setActiveItem(-1);
keyManager.onKeydown(createKeyboardEvent('keydown', 79, undefined, 'o'));
tick(debounceInterval);

expect(keyManager.activeItem).toBe(itemList.items[0]);
}));

it('should not do anything if there is no match', fakeAsync(() => {
keyManager.setActiveItem(1);
keyManager.onKeydown(createKeyboardEvent('keydown', 87, undefined, 'w'));
tick(debounceInterval);

expect(keyManager.activeItem).toBe(itemList.items[1]);
}));

});

});
Expand Down
9 changes: 6 additions & 3 deletions src/cdk/a11y/list-key-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,14 @@ export class ListKeyManager<T extends ListKeyManagerOption> {
.subscribe(inputString => {
const items = this._items.toArray();

for (let i = 0; i < items.length; i++) {
let item = items[i];
// Start at 1 because we want to start searching at the item immediately
// following the current active item.
for (let i = 1; i < items.length + 1; i++) {
const index = (this._activeItemIndex + i) % items.length;
const item = items[index];

if (!item.disabled && item.getLabel!().toUpperCase().trim().indexOf(inputString) === 0) {
this.setActiveItem(i);
this.setActiveItem(index);
break;
}
}
Expand Down