fix(a11y): activeItem out of date if active index is removed from ListKeyManager (#14407)

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

Fixes #14345.
This commit is contained in:
Kristiyan Kostadinov
2018-12-07 01:07:06 +01:00
committed by mmalerba
parent ab7818381e
commit 014dc7915a
2 changed files with 13 additions and 2 deletions
@@ -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();
});
+2 -2
View File
@@ -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);
}
}
});