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

Commit 99e8e2c

Browse files
author
Robert Messerle
committed
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
1 parent 1b984ed commit 99e8e2c

File tree

2 files changed

+40
-21
lines changed

2 files changed

+40
-21
lines changed

src/components/autocomplete/autocomplete.spec.js

Lines changed: 39 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,14 @@ describe('<md-autocomplete>', function () {
2828
return scope;
2929
}
3030

31+
function keydownEvent (keyCode) {
32+
return {
33+
keyCode: keyCode,
34+
stopPropagation: angular.noop,
35+
preventDefault: angular.noop
36+
};
37+
}
38+
3139
describe('basic functionality', function () {
3240
it('should update selected item and search text', inject(function ($timeout, $mdConstant) {
3341
var scope = createScope();
@@ -54,16 +62,8 @@ describe('<md-autocomplete>', function () {
5462
expect(scope.match(scope.searchText).length).toBe(1);
5563
expect(ul.find('li').length).toBe(1);
5664

57-
ctrl.keydown({
58-
keyCode: $mdConstant.KEY_CODE.DOWN_ARROW,
59-
preventDefault: angular.noop,
60-
stopPropagation: angular.noop
61-
});
62-
ctrl.keydown({
63-
keyCode: $mdConstant.KEY_CODE.ENTER,
64-
preventDefault: angular.noop,
65-
stopPropagation: angular.noop
66-
});
65+
ctrl.keydown(keydownEvent($mdConstant.KEY_CODE.DOWN_ARROW));
66+
ctrl.keydown(keydownEvent($mdConstant.KEY_CODE.ENTER));
6767
$timeout.flush();
6868

6969
expect(scope.searchText).toBe('foo');
@@ -88,7 +88,7 @@ describe('<md-autocomplete>', function () {
8888
expect(input.attr('id')).toBe(scope.inputId);
8989
}));
9090

91-
it('should allow you to set an input id without floating label', inject(function () {
91+
it('should allow you to set an input id with floating label', inject(function () {
9292
var scope = createScope(null, { inputId: 'custom-input-id' });
9393
var template = '\
9494
<md-autocomplete\
@@ -106,6 +106,32 @@ describe('<md-autocomplete>', function () {
106106

107107
expect(input.attr('id')).toBe(scope.inputId);
108108
}));
109+
110+
it('should clear value when hitting escape', inject(function ($mdConstant, $timeout) {
111+
var scope = createScope();
112+
var template = '\
113+
<md-autocomplete\
114+
md-search-text="searchText"\
115+
md-items="item in match(searchText)"\
116+
md-item-text="item.display"\
117+
placeholder="placeholder">\
118+
<span md-highlight-text="searchText">{{item.display}}</span>\
119+
</md-autocomplete>';
120+
var element = compile(template, scope);
121+
var input = element.find('input');
122+
var ctrl = element.controller('mdAutocomplete');
123+
124+
expect(scope.searchText).toBe('');
125+
126+
scope.$apply('searchText = "test"');
127+
128+
expect(scope.searchText).toBe('test');
129+
130+
$timeout.flush();
131+
scope.$apply(function () { ctrl.keydown(keydownEvent($mdConstant.KEY_CODE.ESCAPE)); });
132+
133+
expect(scope.searchText).toBe('');
134+
}));
109135
});
110136

111137
describe('basic functionality with template', function () {
@@ -136,16 +162,8 @@ describe('<md-autocomplete>', function () {
136162
expect(scope.match(scope.searchText).length).toBe(1);
137163
expect(ul.find('li').length).toBe(1);
138164

139-
ctrl.keydown({
140-
keyCode: $mdConstant.KEY_CODE.DOWN_ARROW,
141-
preventDefault: angular.noop,
142-
stopPropagation: angular.noop
143-
});
144-
ctrl.keydown({
145-
keyCode: $mdConstant.KEY_CODE.ENTER,
146-
preventDefault: angular.noop,
147-
stopPropagation: angular.noop
148-
});
165+
ctrl.keydown(keydownEvent($mdConstant.KEY_CODE.DOWN_ARROW));
166+
ctrl.keydown(keydownEvent($mdConstant.KEY_CODE.ENTER));
149167
$timeout.flush();
150168

151169
expect(scope.searchText).toBe('foo');

src/components/autocomplete/js/autocompleteController.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,7 @@ function MdAutocompleteCtrl ($scope, $element, $mdUtil, $mdConstant, $mdTheming,
371371
case $mdConstant.KEY_CODE.ESCAPE:
372372
event.stopPropagation();
373373
event.preventDefault();
374+
clearValue();
374375
ctrl.matches = [];
375376
ctrl.hidden = true;
376377
ctrl.index = getDefaultIndex();

0 commit comments

Comments
 (0)