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

Commit 36f63a2

Browse files
clshortfuseThomasBurleson
authored andcommitted
fix(autocomplete): improve clear and blur behavior on escape
* Prevent browser from using default ESCAPE key interaction for escape. * Set ctrl.hidden variable on escape Some browsers clear any input with type search on ESCAPE keypress. ctrl.hidden variable was not correctly reflecting dropdown hidden state Fixes #8917 Closes #8920
1 parent 64115c0 commit 36f63a2

File tree

2 files changed

+32
-13
lines changed

2 files changed

+32
-13
lines changed

src/components/autocomplete/autocomplete.spec.js

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -400,48 +400,67 @@ describe('<md-autocomplete>', function() {
400400
placeholder="placeholder">\
401401
<span md-highlight-text="searchText">{{item.display}}</span>\
402402
</md-autocomplete>';
403-
beforeEach( inject(function($timeout) {
403+
beforeEach( inject(function($timeout, $material) {
404404
scope = createScope();
405405
element = compile(template, scope);
406406
ctrl = element.controller('mdAutocomplete');
407407

408+
$material.flushInterimElement();
409+
410+
// Update the scope
411+
element.scope().searchText = 'fo';
412+
waitForVirtualRepeat(element);
413+
408414
// Focus the input
409415
ctrl.focus();
410416
$timeout.flush();
411-
expect(scope.searchText).toBe('');
412417

413-
scope.$apply('searchText = "test"');
418+
expect(ctrl.hidden).toBe(false);
414419

415-
expect(scope.searchText).toBe('test');
420+
expect(scope.searchText).toBe('fo');
416421

422+
waitForVirtualRepeat(element);
417423
$timeout.flush();
424+
expect(ctrl.hidden).toBe(false);
418425
}));
419426

420427
afterEach(function() { element.remove() });
421-
it('does not clear the value nor blur when hitting escape', inject(function($mdConstant, $document) {
428+
it('does not clear the value nor blur when hitting escape', inject(function($mdConstant, $document, $timeout) {
422429
scope.$apply('escapeOptions = "none"');
423430
scope.$apply(function() {
424431
ctrl.keydown(keydownEvent($mdConstant.KEY_CODE.ESCAPE));
432+
$timeout.flush();
433+
expect(ctrl.hidden).toBe(true);
434+
ctrl.keydown(keydownEvent($mdConstant.KEY_CODE.ESCAPE));
435+
$timeout.flush();
425436
});
426437

427-
expect(scope.searchText).toBe('test');
438+
expect(scope.searchText).toBe('fo');
428439
expect($document.activeElement).toBe(ctrl[0]);
429440
}));
430441

431-
it('does not clear the value but does blur when hitting escape', inject(function($mdConstant, $document) {
442+
it('does not clear the value but does blur when hitting escape', inject(function($mdConstant, $document, $timeout) {
432443
scope.$apply('escapeOptions = "blur"');
433444
scope.$apply(function() {
434445
ctrl.keydown(keydownEvent($mdConstant.KEY_CODE.ESCAPE));
446+
$timeout.flush();
447+
expect(ctrl.hidden).toBe(true);
448+
ctrl.keydown(keydownEvent($mdConstant.KEY_CODE.ESCAPE));
449+
$timeout.flush();
435450
});
436451

437-
expect(scope.searchText).toBe('test');
452+
expect(scope.searchText).toBe('fo');
438453
expect($document.activeElement).toBe(undefined);
439454
}));
440455

441-
it('clear the value but does not blur when hitting escape', inject(function($mdConstant, $document) {
456+
it('clear the value but does not blur when hitting escape', inject(function($mdConstant, $document, $timeout) {
442457
scope.$apply('escapeOptions = "clear"');
443458
scope.$apply(function() {
444459
ctrl.keydown(keydownEvent($mdConstant.KEY_CODE.ESCAPE));
460+
$timeout.flush();
461+
expect(ctrl.hidden).toBe(true);
462+
ctrl.keydown(keydownEvent($mdConstant.KEY_CODE.ESCAPE));
463+
$timeout.flush();
445464
});
446465

447466
expect(scope.searchText).toBe('');

src/components/autocomplete/js/autocompleteController.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -482,21 +482,21 @@ function MdAutocompleteCtrl ($scope, $element, $mdUtil, $mdConstant, $mdTheming,
482482
select(ctrl.index);
483483
break;
484484
case $mdConstant.KEY_CODE.ESCAPE:
485+
event.preventDefault(); // Prevent browser from always clearing input
485486
if (!shouldProcessEscape()) return;
486487
event.stopPropagation();
487-
event.preventDefault();
488488

489489
clearSelectedItem();
490490
if ($scope.searchText && hasEscapeOption('clear')) {
491491
clearSearchText();
492492
}
493493

494+
// Manually hide (needed for mdNotFound support)
495+
ctrl.hidden = true;
496+
494497
if (hasEscapeOption('blur')) {
495498
// Force the component to blur if they hit escape
496499
doBlur(true);
497-
} else {
498-
// Manually hide (needed for mdNotFound support)
499-
ctrl.hidden = true;
500500
}
501501

502502
break;

0 commit comments

Comments
 (0)