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

Commit

Permalink
fix(autocomplete): allow clicking on not-found template.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
devversion authored and ThomasBurleson committed Feb 1, 2016
1 parent 3243575 commit 4ef9674
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 2 deletions.
69 changes: 69 additions & 0 deletions src/components/autocomplete/autocomplete.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,75 @@ describe('<md-autocomplete>', function() {
expect(ctrl2.hasNotFound).toBe(false);
}));

it('should even show the md-not-found template if we have lost focus', inject(function($timeout) {
var scope = createScope();
var template =
'<md-autocomplete' +
' md-selected-item="selectedItem"' +
' md-search-text="searchText"' +
' md-items="item in match(searchText)"' +
' md-item-text="item.display"' +
' placeholder="placeholder">' +
' <md-item-template>{{item.display}}</md-item-template>' +
' <md-not-found>Sorry, not found...</md-not-found>' +
'</md-autocomplete>';

var element = compile(template, scope);
var controller = element.controller('mdAutocomplete');

controller.focus();

scope.searchText = 'somethingthatdoesnotexist';

$timeout.flush();

controller.listEnter();
expect(controller.notFoundVisible()).toBe(true);

controller.blur();
expect(controller.notFoundVisible()).toBe(true);

controller.listLeave();
expect(controller.notFoundVisible()).toBe(false);

$timeout.flush();
element.remove();

}));

it('should not show the md-not-found template if we lost focus and left the list', inject(function($timeout) {
var scope = createScope();
var template =
'<md-autocomplete' +
' md-selected-item="selectedItem"' +
' md-search-text="searchText"' +
' md-items="item in match(searchText)"' +
' md-item-text="item.display"' +
' placeholder="placeholder">' +
' <md-item-template>{{item.display}}</md-item-template>' +
' <md-not-found>Sorry, not found...</md-not-found>' +
'</md-autocomplete>';

var element = compile(template, scope);
var controller = element.controller('mdAutocomplete');

controller.focus();

scope.searchText = 'somethingthatdoesnotexist';

$timeout.flush();

controller.listEnter();
expect(controller.notFoundVisible()).toBe(true);

controller.listLeave();
controller.blur();
expect(controller.notFoundVisible()).toBe(false);

$timeout.flush();
element.remove();
}));

});

describe('xss prevention', function() {
Expand Down
7 changes: 5 additions & 2 deletions src/components/autocomplete/js/autocompleteController.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ function MdAutocompleteCtrl ($scope, $element, $mdUtil, $mdConstant, $mdTheming,
noBlur = false,
selectedItemWatchers = [],
hasFocus = false,
lastCount = 0;
lastCount = 0,
promiseFetch = false;

//-- public variables with handlers
defineProperty('hidden', handleHiddenChange, true);
Expand Down Expand Up @@ -638,11 +639,13 @@ function MdAutocompleteCtrl ($scope, $element, $mdUtil, $mdConstant, $mdTheming,
handleResults(items);
} else if (items) {
setLoading(true);
promiseFetch = true;
$mdUtil.nextTick(function () {
if (items.success) items.success(handleResults);
if (items.then) items.then(handleResults);
if (items.finally) items.finally(function () {
setLoading(false);
promiseFetch = false;
});
},true, $scope);
}
Expand Down Expand Up @@ -707,7 +710,7 @@ function MdAutocompleteCtrl ($scope, $element, $mdUtil, $mdConstant, $mdTheming,
function notFoundVisible () {
var textLength = (ctrl.scope.searchText || '').length;

return ctrl.hasNotFound && !hasMatches() && !ctrl.loading && textLength >= getMinLength() && hasFocus && !hasSelection();
return ctrl.hasNotFound && !hasMatches() && (!ctrl.loading || promiseFetch) && textLength >= getMinLength() && (hasFocus || noBlur) && !hasSelection();
}

/**
Expand Down

0 comments on commit 4ef9674

Please sign in to comment.