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

Commit

Permalink
fix(list): Always call followHref regardless of single-selection mode (
Browse files Browse the repository at this point in the history
  • Loading branch information
kfranqueiro authored Sep 20, 2018
1 parent bce1724 commit b556724
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
10 changes: 6 additions & 4 deletions packages/mdc-list/foundation.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,10 +195,12 @@ class MDCListFoundation extends MDCFoundation {
this.preventDefaultEvent_(evt);
this.focusLastElement();
} else if (isEnter || isSpace) {
if (this.isSingleSelectionList_ && isRootListItem) {
// Check if the space key was pressed on the list item or a child element.
this.setSelectedIndex(currentIndex);
this.preventDefaultEvent_(evt);
if (isRootListItem) {
if (this.isSingleSelectionList_) {
// Check if the space key was pressed on the list item or a child element.
this.setSelectedIndex(currentIndex);
this.preventDefaultEvent_(evt);
}

// Explicitly activate links, since we're preventing default on Enter, and Space doesn't activate them.
this.adapter_.followHref(currentIndex);
Expand Down
22 changes: 21 additions & 1 deletion test/unit/mdc-list/foundation.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -416,12 +416,32 @@ test('#handleKeydown space/enter key does not cause event.preventDefault when si
td.when(mockAdapter.toggleCheckbox(0)).thenReturn(false);
foundation.setSingleSelection(false);
foundation.handleKeydown(event, true, 0);
event.key = 'Enter';
event.key = 'Space';
foundation.handleKeydown(event, true, 0);

td.verify(preventDefault(), {times: 0});
});

test('#handleKeydown space/enter key call adapter.followHref regardless of singleSelection', () => {
const {foundation, mockAdapter} = setupTest();
const target = {classList: ['mdc-list-item']};
const event = {key: 'Enter', target, preventDefault: () => {}};

td.when(mockAdapter.getFocusedElementIndex()).thenReturn(0);
td.when(mockAdapter.getListItemCount()).thenReturn(3);
td.when(mockAdapter.toggleCheckbox(0)).thenReturn(false);
foundation.setSingleSelection(false);
foundation.handleKeydown(event, true, 0);
foundation.setSingleSelection(true);
foundation.handleKeydown(event, true, 0);
event.key = 'Space';
foundation.handleKeydown(event, true, 0);
foundation.setSingleSelection(false);
foundation.handleKeydown(event, true, 0);

td.verify(mockAdapter.followHref(0), {times: 4});
});

test('#handleKeydown space key does not cause preventDefault to be called if singleSelection=false', () => {
const {foundation, mockAdapter} = setupTest();
const preventDefault = td.func('preventDefault');
Expand Down

0 comments on commit b556724

Please sign in to comment.