Skip to content

Commit 3ee6b9d

Browse files
committed
fix(a11y): activeItem out of date if active index is removed from ListKeyManager
Fixes the `activeItem` on the `ListKeyManager` not matching the item at the `activeItemIndex`, if the `activeItem` is removed from the list. Fixes #14345.
1 parent bc8fc75 commit 3ee6b9d

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

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

+11
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,17 @@ describe('Key managers', () => {
9393
expect(keyManager.activeItem!.getLabel()).toBe('one');
9494
});
9595

96+
it('should keep the active item in sync if the active item is removed', () => {
97+
expect(keyManager.activeItemIndex).toBe(0);
98+
expect(keyManager.activeItem!.getLabel()).toBe('one');
99+
100+
itemList.items.shift();
101+
itemList.notifyOnChanges();
102+
103+
expect(keyManager.activeItemIndex).toBe(0);
104+
expect(keyManager.activeItem!.getLabel()).toBe('two');
105+
});
106+
96107
it('should start off the activeItem as null', () => {
97108
expect(new ListKeyManager([]).activeItem).toBeNull();
98109
});

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ export class ListKeyManager<T extends ListKeyManagerOption> {
6767
const itemArray = newItems.toArray();
6868
const newIndex = itemArray.indexOf(this._activeItem);
6969

70-
if (newIndex > -1 && newIndex !== this._activeItemIndex) {
71-
this._activeItemIndex = newIndex;
70+
if (newIndex !== this._activeItemIndex) {
71+
this.updateActiveItem(newIndex > -1 ? newIndex : this._activeItemIndex);
7272
}
7373
}
7474
});

0 commit comments

Comments
 (0)