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

Commit b556724

Browse files
authored
fix(list): Always call followHref regardless of single-selection mode (#3595)
1 parent bce1724 commit b556724

File tree

2 files changed

+27
-5
lines changed

2 files changed

+27
-5
lines changed

packages/mdc-list/foundation.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -195,10 +195,12 @@ class MDCListFoundation extends MDCFoundation {
195195
this.preventDefaultEvent_(evt);
196196
this.focusLastElement();
197197
} else if (isEnter || isSpace) {
198-
if (this.isSingleSelectionList_ && isRootListItem) {
199-
// Check if the space key was pressed on the list item or a child element.
200-
this.setSelectedIndex(currentIndex);
201-
this.preventDefaultEvent_(evt);
198+
if (isRootListItem) {
199+
if (this.isSingleSelectionList_) {
200+
// Check if the space key was pressed on the list item or a child element.
201+
this.setSelectedIndex(currentIndex);
202+
this.preventDefaultEvent_(evt);
203+
}
202204

203205
// Explicitly activate links, since we're preventing default on Enter, and Space doesn't activate them.
204206
this.adapter_.followHref(currentIndex);

test/unit/mdc-list/foundation.test.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -416,12 +416,32 @@ test('#handleKeydown space/enter key does not cause event.preventDefault when si
416416
td.when(mockAdapter.toggleCheckbox(0)).thenReturn(false);
417417
foundation.setSingleSelection(false);
418418
foundation.handleKeydown(event, true, 0);
419-
event.key = 'Enter';
419+
event.key = 'Space';
420420
foundation.handleKeydown(event, true, 0);
421421

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

425+
test('#handleKeydown space/enter key call adapter.followHref regardless of singleSelection', () => {
426+
const {foundation, mockAdapter} = setupTest();
427+
const target = {classList: ['mdc-list-item']};
428+
const event = {key: 'Enter', target, preventDefault: () => {}};
429+
430+
td.when(mockAdapter.getFocusedElementIndex()).thenReturn(0);
431+
td.when(mockAdapter.getListItemCount()).thenReturn(3);
432+
td.when(mockAdapter.toggleCheckbox(0)).thenReturn(false);
433+
foundation.setSingleSelection(false);
434+
foundation.handleKeydown(event, true, 0);
435+
foundation.setSingleSelection(true);
436+
foundation.handleKeydown(event, true, 0);
437+
event.key = 'Space';
438+
foundation.handleKeydown(event, true, 0);
439+
foundation.setSingleSelection(false);
440+
foundation.handleKeydown(event, true, 0);
441+
442+
td.verify(mockAdapter.followHref(0), {times: 4});
443+
});
444+
425445
test('#handleKeydown space key does not cause preventDefault to be called if singleSelection=false', () => {
426446
const {foundation, mockAdapter} = setupTest();
427447
const preventDefault = td.func('preventDefault');

0 commit comments

Comments
 (0)