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

fix(checkbox): properly show focus effect #9827

Merged
merged 1 commit into from
Oct 21, 2016
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
11 changes: 2 additions & 9 deletions src/components/checkbox/checkbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ angular
* </hljs>
*
*/
function MdCheckboxDirective(inputDirective, $mdAria, $mdConstant, $mdTheming, $mdUtil, $timeout) {
function MdCheckboxDirective(inputDirective, $mdAria, $mdConstant, $mdTheming, $mdUtil, $mdInteraction) {
inputDirective = inputDirective[0];

return {
Expand Down Expand Up @@ -130,17 +130,10 @@ function MdCheckboxDirective(inputDirective, $mdAria, $mdConstant, $mdTheming, $
0: {}
}, attr, [ngModelCtrl]);

scope.mouseActive = false;
element.on('click', listener)
.on('keypress', keypressHandler)
.on('mousedown', function() {
scope.mouseActive = true;
$timeout(function() {
scope.mouseActive = false;
}, 100);
})
.on('focus', function() {
if (scope.mouseActive === false) {
if ($mdInteraction.getLastInteractionType() === 'keyboard') {
element.addClass('md-focused');
}
})
Expand Down
20 changes: 19 additions & 1 deletion src/components/checkbox/checkbox.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,16 +127,34 @@ describe('mdCheckbox', function() {
expect(checkbox[0]).not.toHaveClass('md-focused');
});

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

// Fake a keyboard interaction for the $mdInteraction service.
body.triggerHandler('keydown');
checkbox.triggerHandler('focus');

expect(checkbox[0]).toHaveClass('md-focused');

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

it('should not apply focus effect with mouse interaction', function() {
var checkbox = compileAndLink('<md-checkbox ng-model="blue"></md-checkbox>');
var body = angular.element(document.body);

// Fake a mouse interaction for the $mdInteraction service.
body.triggerHandler('mouse');
checkbox.triggerHandler('focus');

expect(checkbox[0]).not.toHaveClass('md-focused');

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

it('should redirect focus of container to the checkbox element', function() {
var checkbox = compileAndLink('<md-checkbox ng-model="blue"></md-checkbox>');

Expand Down