mirror of
https://github.com/angular/angular.git
synced 2026-09-14 13:54:52 +08:00
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:
committed by
mmalerba
parent
ab7818381e
commit
014dc7915a
@@ -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();
|
||||
});
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user