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

Commit

Permalink
fix(list): Add #adapter.listItemAtIndexHasClass to prevent user state…
Browse files Browse the repository at this point in the history
… change to disabled items (#4922)

BREAKING CHANGE: New adapter method listItemAtIndexHasClass
  • Loading branch information
Matt Goo authored and allan-chen committed Sep 24, 2019
1 parent e2506f4 commit b6d213c
Show file tree
Hide file tree
Showing 10 changed files with 422 additions and 5 deletions.
1 change: 1 addition & 0 deletions packages/mdc-list/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,7 @@ Method Signature | Description
`notifyAction(index: number) => void` | Notifies user action on list item including keyboard and mouse actions.
`isFocusInsideList() => boolean` | Returns true if the current focused element is inside list root.
`isRootFocused() => boolean` | Returns true if root element is focused.
`listItemAtIndexHasClass(index: number, className: string) => boolean` | Returns true if list item at `index` has class `className`.

### `MDCListFoundation`

Expand Down
7 changes: 7 additions & 0 deletions packages/mdc-list/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,13 @@ export interface MDCListAdapter {
*/
isRootFocused(): boolean;

/**
* @param index list item index.
* @param className the name of the class whose presence is to be checked.
* @return true if list item at `index` has class `className`.
*/
listItemAtIndexHasClass(index: number, className: string): boolean;

/**
* Sets the checked status of checkbox or radio at given list item index.
*/
Expand Down
1 change: 1 addition & 0 deletions packages/mdc-list/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ export class MDCList extends MDCComponent<MDCListFoundation> {
return this.root_.contains(document.activeElement);
},
isRootFocused: () => document.activeElement === this.root_,
listItemAtIndexHasClass: (index, className) => this.listElements[index].classList.contains(className),
notifyAction: (index) => {
this.emit<MDCListActionEventDetail>(strings.ACTION_EVENT, {index}, /** shouldBubble */ true);
},
Expand Down
6 changes: 3 additions & 3 deletions packages/mdc-list/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ const strings = {
ARIA_ORIENTATION_HORIZONTAL: 'horizontal',
ARIA_ROLE_CHECKBOX_SELECTOR: '[role="checkbox"]',
ARIA_SELECTED: 'aria-selected',
CHECKBOX_RADIO_SELECTOR: 'input[type="checkbox"]:not(:disabled), input[type="radio"]:not(:disabled)',
CHECKBOX_SELECTOR: 'input[type="checkbox"]:not(:disabled)',
CHECKBOX_RADIO_SELECTOR: 'input[type="checkbox"], input[type="radio"]',
CHECKBOX_SELECTOR: 'input[type="checkbox"]',
CHILD_ELEMENTS_TO_TOGGLE_TABINDEX: `
.${cssClasses.LIST_ITEM_CLASS} button:not(:disabled),
.${cssClasses.LIST_ITEM_CLASS} a
Expand All @@ -52,7 +52,7 @@ const strings = {
.${cssClasses.LIST_ITEM_CLASS} input[type="radio"]:not(:disabled),
.${cssClasses.LIST_ITEM_CLASS} input[type="checkbox"]:not(:disabled)
`,
RADIO_SELECTOR: 'input[type="radio"]:not(:disabled)',
RADIO_SELECTOR: 'input[type="radio"]',
};

const numbers = {
Expand Down
9 changes: 9 additions & 0 deletions packages/mdc-list/foundation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export class MDCListFoundation extends MDCFoundation<MDCListAdapter> {
isCheckboxCheckedAtIndex: () => false,
isFocusInsideList: () => false,
isRootFocused: () => false,
listItemAtIndexHasClass: () => false,
notifyAction: () => undefined,
removeClassForElementIndex: () => undefined,
setAttributeForElementIndex: () => undefined,
Expand Down Expand Up @@ -462,7 +463,15 @@ export class MDCListFoundation extends MDCFoundation<MDCListAdapter> {
return index >= 0 && index < listSize;
}

/**
* Sets selected index on user action, toggles checkbox / radio based on toggleCheckbox value.
* User interaction should not toggle list item(s) when disabled.
*/
private setSelectedIndexOnAction_(index: number, toggleCheckbox = true) {
if (this.adapter_.listItemAtIndexHasClass(index, cssClasses.LIST_ITEM_DISABLED_CLASS)) {
return;
}

if (this.isCheckboxList_) {
this.toggleCheckboxAtIndex_(index, toggleCheckbox);
} else {
Expand Down
16 changes: 16 additions & 0 deletions test/screenshot/golden.json
Original file line number Diff line number Diff line change
Expand Up @@ -767,6 +767,14 @@
"desktop_windows_ie@11": "https://storage.googleapis.com/mdc-web-screenshot-tests/travis/2018/11/28/20_57_26_802/spec/mdc-list/classes/checkbox.html.windows_ie_11.png"
}
},
"spec/mdc-list/classes/disabled-checkbox.html": {
"public_url": "https://storage.googleapis.com/mdc-web-screenshot-tests/allanchen/2019/09/19/18_00_58_703/spec/mdc-list/classes/disabled-checkbox.html?utm_source=golden_json",
"screenshots": {
"desktop_windows_chrome@latest": "https://storage.googleapis.com/mdc-web-screenshot-tests/allanchen/2019/09/19/18_00_58_703/spec/mdc-list/classes/disabled-checkbox.html.windows_chrome_76.png",
"desktop_windows_firefox@latest": "https://storage.googleapis.com/mdc-web-screenshot-tests/allanchen/2019/09/19/18_00_58_703/spec/mdc-list/classes/disabled-checkbox.html.windows_firefox_69.png",
"desktop_windows_ie@11": "https://storage.googleapis.com/mdc-web-screenshot-tests/allanchen/2019/09/19/18_00_58_703/spec/mdc-list/classes/disabled-checkbox.html.windows_ie_11.png"
}
},
"spec/mdc-list/classes/disabled-items.html": {
"public_url": "https://storage.googleapis.com/mdc-web-screenshot-tests/travis/2019/05/10/14_25_41_111/spec/mdc-list/classes/disabled-items.html?utm_source=golden_json",
"screenshots": {
Expand All @@ -775,6 +783,14 @@
"desktop_windows_ie@11": "https://storage.googleapis.com/mdc-web-screenshot-tests/travis/2019/04/08/21_52_36_805/spec/mdc-list/classes/disabled-items.html.windows_ie_11.png"
}
},
"spec/mdc-list/classes/disabled-radio.html": {
"public_url": "https://storage.googleapis.com/mdc-web-screenshot-tests/allanchen/2019/09/19/18_00_58_703/spec/mdc-list/classes/disabled-radio.html?utm_source=golden_json",
"screenshots": {
"desktop_windows_chrome@latest": "https://storage.googleapis.com/mdc-web-screenshot-tests/allanchen/2019/09/19/18_00_58_703/spec/mdc-list/classes/disabled-radio.html.windows_chrome_76.png",
"desktop_windows_firefox@latest": "https://storage.googleapis.com/mdc-web-screenshot-tests/allanchen/2019/09/19/18_00_58_703/spec/mdc-list/classes/disabled-radio.html.windows_firefox_69.png",
"desktop_windows_ie@11": "https://storage.googleapis.com/mdc-web-screenshot-tests/allanchen/2019/09/19/18_00_58_703/spec/mdc-list/classes/disabled-radio.html.windows_ie_11.png"
}
},
"spec/mdc-list/classes/list-group.html": {
"public_url": "https://storage.googleapis.com/mdc-web-screenshot-tests/abhiomkar/2019/09/11/16_03_07_425/spec/mdc-list/classes/list-group.html?utm_source=golden_json",
"screenshots": {
Expand Down
158 changes: 158 additions & 0 deletions test/screenshot/spec/mdc-list/classes/disabled-checkbox.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
<!DOCTYPE html>
<!--
Copyright 2018 Google Inc.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-->
<html lang="en">
<head>
<meta charset="utf-8">
<title>Disabled Checkbox List - MDC Web Screenshot Test</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="../../../out/mdc.list.css">
<link rel="stylesheet" href="../../../out/mdc.checkbox.css">
<link rel="stylesheet" href="../../../out/spec/fixture.css">
<link rel="stylesheet" href="../../../out/spec/mdc-list/fixture.css">

<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-118996389-2"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-118996389-2');
</script>
</head>

<body class="test-container">
<main class="test-viewport test-viewport--mobile">
<div class="test-layout">
<div class="test-cell test-cell--list">
<ul class="mdc-list test-checkbox-list" style="list-style-type: none;" role="group" aria-label="Checkbox options">
<li class="mdc-list-item" role="checkbox" aria-checked="false">
<span class="mdc-list-item__graphic">
<div class="mdc-checkbox">
<input type="checkbox"
class="mdc-checkbox__native-control"
id="test-list-checkbox-item-1" />
<div class="mdc-checkbox__background">
<svg class="mdc-checkbox__checkmark"
viewBox="0 0 24 24">
<path class="mdc-checkbox__checkmark-path"
fill="none"
d="M1.73,12.91 8.1,19.28 22.79,4.59"/>
</svg>
<div class="mdc-checkbox__mixedmark"></div>
</div>
</div>
</span>
<label class="mdc-list-item__text test-list-item__label" for="test-list-checkbox-item-1">Option 1</label>
</li>
<li class="mdc-list-item mdc-list-item--disabled" role="checkbox" aria-checked="true" tabindex="0">
<span class="mdc-list-item__graphic">
<div class="mdc-checkbox mdc-checkbox--disabled">
<input type="checkbox"
class="mdc-checkbox__native-control"
id="test-list-checkbox-item-2"
checked
disabled />
<div class="mdc-checkbox__background">
<svg class="mdc-checkbox__checkmark"
viewBox="0 0 24 24">
<path class="mdc-checkbox__checkmark-path"
fill="none"
d="M1.73,12.91 8.1,19.28 22.79,4.59"/>
</svg>
<div class="mdc-checkbox__mixedmark"></div>
</div>
</div>
</span>
<label class="mdc-list-item__text test-list-item__label" for="test-list-checkbox-item-2">Option 2</label>
</li>
<li class="mdc-list-item" role="checkbox" aria-checked="false">
<span class="mdc-list-item__graphic">
<div class="mdc-checkbox">
<input type="checkbox"
class="mdc-checkbox__native-control"
id="test-list-checkbox-item-3" />
<div class="mdc-checkbox__background">
<svg class="mdc-checkbox__checkmark"
viewBox="0 0 24 24">
<path class="mdc-checkbox__checkmark-path"
fill="none"
d="M1.73,12.91 8.1,19.28 22.79,4.59"/>
</svg>
<div class="mdc-checkbox__mixedmark"></div>
</div>
</div>
</span>
<label class="mdc-list-item__text test-list-item__label" for="test-list-checkbox-item-3">Option 3</label>
</li>
<li class="mdc-list-item" role="checkbox" aria-checked="false">
<span class="mdc-list-item__graphic">
<div class="mdc-checkbox">
<input type="checkbox"
class="mdc-checkbox__native-control"
id="test-list-checkbox-item-4" />
<div class="mdc-checkbox__background">
<svg class="mdc-checkbox__checkmark"
viewBox="0 0 24 24">
<path class="mdc-checkbox__checkmark-path"
fill="none"
d="M1.73,12.91 8.1,19.28 22.79,4.59"/>
</svg>
<div class="mdc-checkbox__mixedmark"></div>
</div>
</div>
</span>
<label class="mdc-list-item__text test-list-item__label" for="test-list-checkbox-item-4">Option 4</label>
</li>
<li class="mdc-list-item" role="checkbox" aria-checked="false">
<span class="mdc-list-item__graphic">
<div class="mdc-checkbox">
<input type="checkbox"
class="mdc-checkbox__native-control"
id="test-list-checkbox-item-5" />
<div class="mdc-checkbox__background">
<svg class="mdc-checkbox__checkmark"
viewBox="0 0 24 24">
<path class="mdc-checkbox__checkmark-path"
fill="none"
d="M1.73,12.91 8.1,19.28 22.79,4.59"/>
</svg>
<div class="mdc-checkbox__mixedmark"></div>
</div>
</div>
</span>
<label class="mdc-list-item__text test-list-item__label" for="test-list-checkbox-item-5">Option 5</label>
</li>
</ul>
</div>
</div>
</main>

<!-- Automatically provides/replaces `Promise` if missing or broken -->
<script src="https://cdn.jsdelivr.net/npm/es6-promise@4/dist/es6-promise.js"></script>
<script src="https://cdn.jsdelivr.net/npm/es6-promise@4/dist/es6-promise.auto.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/fontfaceobserver/2.0.13/fontfaceobserver.standalone.js"></script>
<script src="../../../out/material-components-web.js"></script>
<script src="../../../out/spec/fixture.js"></script>
<script src="../../../out/spec/mdc-list/fixture.js"></script>
</body>
</html>
Loading

0 comments on commit b6d213c

Please sign in to comment.