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

Commit

Permalink
fix(list): Update clickable elements selector (#3312)
Browse files Browse the repository at this point in the history
(cherry picked from commit c8ded0a)
  • Loading branch information
williamernest authored and Kenneth G. Franqueiro committed Aug 13, 2018
1 parent 85000e8 commit c4fc932
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 14 deletions.
4 changes: 2 additions & 2 deletions packages/mdc-list/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ single list item to become selected or deselected.

```html
<ul id="my-list" class="mdc-list" aria-orientation="vertical">
<li class="mdc-list-item">Single-line item</li>
<li class="mdc-list-item" tabindex="0">Single-line item</li>
<li class="mdc-list-item">Single-line item</li>
<li class="mdc-list-item">Single-line item</li>
</ul>
Expand All @@ -158,7 +158,7 @@ the `mdc-list-item--selected` class and `aria-selected="true"` attribute before
```html
<ul id="my-list" class="mdc-list" aria-orientation="vertical">
<li class="mdc-list-item">Single-line item</li>
<li class="mdc-list-item mdc-list-item--selected" aria-selected="true">Single-line item</li>
<li class="mdc-list-item mdc-list-item--selected" aria-selected="true" tabindex="0">Single-line item</li>
<li class="mdc-list-item">Single-line item</li>
</ul>
```
Expand Down
4 changes: 2 additions & 2 deletions packages/mdc-list/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ const cssClasses = {
/** @enum {string} */
const strings = {
ARIA_ORIENTATION: 'aria-orientation',
ARIA_ORIENTATION_VERTICAL: 'vertical',
ARIA_ORIENTATION_HORIZONTAL: 'horizontal',
ARIA_SELECTED: 'aria-selected',
FOCUSABLE_CHILD_ELEMENTS: 'button:not(:disabled), a',
FOCUSABLE_CHILD_ELEMENTS: `.${cssClasses.LIST_ITEM_CLASS} button:not(:disabled), .${cssClasses.LIST_ITEM_CLASS} a`,
ENABLED_ITEMS_SELECTOR: '.mdc-list-item:not(.mdc-list-item--disabled)',
};

Expand Down
2 changes: 1 addition & 1 deletion packages/mdc-list/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class MDCList extends MDCComponent {

layout() {
const direction = this.root_.getAttribute(strings.ARIA_ORIENTATION);
this.vertical = direction === strings.ARIA_ORIENTATION_VERTICAL;
this.vertical = direction !== strings.ARIA_ORIENTATION_HORIZONTAL;

// List items need to have at least tabindex=-1 to be focusable.
[].slice.call(this.root_.querySelectorAll('.mdc-list-item:not([tabindex])'))
Expand Down
36 changes: 27 additions & 9 deletions test/unit/mdc-list/mdc-list.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ function getFixture() {
`;
}

function setupTest() {
const root = getFixture();
function setupTest(root = getFixture()) {
const MockFoundationCtor = td.constructor(MDCListFoundation);
const mockFoundation = new MockFoundationCtor();
const component = new MDCList(root, mockFoundation);
Expand All @@ -54,6 +53,25 @@ test('attachTo initializes and returns a MDCList instance', () => {
assert.isTrue(MDCList.attachTo(getFixture()) instanceof MDCList);
});

test('component calls setVerticalOrientation on the foundation if aria-orientation is not set', () => {
const {mockFoundation} = setupTest();
td.verify(mockFoundation.setVerticalOrientation(true), {times: 1});
});

test('component calls setVerticalOrientation(false) on the foundation if aria-orientation=horizontal', () => {
const root = getFixture();
root.setAttribute('aria-orientation', 'horizontal');
const {mockFoundation} = setupTest(root);
td.verify(mockFoundation.setVerticalOrientation(false), {times: 1});
});

test('component calls setVerticalOrientation(true) on the foundation if aria-orientation=vertical', () => {
const root = getFixture();
root.setAttribute('aria-orientation', 'vertical');
const {mockFoundation} = setupTest(root);
td.verify(mockFoundation.setVerticalOrientation(true), {times: 1});
});

test('#adapter.getListItemCount returns correct number of list items', () => {
const {root, component} = setupTest();
document.body.appendChild(root);
Expand Down Expand Up @@ -177,33 +195,33 @@ test('#adapter.focusItemAtIndex focuses the list item at the index specified', (
});

test('adapter#isListItem returns true if the element is a list item', () => {
const {root, component} = setupTest(true);
const {root, component} = setupTest();
const item1 = root.querySelectorAll('.mdc-list-item')[0];
assert.isTrue(component.getDefaultFoundation().adapter_.isListItem(item1));
});

test('adapter#isListItem returns false if the element is a not a list item', () => {
const {root, component} = setupTest(true);
const {root, component} = setupTest();
const item1 = root.querySelectorAll('.mdc-list-item button')[0];
assert.isFalse(component.getDefaultFoundation().adapter_.isListItem(item1));
});

test('adapter#isElementFocusable returns true if the element is a focusable list item sub-element', () => {
const {root, component} = setupTest(true);
const {root, component} = setupTest();
const item1 = root.querySelectorAll('.mdc-list-item button')[0];
assert.isTrue(component.getDefaultFoundation().adapter_.isElementFocusable(item1));
});

test('adapter#isElementFocusable returns false if the element is not a focusable list item sub-element',
() => {
const {root, component} = setupTest(true);
const {root, component} = setupTest();
const item1 = root.querySelectorAll('.mdc-list-item')[2];
assert.isFalse(component.getDefaultFoundation().adapter_.isElementFocusable(item1));
});

test('adapter#isElementFocusable returns false if the element is null/undefined',
() => {
const {component} = setupTest(true);
const {component} = setupTest();
assert.isFalse(component.getDefaultFoundation().adapter_.isElementFocusable());
});

Expand Down Expand Up @@ -231,8 +249,8 @@ test('layout adds tabindex=-1 to all list item button/a elements', () => {

test('vertical calls setVerticalOrientation on foundation', () => {
const {component, mockFoundation} = setupTest();
component.vertical = true;
td.verify(mockFoundation.setVerticalOrientation(true), {times: 1});
component.vertical = false;
td.verify(mockFoundation.setVerticalOrientation(false), {times: 1});
});

test('wrapFocus calls setWrapFocus on foundation', () => {
Expand Down

0 comments on commit c4fc932

Please sign in to comment.