Skip to content
This repository was archived by the owner on Sep 5, 2024. It is now read-only.

Commit 3c0fed9

Browse files
author
Marcy Sutton
committed
fix(checkbox): disable checkboxes with tabindex=-1
Closes #2087
1 parent 4216d24 commit 3c0fed9

2 files changed

Lines changed: 41 additions & 3 deletions

File tree

src/components/checkbox/checkbox.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ function MdCheckboxDirective(inputDirective, $mdInkRipple, $mdAria, $mdConstant,
7272
function compile (tElement, tAttrs) {
7373

7474
tAttrs.type = 'checkbox';
75-
tAttrs.tabIndex = 0;
75+
tAttrs.tabindex = tAttrs.tabindex || '0';
7676
tElement.attr('role', tAttrs.type);
7777

7878
return function postLink(scope, element, attr, ngModelCtrl) {
@@ -85,7 +85,10 @@ function MdCheckboxDirective(inputDirective, $mdInkRipple, $mdAria, $mdConstant,
8585
ngModelCtrl.$setViewValue.bind(ngModelCtrl)
8686
);
8787
}
88-
88+
$$watchExpr('ngDisabled', 'tabindex', {
89+
true: '-1',
90+
false: attr.tabindex
91+
});
8992
$mdAria.expectWithText(element, 'aria-label');
9093

9194
// Reuse the original input[type=checkbox] directive from Angular core.
@@ -113,6 +116,16 @@ function MdCheckboxDirective(inputDirective, $mdInkRipple, $mdAria, $mdConstant,
113116

114117
ngModelCtrl.$render = render;
115118

119+
function $$watchExpr(expr, htmlAttr, valueOpts) {
120+
if (attr[expr]) {
121+
scope.$watch(attr[expr], function(val) {
122+
if (valueOpts[val]) {
123+
element.attr(htmlAttr, valueOpts[val]);
124+
}
125+
});
126+
}
127+
}
128+
116129
function keypressHandler(ev) {
117130
var keyCode = ev.which || ev.keyCode;
118131
if (keyCode === $mdConstant.KEY_CODE.SPACE || keyCode === $mdConstant.KEY_CODE.ENTER) {

src/components/checkbox/checkbox.spec.js

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ describe('mdCheckbox', function() {
4949
expect(cbElements.eq(0).attr('role')).toEqual('checkbox');
5050
}));
5151

52-
it('should be disabled with disabled attr', inject(function($compile, $rootScope) {
52+
it('should be disabled with ngDisabled attr', inject(function($compile, $rootScope) {
5353
var element = $compile('<div>' +
5454
'<md-checkbox ng-disabled="isDisabled" ng-model="blue">' +
5555
'</md-checkbox>' +
@@ -69,6 +69,31 @@ describe('mdCheckbox', function() {
6969
expect($rootScope.blue).toBe(true);
7070
}));
7171

72+
it('should preserve existing tabindex', inject(function($compile, $rootScope) {
73+
var element = $compile('<div>' +
74+
'<md-checkbox ng-model="blue" tabindex="2">' +
75+
'</md-checkbox>' +
76+
'</div>')($rootScope);
77+
78+
var checkbox = element.find('md-checkbox');
79+
expect(checkbox.attr('tabindex')).toBe('2');
80+
}));
81+
82+
it('should disable with tabindex=-1', inject(function($compile, $rootScope) {
83+
var element = $compile('<div>' +
84+
'<md-checkbox ng-disabled="isDisabled" ng-model="blue">' +
85+
'</md-checkbox>' +
86+
'</div>')($rootScope);
87+
88+
var checkbox = element.find('md-checkbox');
89+
90+
$rootScope.$apply('isDisabled = true');
91+
expect(checkbox.attr('tabindex')).toBe('-1');
92+
93+
$rootScope.$apply('isDisabled = false');
94+
expect(checkbox.attr('tabindex')).toBe('0');
95+
}));
96+
7297
it('should not set focus state on mousedown', inject(function($compile, $rootScope) {
7398
var checkbox = $compile('<md-checkbox ng-model="blue">')($rootScope.$new());
7499
$rootScope.$apply();

0 commit comments

Comments
 (0)