Skip to content

Commit

Permalink
Revert "fix(cdk/a11y): activeItem out of date if active index is remo…
Browse files Browse the repository at this point in the history
…ved from ListKeyManager (#14471)" (#24233)

This reverts commit e61c2fa.

(cherry picked from commit 8504c77)
  • Loading branch information
andrewseguin committed Jan 19, 2022
1 parent 1f6a3f8 commit ca04fc1
Show file tree
Hide file tree
Showing 4 changed files with 4 additions and 24 deletions.
17 changes: 2 additions & 15 deletions src/cdk/a11y/key-manager/list-key-manager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,28 +82,15 @@ describe('Key managers', () => {
spyOn(keyManager, 'setActiveItem').and.callThrough();
});

it('should maintain the active item if the amount of items changes', fakeAsync(() => {
it('should maintain the active item if the amount of items changes', () => {
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: 2 additions & 5 deletions src/cdk/a11y/key-manager/list-key-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,8 @@ export class ListKeyManager<T extends ListKeyManagerOption> {
const itemArray = newItems.toArray();
const newIndex = itemArray.indexOf(this._activeItem);

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

expect(fixture.componentInstance.items.length).toBe(0);
}));
Expand All @@ -1202,7 +1201,6 @@ 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: 0 additions & 2 deletions src/material/menu/menu.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1171,7 +1171,6 @@ describe('MatMenu', () => {
fixture.detectChanges();
tick(500);
fixture.detectChanges();
tick();

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

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

0 comments on commit ca04fc1

Please sign in to comment.