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

Commit 9ec35b7

Browse files
authoredNov 30, 2017
fix(icon-toggle): Don't nuke tabindex if initializing disabled to false (#1667)
1 parent 7a23183 commit 9ec35b7

File tree

2 files changed

+32
-7
lines changed

2 files changed

+32
-7
lines changed
 

‎packages/mdc-icon-toggle/foundation.js

+1
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ class MDCIconToggleFoundation extends MDCFoundation {
8989

9090
init() {
9191
this.refreshToggleData();
92+
this.savedTabIndex_ = this.adapter_.getTabIndex();
9293
this.adapter_.registerInteractionHandler('click', this.clickHandler_);
9394
this.adapter_.registerInteractionHandler('keydown', this.keydownHandler_);
9495
this.adapter_.registerInteractionHandler('keyup', this.keyupHandler_);

‎test/unit/mdc-icon-toggle/mdc-icon-toggle.test.js

+31-7
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,17 @@ import {MDCIconToggle, MDCIconToggleFoundation} from '../../../packages/mdc-icon
2525
import {MDCRipple} from '../../../packages/mdc-ripple';
2626
import {cssClasses} from '../../../packages/mdc-ripple/constants';
2727

28-
function setupTest({useInnerIconElement = false} = {}) {
28+
function setupTest({tabIndex = undefined, useInnerIconElement = false} = {}) {
2929
const root = document.createElement(useInnerIconElement ? 'span' : 'i');
3030
if (useInnerIconElement) {
3131
const icon = document.createElement('i');
3232
icon.id = 'icon';
3333
root.dataset.iconInnerSelector = `#${icon.id}`;
3434
root.appendChild(icon);
3535
}
36+
if (tabIndex !== undefined) {
37+
root.tabIndex = tabIndex;
38+
}
3639
const component = new MDCIconToggle(root);
3740
return {root, component};
3841
}
@@ -74,17 +77,40 @@ test('set/get on', () => {
7477
assert.equal(root.getAttribute('aria-pressed'), 'false');
7578
});
7679

77-
test('set/get disabled', () => {
78-
const {root, component} = setupTest();
80+
test('set/get disabled to true', () => {
81+
const {root, component} = setupTest({tabIndex: 0});
82+
7983
component.disabled = true;
8084
assert.isOk(component.disabled);
8185
assert.equal(root.getAttribute('aria-disabled'), 'true');
8286
assert.isOk(root.classList.contains(MDCIconToggleFoundation.cssClasses.DISABLED));
87+
assert.equal(root.tabIndex, -1);
88+
});
89+
90+
test('set/get disabled to false', () => {
91+
const {root, component} = setupTest({tabIndex: 0});
8392

8493
component.disabled = false;
8594
assert.isNotOk(component.disabled);
8695
assert.isNotOk(root.hasAttribute('aria-disabled'));
8796
assert.isNotOk(root.classList.contains(MDCIconToggleFoundation.cssClasses.DISABLED));
97+
assert.equal(root.tabIndex, 0, 'element\'s tabIndex should be the same value it already had');
98+
});
99+
100+
test('set/get disabled to true, then false', () => {
101+
const {root, component} = setupTest({tabIndex: 0});
102+
103+
component.disabled = true;
104+
assert.isOk(component.disabled);
105+
assert.equal(root.getAttribute('aria-disabled'), 'true');
106+
assert.isOk(root.classList.contains(MDCIconToggleFoundation.cssClasses.DISABLED));
107+
assert.equal(root.tabIndex, -1);
108+
109+
component.disabled = false;
110+
assert.isNotOk(component.disabled);
111+
assert.isNotOk(root.hasAttribute('aria-disabled'));
112+
assert.isNotOk(root.classList.contains(MDCIconToggleFoundation.cssClasses.DISABLED));
113+
assert.equal(root.tabIndex, 0, 'element\'s tabIndex should be the same value it originally had');
88114
});
89115

90116
test('#refreshToggleData proxies to foundation.refreshToggleData()', () => {
@@ -174,14 +200,12 @@ test('#adapter.setText sets the text content of the inner icon element when used
174200
});
175201

176202
test('#adapter.getTabIndex returns the tabIndex of the element', () => {
177-
const {root, component} = setupTest();
178-
root.tabIndex = 4;
203+
const {component} = setupTest({tabIndex: 4});
179204
assert.equal(component.getDefaultFoundation().adapter_.getTabIndex(), 4);
180205
});
181206

182207
test('#adapter.setTabIndex sets the tabIndex of the element', () => {
183-
const {root, component} = setupTest();
184-
root.tabIndex = 4;
208+
const {root, component} = setupTest({tabIndex: 4});
185209
component.getDefaultFoundation().adapter_.setTabIndex(2);
186210
assert.equal(root.tabIndex, 2);
187211
});

0 commit comments

Comments
 (0)