From 7763111e5a7ef21a1941974968f7dd94bd09a1e9 Mon Sep 17 00:00:00 2001 From: Paul Gschwendtner Date: Wed, 12 Oct 2016 19:47:34 +0200 Subject: [PATCH] fix(checkbox): properly show focus effect * 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. --- src/components/checkbox/checkbox.js | 11 ++--------- src/components/checkbox/checkbox.spec.js | 20 +++++++++++++++++++- 2 files changed, 21 insertions(+), 10 deletions(-) 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('');