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

Commit 002207c

Browse files
devversionjelbourn
authored andcommitted
fix(checkbox): properly show focus effect (#9827)
* Right now (as same as with the button) the focus will be automatically restored when switching the browser tabs (this is bad UX). * Using the new $mdInteraction service could remove the random $timeout and just fix this issue easily.
1 parent 3034237 commit 002207c

File tree

2 files changed

+21
-10
lines changed

2 files changed

+21
-10
lines changed

src/components/checkbox/checkbox.js

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ angular
5454
* </hljs>
5555
*
5656
*/
57-
function MdCheckboxDirective(inputDirective, $mdAria, $mdConstant, $mdTheming, $mdUtil, $timeout) {
57+
function MdCheckboxDirective(inputDirective, $mdAria, $mdConstant, $mdTheming, $mdUtil, $mdInteraction) {
5858
inputDirective = inputDirective[0];
5959

6060
return {
@@ -130,17 +130,10 @@ function MdCheckboxDirective(inputDirective, $mdAria, $mdConstant, $mdTheming, $
130130
0: {}
131131
}, attr, [ngModelCtrl]);
132132

133-
scope.mouseActive = false;
134133
element.on('click', listener)
135134
.on('keypress', keypressHandler)
136-
.on('mousedown', function() {
137-
scope.mouseActive = true;
138-
$timeout(function() {
139-
scope.mouseActive = false;
140-
}, 100);
141-
})
142135
.on('focus', function() {
143-
if (scope.mouseActive === false) {
136+
if ($mdInteraction.getLastInteractionType() === 'keyboard') {
144137
element.addClass('md-focused');
145138
}
146139
})

src/components/checkbox/checkbox.spec.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,16 +127,34 @@ describe('mdCheckbox', function() {
127127
expect(checkbox[0]).not.toHaveClass('md-focused');
128128
});
129129

130-
it('should set focus state on focus and remove on blur', function() {
130+
it('should apply focus effect with keyboard interaction', function() {
131131
var checkbox = compileAndLink('<md-checkbox ng-model="blue"></md-checkbox>');
132+
var body = angular.element(document.body);
132133

134+
// Fake a keyboard interaction for the $mdInteraction service.
135+
body.triggerHandler('keydown');
133136
checkbox.triggerHandler('focus');
137+
134138
expect(checkbox[0]).toHaveClass('md-focused');
135139

136140
checkbox.triggerHandler('blur');
137141
expect(checkbox[0]).not.toHaveClass('md-focused');
138142
});
139143

144+
it('should not apply focus effect with mouse interaction', function() {
145+
var checkbox = compileAndLink('<md-checkbox ng-model="blue"></md-checkbox>');
146+
var body = angular.element(document.body);
147+
148+
// Fake a mouse interaction for the $mdInteraction service.
149+
body.triggerHandler('mouse');
150+
checkbox.triggerHandler('focus');
151+
152+
expect(checkbox[0]).not.toHaveClass('md-focused');
153+
154+
checkbox.triggerHandler('blur');
155+
expect(checkbox[0]).not.toHaveClass('md-focused');
156+
});
157+
140158
it('should redirect focus of container to the checkbox element', function() {
141159
var checkbox = compileAndLink('<md-checkbox ng-model="blue"></md-checkbox>');
142160

0 commit comments

Comments
 (0)