Skip to content

Commit ca04fc1

Browse files
committed
Revert "fix(cdk/a11y): activeItem out of date if active index is removed from ListKeyManager (#14471)" (#24233)
This reverts commit e61c2fa. (cherry picked from commit 8504c77)
1 parent 1f6a3f8 commit ca04fc1

File tree

4 files changed

+4
-24
lines changed

4 files changed

+4
-24
lines changed

src/cdk/a11y/key-manager/list-key-manager.spec.ts

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -82,28 +82,15 @@ describe('Key managers', () => {
8282
spyOn(keyManager, 'setActiveItem').and.callThrough();
8383
});
8484

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

9291
expect(keyManager.activeItemIndex).toBe(1);
9392
expect(keyManager.activeItem!.getLabel()).toBe('one');
94-
}));
95-
96-
it('should keep the active item in sync if the active item is removed', fakeAsync(() => {
97-
expect(keyManager.activeItemIndex).toBe(0);
98-
expect(keyManager.activeItem!.getLabel()).toBe('one');
99-
100-
itemList.reset(itemList.toArray().slice(1));
101-
itemList.notifyOnChanges();
102-
tick();
103-
104-
expect(keyManager.activeItemIndex).toBe(0);
105-
expect(keyManager.activeItem!.getLabel()).toBe('two');
106-
}));
93+
});
10794

10895
it('should start off the activeItem as null', () => {
10996
expect(new ListKeyManager([]).activeItem).toBeNull();

src/cdk/a11y/key-manager/list-key-manager.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,8 @@ export class ListKeyManager<T extends ListKeyManagerOption> {
7070
const itemArray = newItems.toArray();
7171
const newIndex = itemArray.indexOf(this._activeItem);
7272

73-
if (newIndex !== this._activeItemIndex) {
74-
// Timeout is required to avoid "changed after checked" errors.
75-
setTimeout(() => {
76-
this.setActiveItem(newIndex > -1 ? newIndex : this._activeItemIndex);
77-
}, 0);
73+
if (newIndex > -1 && newIndex !== this._activeItemIndex) {
74+
this._activeItemIndex = newIndex;
7875
}
7976
}
8077
});

src/material-experimental/mdc-menu/menu.spec.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1176,7 +1176,6 @@ describe('MDC-based MatMenu', () => {
11761176
fixture.detectChanges();
11771177
tick(500);
11781178
fixture.detectChanges();
1179-
tick();
11801179

11811180
expect(fixture.componentInstance.items.length).toBe(0);
11821181
}));
@@ -1202,7 +1201,6 @@ describe('MDC-based MatMenu', () => {
12021201
.toBe(true);
12031202
tick(500);
12041203
fixture.detectChanges();
1205-
tick();
12061204

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

src/material/menu/menu.spec.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1171,7 +1171,6 @@ describe('MatMenu', () => {
11711171
fixture.detectChanges();
11721172
tick(500);
11731173
fixture.detectChanges();
1174-
tick();
11751174

11761175
expect(fixture.componentInstance.items.length).toBe(0);
11771176
}));
@@ -1197,7 +1196,6 @@ describe('MatMenu', () => {
11971196
.toBe(true);
11981197
tick(500);
11991198
fixture.detectChanges();
1200-
tick();
12011199

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

0 commit comments

Comments
 (0)