diff --git a/src/components/checkbox/checkbox.js b/src/components/checkbox/checkbox.js index 34949ef648..028035c83c 100644 --- a/src/components/checkbox/checkbox.js +++ b/src/components/checkbox/checkbox.js @@ -54,7 +54,7 @@ angular * * */ -function MdCheckboxDirective(inputDirective, $mdAria, $mdConstant, $mdTheming, $mdUtil, $timeout) { +function MdCheckboxDirective(inputDirective, $mdAria, $mdConstant, $mdTheming, $mdUtil, $mdInteraction) { inputDirective = inputDirective[0]; return { @@ -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'); } }) diff --git a/src/components/checkbox/checkbox.spec.js b/src/components/checkbox/checkbox.spec.js index 656e49961c..4e4be29f87 100644 --- a/src/components/checkbox/checkbox.spec.js +++ b/src/components/checkbox/checkbox.spec.js @@ -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(''); + 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(''); + 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('');