diff --git a/src/cdk/a11y/key-manager/list-key-manager.spec.ts b/src/cdk/a11y/key-manager/list-key-manager.spec.ts index 4bbc026b87ac..2e7ae77d0760 100644 --- a/src/cdk/a11y/key-manager/list-key-manager.spec.ts +++ b/src/cdk/a11y/key-manager/list-key-manager.spec.ts @@ -93,6 +93,17 @@ describe('Key managers', () => { expect(keyManager.activeItem!.getLabel()).toBe('one'); }); + it('should keep the active item in sync if the active item is removed', () => { + expect(keyManager.activeItemIndex).toBe(0); + expect(keyManager.activeItem!.getLabel()).toBe('one'); + + itemList.items.shift(); + itemList.notifyOnChanges(); + + expect(keyManager.activeItemIndex).toBe(0); + expect(keyManager.activeItem!.getLabel()).toBe('two'); + }); + it('should start off the activeItem as null', () => { expect(new ListKeyManager([]).activeItem).toBeNull(); }); diff --git a/src/cdk/a11y/key-manager/list-key-manager.ts b/src/cdk/a11y/key-manager/list-key-manager.ts index 9b6bf2d04dcc..c68001c4021f 100644 --- a/src/cdk/a11y/key-manager/list-key-manager.ts +++ b/src/cdk/a11y/key-manager/list-key-manager.ts @@ -67,8 +67,8 @@ export class ListKeyManager { const itemArray = newItems.toArray(); const newIndex = itemArray.indexOf(this._activeItem); - if (newIndex > -1 && newIndex !== this._activeItemIndex) { - this._activeItemIndex = newIndex; + if (newIndex !== this._activeItemIndex) { + this.updateActiveItem(newIndex > -1 ? newIndex : this._activeItemIndex); } } });