From 99e8e2c20f58555f30dda045211309025494879f Mon Sep 17 00:00:00 2001 From: Robert Messerle Date: Thu, 23 Jul 2015 11:26:57 -0700 Subject: [PATCH] fix(autocomplete): hitting escape once again clears the search text This was caused when we added `stopPropagation` to the keydown event. We now have to manually clear the field. Closes #3847 --- .../autocomplete/autocomplete.spec.js | 60 ++++++++++++------- .../autocomplete/js/autocompleteController.js | 1 + 2 files changed, 40 insertions(+), 21 deletions(-) diff --git a/src/components/autocomplete/autocomplete.spec.js b/src/components/autocomplete/autocomplete.spec.js index 90d7dcc5837..d2a508916c8 100644 --- a/src/components/autocomplete/autocomplete.spec.js +++ b/src/components/autocomplete/autocomplete.spec.js @@ -28,6 +28,14 @@ describe('', function () { return scope; } + function keydownEvent (keyCode) { + return { + keyCode: keyCode, + stopPropagation: angular.noop, + preventDefault: angular.noop + }; + } + describe('basic functionality', function () { it('should update selected item and search text', inject(function ($timeout, $mdConstant) { var scope = createScope(); @@ -54,16 +62,8 @@ describe('', function () { expect(scope.match(scope.searchText).length).toBe(1); expect(ul.find('li').length).toBe(1); - ctrl.keydown({ - keyCode: $mdConstant.KEY_CODE.DOWN_ARROW, - preventDefault: angular.noop, - stopPropagation: angular.noop - }); - ctrl.keydown({ - keyCode: $mdConstant.KEY_CODE.ENTER, - preventDefault: angular.noop, - stopPropagation: angular.noop - }); + ctrl.keydown(keydownEvent($mdConstant.KEY_CODE.DOWN_ARROW)); + ctrl.keydown(keydownEvent($mdConstant.KEY_CODE.ENTER)); $timeout.flush(); expect(scope.searchText).toBe('foo'); @@ -88,7 +88,7 @@ describe('', function () { expect(input.attr('id')).toBe(scope.inputId); })); - it('should allow you to set an input id without floating label', inject(function () { + it('should allow you to set an input id with floating label', inject(function () { var scope = createScope(null, { inputId: 'custom-input-id' }); var template = '\ ', function () { expect(input.attr('id')).toBe(scope.inputId); })); + + it('should clear value when hitting escape', inject(function ($mdConstant, $timeout) { + var scope = createScope(); + var template = '\ + \ + {{item.display}}\ + '; + var element = compile(template, scope); + var input = element.find('input'); + var ctrl = element.controller('mdAutocomplete'); + + expect(scope.searchText).toBe(''); + + scope.$apply('searchText = "test"'); + + expect(scope.searchText).toBe('test'); + + $timeout.flush(); + scope.$apply(function () { ctrl.keydown(keydownEvent($mdConstant.KEY_CODE.ESCAPE)); }); + + expect(scope.searchText).toBe(''); + })); }); describe('basic functionality with template', function () { @@ -136,16 +162,8 @@ describe('', function () { expect(scope.match(scope.searchText).length).toBe(1); expect(ul.find('li').length).toBe(1); - ctrl.keydown({ - keyCode: $mdConstant.KEY_CODE.DOWN_ARROW, - preventDefault: angular.noop, - stopPropagation: angular.noop - }); - ctrl.keydown({ - keyCode: $mdConstant.KEY_CODE.ENTER, - preventDefault: angular.noop, - stopPropagation: angular.noop - }); + ctrl.keydown(keydownEvent($mdConstant.KEY_CODE.DOWN_ARROW)); + ctrl.keydown(keydownEvent($mdConstant.KEY_CODE.ENTER)); $timeout.flush(); expect(scope.searchText).toBe('foo'); diff --git a/src/components/autocomplete/js/autocompleteController.js b/src/components/autocomplete/js/autocompleteController.js index cb95d1dd6d5..a529de1f1de 100644 --- a/src/components/autocomplete/js/autocompleteController.js +++ b/src/components/autocomplete/js/autocompleteController.js @@ -371,6 +371,7 @@ function MdAutocompleteCtrl ($scope, $element, $mdUtil, $mdConstant, $mdTheming, case $mdConstant.KEY_CODE.ESCAPE: event.stopPropagation(); event.preventDefault(); + clearValue(); ctrl.matches = []; ctrl.hidden = true; ctrl.index = getDefaultIndex();