Skip to content
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

fix(cdk/a11y): activeItem out of date if active index is removed from ListKeyManager #14471

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 15 additions & 2 deletions src/cdk/a11y/key-manager/list-key-manager.spec.ts
Expand Up @@ -82,15 +82,28 @@ describe('Key managers', () => {
spyOn(keyManager, 'setActiveItem').and.callThrough();
});

it('should maintain the active item if the amount of items changes', () => {
it('should maintain the active item if the amount of items changes', fakeAsync(() => {
expect(keyManager.activeItemIndex).toBe(0);
expect(keyManager.activeItem!.getLabel()).toBe('one');
itemList.reset([new FakeFocusable('zero'), ...itemList.toArray()]);
itemList.notifyOnChanges();
tick();

expect(keyManager.activeItemIndex).toBe(1);
expect(keyManager.activeItem!.getLabel()).toBe('one');
});
}));

it('should keep the active item in sync if the active item is removed', fakeAsync(() => {
expect(keyManager.activeItemIndex).toBe(0);
expect(keyManager.activeItem!.getLabel()).toBe('one');

itemList.reset(itemList.toArray().slice(1));
itemList.notifyOnChanges();
tick();

expect(keyManager.activeItemIndex).toBe(0);
expect(keyManager.activeItem!.getLabel()).toBe('two');
}));

it('should start off the activeItem as null', () => {
expect(new ListKeyManager([]).activeItem).toBeNull();
Expand Down
7 changes: 5 additions & 2 deletions src/cdk/a11y/key-manager/list-key-manager.ts
Expand Up @@ -70,8 +70,11 @@ export class ListKeyManager<T extends ListKeyManagerOption> {
const itemArray = newItems.toArray();
const newIndex = itemArray.indexOf(this._activeItem);

if (newIndex > -1 && newIndex !== this._activeItemIndex) {
this._activeItemIndex = newIndex;
if (newIndex !== this._activeItemIndex) {
// Timeout is required to avoid "changed after checked" errors.
setTimeout(() => {
this.updateActiveItem(newIndex > -1 ? newIndex : this._activeItemIndex);
}, 0);
}
}
});
Expand Down
2 changes: 2 additions & 0 deletions src/material-experimental/mdc-menu/menu.spec.ts
Expand Up @@ -1176,6 +1176,7 @@ describe('MDC-based MatMenu', () => {
fixture.detectChanges();
tick(500);
fixture.detectChanges();
tick();

expect(fixture.componentInstance.items.length).toBe(0);
}));
Expand All @@ -1201,6 +1202,7 @@ describe('MDC-based MatMenu', () => {
.toBe(true);
tick(500);
fixture.detectChanges();
tick();

expect(trigger.menuOpen).withContext('Expected menu to be closed').toBe(false);
}));
Expand Down
2 changes: 2 additions & 0 deletions src/material/menu/menu.spec.ts
Expand Up @@ -1171,6 +1171,7 @@ describe('MatMenu', () => {
fixture.detectChanges();
tick(500);
fixture.detectChanges();
tick();

expect(fixture.componentInstance.items.length).toBe(0);
}));
Expand All @@ -1196,6 +1197,7 @@ describe('MatMenu', () => {
.toBe(true);
tick(500);
fixture.detectChanges();
tick();

expect(trigger.menuOpen).withContext('Expected menu to be closed').toBe(false);
}));
Expand Down