From 13abb24232b18a54d85c7fde4f0b30a633977e05 Mon Sep 17 00:00:00 2001 From: Will Ernest <34519388+williamernest@users.noreply.github.com> Date: Thu, 13 Sep 2018 15:45:47 -0500 Subject: [PATCH] feat(list): Update list to toggle tabindex of radio/checkbox (#3542) --- packages/mdc-list/README.md | 22 +++++++++++----------- packages/mdc-list/constants.js | 4 +++- test/unit/mdc-list/mdc-list.test.js | 21 ++++++++++++++------- 3 files changed, 28 insertions(+), 19 deletions(-) diff --git a/packages/mdc-list/README.md b/packages/mdc-list/README.md index 28c0d06edcc..aa18973ff11 100644 --- a/packages/mdc-list/README.md +++ b/packages/mdc-list/README.md @@ -268,12 +268,12 @@ The MDCList JavaScript component implements the WAI-ARIA best practices for [Listbox](https://www.w3.org/TR/wai-aria-practices-1.1/#Listbox). This includes overriding the default tab behavior within the list component. You should not add `tabindex` to any of the `li` elements in a list. -As the user navigates through the list, any `button` or `a` elements within the list will receive `tabindex="-1"` -when the list item is not focused. When the list item receives focus, the child `button` and `a` elements will -receive `tabIndex="0"`. This allows for the user to tab through list item elements and then tab to the -first element after the list. The `Arrow`, `Home`, and `End` keys should be used for navigating internal list elements. -If `singleSelection=true`, the list will allow the user to use the `Space` or `Enter` keys to select or deselect -a list item. The MDCList will perform the following actions for each key press +As the user navigates through the list, any `button`, `a`, `input[type="radio"]` and `input[type="checkbox"]` elements +within the list will receive `tabindex="-1"` when the list item is not focused. When the list item receives focus, the +aforementioned elements will receive `tabIndex="0"`. This allows for the user to tab through list item elements +and then tab to the first element after the list. The `Arrow`, `Home`, and `End` keys should be used for navigating +internal list elements. If `singleSelection=true`, the list will allow the user to use the `Space` or `Enter` keys to +select or deselect a list item. The MDCList will perform the following actions for each key press Key | Action --- | --- @@ -300,8 +300,8 @@ The default component requires that every list item receives a `tabindex` value (`li` elements cannot receive focus at all without a `tabindex` value). Any element not already containing a `tabindex` attribute will receive `tabindex=-1`. The first list item should have `tabindex="0"` so that the user can find the first element using the `tab` key, but subsequent `tab` keys strokes will cause focus to -skip over the entire list. If the list items contain sub-elements that are focusable (`button` or `a` elements), -these should also receive `tabIndex="-1"`. +skip over the entire list. If the list items contain sub-elements that are focusable (`button`, `a`, +`input[type="radio]`, and `input[type="checkbox"]` elements), these should also receive `tabIndex="-1"`. ```html `; @@ -216,15 +221,17 @@ test('adapter#focusItemAtIndex focuses the list item at the index specified', () document.body.removeChild(root); }); -test('adapter#setTabIndexForListItemChildren sets the child button/a elements of index', () => { +test('adapter#setTabIndexForListItemChildren sets the child button/a/radio/checkbox elements of index', () => { const {root, component} = setupTest(); document.body.appendChild(root); - const listItemIndex = 1; - const listItem = root.querySelectorAll('.mdc-list-item')[listItemIndex]; - component.getDefaultFoundation().adapter_.setTabIndexForListItemChildren(listItemIndex, 0); + const listItems = root.querySelectorAll('.mdc-list-item'); + + for (let index = 0; index < listItems.length; index++) { + assert.equal(0, listItems[index].querySelectorAll('[tabindex="0"]').length); + component.getDefaultFoundation().adapter_.setTabIndexForListItemChildren(index, 0); + assert.equal(1, listItems[index].querySelectorAll('[tabindex="0"]').length); + } - assert.equal(1, root.querySelectorAll('button[tabindex="0"]').length); - assert.equal(listItem, root.querySelectorAll('button[tabindex="0"]')[0].parentElement); document.body.removeChild(root); });