-
Notifications
You must be signed in to change notification settings - Fork 6.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(slide-toggle): support native tabindex attribute (#6613)
Currently the slide-toggle only allows changing the tabIndex using the `tabIndex` binding. Using the native `tabindex` attribute doesn't have any affect. With this change the native tabindex property will be respected. References #6465
- Loading branch information
1 parent
fe37cb2
commit 8f9f3c8
Showing
5 changed files
with
115 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import {mixinTabIndex} from './tabindex'; | ||
|
||
describe('mixinTabIndex', () => { | ||
|
||
it('should augment an existing class with a tabIndex property', () => { | ||
const classWithMixin = mixinTabIndex(TestClass); | ||
const instance = new classWithMixin(); | ||
|
||
expect(instance.tabIndex) | ||
.toBe(0, 'Expected the mixed-into class to have a tabIndex property'); | ||
|
||
instance.tabIndex = 4; | ||
|
||
expect(instance.tabIndex) | ||
.toBe(4, 'Expected the mixed-into class to have an updated tabIndex property'); | ||
}); | ||
|
||
it('should set tabIndex to `-1` if the disabled property is set to true', () => { | ||
const classWithMixin = mixinTabIndex(TestClass); | ||
const instance = new classWithMixin(); | ||
|
||
expect(instance.tabIndex) | ||
.toBe(0, 'Expected tabIndex to be set to 0 initially'); | ||
|
||
instance.disabled = true; | ||
|
||
expect(instance.tabIndex) | ||
.toBe(-1, 'Expected tabIndex to be set to -1 if the disabled property is set to true'); | ||
}); | ||
|
||
it('should allow having a custom default tabIndex value', () => { | ||
const classWithMixin = mixinTabIndex(TestClass, 20); | ||
const instance = new classWithMixin(); | ||
|
||
expect(instance.tabIndex) | ||
.toBe(20, 'Expected tabIndex to be set to 20 initially'); | ||
|
||
instance.tabIndex = 0; | ||
|
||
expect(instance.tabIndex) | ||
.toBe(0, 'Expected tabIndex to still support 0 as value'); | ||
}); | ||
|
||
}); | ||
|
||
class TestClass { | ||
disabled = false; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/** | ||
* @license | ||
* Copyright Google Inc. All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://angular.io/license | ||
*/ | ||
|
||
import {Constructor} from './constructor'; | ||
import {CanDisable} from './disabled'; | ||
|
||
/** @docs-private */ | ||
export interface HasTabIndex { | ||
tabIndex: number; | ||
} | ||
|
||
/** Mixin to augment a directive with a `tabIndex` property. */ | ||
export function mixinTabIndex<T extends Constructor<CanDisable>>(base: T, defaultTabIndex = 0) | ||
: Constructor<HasTabIndex> & T { | ||
return class extends base { | ||
private _tabIndex: number = defaultTabIndex; | ||
|
||
get tabIndex(): number { return this.disabled ? -1 : this._tabIndex; } | ||
set tabIndex(value: number) { | ||
// If the specified tabIndex value is null or undefined, fall back to the default value. | ||
this._tabIndex = value != null ? value : defaultTabIndex; | ||
} | ||
|
||
constructor(...args: any[]) { | ||
super(...args); | ||
} | ||
}; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters