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

Commit 52a519e

Browse files
devversionThomasBurleson
authored andcommitted
fix(autocomplete): allow clicking on not-found template.
Currently the Not Found Template will be removed for a few seconds from the DOM, that's why clicks not getting registered. This can be fixed by checking for the mouse position. The Mouse should be on the list. References #5308. Fixes #6191. Fixes #5526. Closes #6103
1 parent 1f99795 commit 52a519e

File tree

2 files changed

+74
-2
lines changed

2 files changed

+74
-2
lines changed

src/components/autocomplete/autocomplete.spec.js

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,75 @@ describe('<md-autocomplete>', function() {
477477
expect(ctrl2.hasNotFound).toBe(false);
478478
}));
479479

480+
it('should even show the md-not-found template if we have lost focus', inject(function($timeout) {
481+
var scope = createScope();
482+
var template =
483+
'<md-autocomplete' +
484+
' md-selected-item="selectedItem"' +
485+
' md-search-text="searchText"' +
486+
' md-items="item in match(searchText)"' +
487+
' md-item-text="item.display"' +
488+
' placeholder="placeholder">' +
489+
' <md-item-template>{{item.display}}</md-item-template>' +
490+
' <md-not-found>Sorry, not found...</md-not-found>' +
491+
'</md-autocomplete>';
492+
493+
var element = compile(template, scope);
494+
var controller = element.controller('mdAutocomplete');
495+
496+
controller.focus();
497+
498+
scope.searchText = 'somethingthatdoesnotexist';
499+
500+
$timeout.flush();
501+
502+
controller.listEnter();
503+
expect(controller.notFoundVisible()).toBe(true);
504+
505+
controller.blur();
506+
expect(controller.notFoundVisible()).toBe(true);
507+
508+
controller.listLeave();
509+
expect(controller.notFoundVisible()).toBe(false);
510+
511+
$timeout.flush();
512+
element.remove();
513+
514+
}));
515+
516+
it('should not show the md-not-found template if we lost focus and left the list', inject(function($timeout) {
517+
var scope = createScope();
518+
var template =
519+
'<md-autocomplete' +
520+
' md-selected-item="selectedItem"' +
521+
' md-search-text="searchText"' +
522+
' md-items="item in match(searchText)"' +
523+
' md-item-text="item.display"' +
524+
' placeholder="placeholder">' +
525+
' <md-item-template>{{item.display}}</md-item-template>' +
526+
' <md-not-found>Sorry, not found...</md-not-found>' +
527+
'</md-autocomplete>';
528+
529+
var element = compile(template, scope);
530+
var controller = element.controller('mdAutocomplete');
531+
532+
controller.focus();
533+
534+
scope.searchText = 'somethingthatdoesnotexist';
535+
536+
$timeout.flush();
537+
538+
controller.listEnter();
539+
expect(controller.notFoundVisible()).toBe(true);
540+
541+
controller.listLeave();
542+
controller.blur();
543+
expect(controller.notFoundVisible()).toBe(false);
544+
545+
$timeout.flush();
546+
element.remove();
547+
}));
548+
480549
});
481550

482551
describe('xss prevention', function() {

src/components/autocomplete/js/autocompleteController.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ function MdAutocompleteCtrl ($scope, $element, $mdUtil, $mdConstant, $mdTheming,
1818
noBlur = false,
1919
selectedItemWatchers = [],
2020
hasFocus = false,
21-
lastCount = 0;
21+
lastCount = 0,
22+
promiseFetch = false;
2223

2324
//-- public variables with handlers
2425
defineProperty('hidden', handleHiddenChange, true);
@@ -638,11 +639,13 @@ function MdAutocompleteCtrl ($scope, $element, $mdUtil, $mdConstant, $mdTheming,
638639
handleResults(items);
639640
} else if (items) {
640641
setLoading(true);
642+
promiseFetch = true;
641643
$mdUtil.nextTick(function () {
642644
if (items.success) items.success(handleResults);
643645
if (items.then) items.then(handleResults);
644646
if (items.finally) items.finally(function () {
645647
setLoading(false);
648+
promiseFetch = false;
646649
});
647650
},true, $scope);
648651
}
@@ -707,7 +710,7 @@ function MdAutocompleteCtrl ($scope, $element, $mdUtil, $mdConstant, $mdTheming,
707710
function notFoundVisible () {
708711
var textLength = (ctrl.scope.searchText || '').length;
709712

710-
return ctrl.hasNotFound && !hasMatches() && !ctrl.loading && textLength >= getMinLength() && hasFocus && !hasSelection();
713+
return ctrl.hasNotFound && !hasMatches() && (!ctrl.loading || promiseFetch) && textLength >= getMinLength() && (hasFocus || noBlur) && !hasSelection();
711714
}
712715

713716
/**

0 commit comments

Comments
 (0)