Skip to content

fix(ListKeyManager): Unset activeItem if it is removed from items #20281

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

Closed
wants to merge 1 commit into from
Closed
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
12 changes: 12 additions & 0 deletions src/cdk/a11y/key-manager/list-key-manager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,18 @@ describe('Key managers', () => {
expect(keyManager.activeItem).toBeNull();
});

it('should reset the activeItem to null if it is removed from the QueryList', () => {
expect(keyManager.activeItemIndex).toBe(0);
expect(keyManager.activeItem!.getLabel()).toBe('one');

itemList.items.splice(0);
itemList.notifyOnChanges();

expect(keyManager.activeItemIndex).toBe(-1);
expect(keyManager.activeItem).toBeNull();

});

describe('Key events', () => {

it('should emit tabOut when the tab key is pressed', () => {
Expand Down
3 changes: 3 additions & 0 deletions src/cdk/a11y/key-manager/list-key-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ export class ListKeyManager<T extends ListKeyManagerOption> {

if (newIndex > -1 && newIndex !== this._activeItemIndex) {
this._activeItemIndex = newIndex;
} else {
this._activeItemIndex = -1;
this._activeItem = null;
}
}
});
Expand Down