Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle tabindex attribute correctly #83

Merged
merged 2 commits into from
Mar 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion iron-control-state.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,12 @@ declare namespace Polymer {
* If true, the user cannot interact with this element.
*/
disabled: boolean|null|undefined;
_oldTabIndex: number|null|undefined;

/**
* Value of the `tabindex` attribute before `disabled` was activated.
* `null` means the attribute was not present.
*/
_oldTabIndex: string|null|undefined;
_boundFocusBlurHandler: Function|null|undefined;
ready(): void;
_focusBlurHandler(event: any): void;
Expand Down
19 changes: 16 additions & 3 deletions iron-control-state.html
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,13 @@
reflectToAttribute: true
},

/**
* Value of the `tabindex` attribute before `disabled` was activated.
* `null` means the attribute was not present.
* @type {?string|undefined}
*/
_oldTabIndex: {
type: Number
type: String
},

_boundFocusBlurHandler: {
Expand Down Expand Up @@ -103,12 +108,20 @@
this.setAttribute('aria-disabled', disabled ? 'true' : 'false');
this.style.pointerEvents = disabled ? 'none' : '';
if (disabled) {
this._oldTabIndex = this.tabIndex;
// Read the `tabindex` attribute instead of the `tabIndex` property.
// The property returns `-1` if there is no `tabindex` attribute.
// This distinction is important when restoring the value because
// leaving `-1` hides shadow root children from the tab order.
this._oldTabIndex = this.getAttribute('tabindex');
this._setFocused(false);
this.tabIndex = -1;
this.blur();
} else if (this._oldTabIndex !== undefined) {
this.tabIndex = this._oldTabIndex;
if (this._oldTabIndex === null) {
this.removeAttribute('tabindex');
} else {
this.setAttribute('tabindex', this._oldTabIndex);
}
}
},

Expand Down
48 changes: 48 additions & 0 deletions test/disabled-state.html
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,18 @@
</template>
</test-fixture>

<test-fixture id="InitiallyWithoutTabIndex">
<template>
<test-control></test-control>
</template>
</test-fixture>

<test-fixture id="InitiallyWithTabIndex">
<template>
<test-control tabindex="0"></test-control>
</template>
</test-fixture>

<script>
suite('disabled-state', function() {
var disableTarget;
Expand Down Expand Up @@ -75,6 +87,42 @@
expect(disableTarget.getAttribute('aria-disabled')).to.be.eql('true');
});
});

suite('`tabindex` attribute handling', function() {
suite('without `tabindex`', function() {
setup(function() {
disableTarget = fixture('InitiallyWithoutTabIndex');
});

test('adds `tabindex = -1` when disabled', function() {
disableTarget.disabled = true;
expect(disableTarget.getAttribute('tabindex')).to.be.eql('-1');
});

test('removed `tabindex` when re-enabled', function() {
disableTarget.disabled = true;
disableTarget.disabled = false;
expect(disableTarget.getAttribute('tabindex')).to.be.eql(null);
});
});

suite('with `tabindex`', function() {
setup(function() {
disableTarget = fixture('InitiallyWithTabIndex');
});

test('adds `tabindex = -1` when disabled', function() {
disableTarget.disabled = true;
expect(disableTarget.getAttribute('tabindex')).to.be.eql('-1');
});

test('restores `tabindex = 0` when re-enabled', function() {
disableTarget.disabled = true;
disableTarget.disabled = false;
expect(disableTarget.getAttribute('tabindex')).to.be.eql('0');
});
});
});
});
</script>

Expand Down