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

fix(list): added #adapter.listItemAtIndexHasClass to prevent user state change to disabled items #4922

Merged
merged 10 commits into from
Sep 24, 2019
1 change: 1 addition & 0 deletions packages/mdc-list/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,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.
`isListItemDisabled(index: number) => boolean` | Returns true if root element is disabled.

### `MDCListFoundation`

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

/**
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You also need to sync this to internal and implement this adapter before merging it.

* @param index list item index.
* @return true if root element is disabled.
*/
isListItemDisabled(index: number): boolean;
allan-chen marked this conversation as resolved.
Show resolved Hide resolved

/**
* Sets the checked status of checkbox or radio at given list item index.
*/
Expand Down
5 changes: 3 additions & 2 deletions packages/mdc-list/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,12 +158,13 @@ export class MDCList extends MDCComponent<MDCListFoundation> {
},
isCheckboxCheckedAtIndex: (index) => {
const listItem = this.listElements[index];
const toggleEl = listItem.querySelector<HTMLInputElement>(strings.CHECKBOX_SELECTOR);
const toggleEl = listItem.querySelector<HTMLInputElement>(strings.ENABLED_CHECKBOX_SELECTOR);
return toggleEl!.checked;
},
isFocusInsideList: () => {
return this.root_.contains(document.activeElement);
},
isListItemDisabled: (index) => this.listElements[index].classList.contains(cssClasses.LIST_ITEM_DISABLED_CLASS),
isRootFocused: () => document.activeElement === this.root_,
notifyAction: (index) => {
this.emit<MDCListActionEventDetail>(strings.ACTION_EVENT, {index}, /** shouldBubble */ true);
Expand All @@ -182,7 +183,7 @@ export class MDCList extends MDCComponent<MDCListFoundation> {
},
setCheckedCheckboxOrRadioAtIndex: (index, isChecked) => {
const listItem = this.listElements[index];
const toggleEl = listItem.querySelector<HTMLInputElement>(strings.CHECKBOX_RADIO_SELECTOR);
const toggleEl = listItem.querySelector<HTMLInputElement>(strings.ENABLED_CHECKBOX_RADIO_SELECTOR);
toggleEl!.checked = isChecked;

const event = document.createEvent('Event');
Expand Down
2 changes: 2 additions & 0 deletions packages/mdc-list/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ const strings = {
.${cssClasses.LIST_ITEM_CLASS} button:not(:disabled),
.${cssClasses.LIST_ITEM_CLASS} a
`,
ENABLED_CHECKBOX_RADIO_SELECTOR: 'input[type="checkbox"], input[type="radio"]',
allan-chen marked this conversation as resolved.
Show resolved Hide resolved
ENABLED_CHECKBOX_SELECTOR: 'input[type="checkbox"]',
FOCUSABLE_CHILD_ELEMENTS: `
.${cssClasses.LIST_ITEM_CLASS} button:not(:disabled),
.${cssClasses.LIST_ITEM_CLASS} a,
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 @@ -56,6 +56,7 @@ export class MDCListFoundation extends MDCFoundation<MDCListAdapter> {
hasRadioAtIndex: () => false,
isCheckboxCheckedAtIndex: () => false,
isFocusInsideList: () => false,
isListItemDisabled: () => false,
isRootFocused: () => false,
notifyAction: () => undefined,
removeClassForElementIndex: () => undefined,
Expand Down Expand Up @@ -444,7 +445,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_.isListItemDisabled(index)) {
return;
}
moog16 marked this conversation as resolved.
Show resolved Hide resolved

if (this.isCheckboxList_) {
this.toggleCheckboxAtIndex_(index, toggleCheckbox);
} else {
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>
143 changes: 143 additions & 0 deletions test/screenshot/spec/mdc-list/classes/disabled-radio.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
<!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 Radio 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.radio.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-radio-list" role="radiogroup" style="list-style-type: none;">
<li class="mdc-list-item" role="radio" aria-checked="false">
<span class="mdc-list-item__graphic">
<div class="mdc-radio">
<input class="mdc-radio__native-control"
type="radio"
id="test-list-radio-item-1"
name="test-list-radio-item-group"
value="1">
<div class="mdc-radio__background">
<div class="mdc-radio__outer-circle"></div>
<div class="mdc-radio__inner-circle"></div>
</div>
</div>
</span>
<label class="mdc-list-item__text test-list-item__label" for="test-list-radio-item-1">Option 1</label>
</li>
<li class="mdc-list-item mdc-list-item--disabled" role="radio" aria-checked="true" tabindex="0">
<span class="mdc-list-item__graphic">
<div class="mdc-radio mdc-radio--disabled">
<input class="mdc-radio__native-control"
type="radio"
id="test-list-radio-item-2"
name="test-list-radio-item-group"
value="2"
checked
disabled >
<div class="mdc-radio__background">
<div class="mdc-radio__outer-circle"></div>
<div class="mdc-radio__inner-circle"></div>
</div>
</div>
</span>
<label class="mdc-list-item__text test-list-item__label" for="test-list-radio-item-2">Option 2</label>
</li>
<li class="mdc-list-item" role="radio" aria-checked="false">
<span class="mdc-list-item__graphic">
<div class="mdc-radio">
<input class="mdc-radio__native-control"
type="radio"
id="test-list-radio-item-3"
name="test-list-radio-item-group"
value="3">
<div class="mdc-radio__background">
<div class="mdc-radio__outer-circle"></div>
<div class="mdc-radio__inner-circle"></div>
</div>
</div>
</span>
<label class="mdc-list-item__text test-list-item__label" for="test-list-radio-item-3">Option 3</label>
</li>
<li class="mdc-list-item" role="radio" aria-checked="false">
<span class="mdc-list-item__graphic">
<div class="mdc-radio">
<input class="mdc-radio__native-control"
type="radio"
id="test-list-radio-item-4"
name="test-list-radio-item-group"
value="4">
<div class="mdc-radio__background">
<div class="mdc-radio__outer-circle"></div>
<div class="mdc-radio__inner-circle"></div>
</div>
</div>
</span>
<label class="mdc-list-item__text test-list-item__label" for="test-list-radio-item-4">Option 4</label>
</li>
<li class="mdc-list-item" role="radio" aria-checked="false">
<span class="mdc-list-item__graphic">
<div class="mdc-radio">
<input class="mdc-radio__native-control"
type="radio"
id="test-list-radio-item-5"
name="test-list-radio-item-group"
value="5">
<div class="mdc-radio__background">
<div class="mdc-radio__outer-circle"></div>
<div class="mdc-radio__inner-circle"></div>
</div>
</div>
</span>
<label class="mdc-list-item__text test-list-item__label" for="test-list-radio-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