Skip to content
This repository has been archived by the owner on Jan 13, 2025. It is now read-only.

Commit

Permalink
fix(menu): recompute index before marking selection (#5047)
Browse files Browse the repository at this point in the history
  • Loading branch information
lasalvavida authored and abhiomkar committed Sep 6, 2019
1 parent 7ec18c6 commit 90f6247
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
6 changes: 4 additions & 2 deletions packages/mdc-menu/foundation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,10 @@ export class MDCMenuFoundation extends MDCFoundation<MDCMenuAdapter> {

// Wait for the menu to close before adding/removing classes that affect styles.
this.closeAnimationEndTimerId_ = setTimeout(() => {
if (this.adapter_.isSelectableItemAtIndex(index)) {
this.setSelectedIndex(index);
// Recompute the index in case the menu contents have changed.
const recomputedIndex = this.adapter_.getElementIndex(listItem);
if (this.adapter_.isSelectableItemAtIndex(recomputedIndex)) {
this.setSelectedIndex(recomputedIndex);
}
}, MDCMenuSurfaceFoundation.numbers.TRANSITION_CLOSE_DURATION);
}
Expand Down
24 changes: 24 additions & 0 deletions test/unit/mdc-menu/menu.foundation.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,30 @@ test('handleItemAction item action event inside of a child element of a selectio
{times: 0});
});

test('handleItemAction adds class to the correct child element of a selection group when menu has mutated', () => {
const {foundation, mockAdapter, clock} = setupTest();
const itemEl = document.createElement('li');
td.when(mockAdapter.elementContainsClass(itemEl, listClasses.LIST_ITEM_CLASS)).thenReturn(true);
td.when(mockAdapter.getElementIndex(itemEl)).thenReturn(1);
td.when(mockAdapter.elementContainsClass(itemEl, cssClasses.MENU_SELECTION_GROUP)).thenReturn(true);

td.when(mockAdapter.isSelectableItemAtIndex(1)).thenReturn(true);
td.when(mockAdapter.getSelectedSiblingOfItemAtIndex(1)).thenReturn(-1);
td.when(mockAdapter.getMenuItemCount()).thenReturn(2);

foundation.handleItemAction(itemEl);

// Element at index 1 is now at index 0
td.when(mockAdapter.getElementIndex(itemEl)).thenReturn(0);
td.when(mockAdapter.isSelectableItemAtIndex(0)).thenReturn(true);
td.when(mockAdapter.getSelectedSiblingOfItemAtIndex(0)).thenReturn(-1);
td.when(mockAdapter.getMenuItemCount()).thenReturn(1);

clock.tick(numbers.TRANSITION_CLOSE_DURATION);

td.verify(mockAdapter.addClassToElementAtIndex(0, cssClasses.MENU_SELECTED_LIST_ITEM), {times: 1});
});

test('handleMenuSurfaceOpened menu focuses the list root element by default on menu surface opened', () => {
const {foundation, mockAdapter} = setupTest();

Expand Down

0 comments on commit 90f6247

Please sign in to comment.