Skip to content

Commit

Permalink
fix(menu): recompute index before marking selection
Browse files Browse the repository at this point in the history
Fixed issues with changing menu contents in menus with
selectable items. Marking selected items in a menu is
deferred until the menu closes, so if the menu contents
change before the callback is executed, the wrong item
can be marked in the DOM.
  • Loading branch information
lasalvavida committed Sep 4, 2019
1 parent 4c4342d commit 33b9554
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 33b9554

Please sign in to comment.