Skip to content

Commit

Permalink
fix(a11y): activeItem out of date if active index is removed from Lis…
Browse files Browse the repository at this point in the history
…tKeyManager (angular#14407)

Fixes the `activeItem` on the `ListKeyManager` not matching the item at the `activeItemIndex`, if the `activeItem` is removed from the list.

Fixes angular#14345.
  • Loading branch information
crisbeto authored and josephperrott committed Jan 14, 2019
1 parent e99d1b0 commit ccfe53f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
11 changes: 11 additions & 0 deletions src/cdk/a11y/key-manager/list-key-manager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
Expand Down
4 changes: 2 additions & 2 deletions src/cdk/a11y/key-manager/list-key-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ 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) {
this.updateActiveItem(newIndex > -1 ? newIndex : this._activeItemIndex);
}
}
});
Expand Down

0 comments on commit ccfe53f

Please sign in to comment.