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

Commit b05b1f7

Browse files
devversionThomasBurleson
authored andcommitted
fix(autocomplete): probably clear the autocomplete.
At the moment we calling the `select(-1)` function, but that will cause problems, because it will stuck. There is no need of calling the `select(-1)` function, we should just clear the loading indicator on an synchronous query. On an async query, the loading indicator will be set by default to false on completion. This fixes the issue, which required two clicks on a list item after cleared the autocomplete through the button. Fixes #7180 Closes #7354
1 parent 51c072e commit b05b1f7

File tree

2 files changed

+47
-5
lines changed

2 files changed

+47
-5
lines changed

src/components/autocomplete/autocomplete.spec.js

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,18 @@ describe('<md-autocomplete>', function() {
1616
items = items || ['foo', 'bar', 'baz'].map(function(item) {
1717
return {display: item};
1818
});
19-
inject(function($rootScope) {
19+
inject(function($rootScope, $timeout) {
2020
scope = $rootScope.$new();
2121
scope.match = function(term) {
2222
return items.filter(function(item) {
2323
return item.display.indexOf(term) === 0;
2424
});
2525
};
26+
scope.asyncMatch = function(term) {
27+
return $timeout(function() {
28+
return scope.match(term)
29+
}, 1000);
30+
};
2631
scope.searchText = '';
2732
scope.selectedItem = null;
2833
for (var key in obj) scope[key] = obj[key];
@@ -657,6 +662,37 @@ describe('<md-autocomplete>', function() {
657662
}));
658663
});
659664

665+
describe('Async matching', function() {
666+
667+
it('should probably stop the loading indicator when clearing', inject(function($timeout, $material) {
668+
var scope = createScope();
669+
var template =
670+
'<md-autocomplete ' +
671+
' md-search-text="searchText"' +
672+
' md-items="item in asyncMatch(searchText)" ' +
673+
' md-item-text="item.display" ' +
674+
' placeholder="placeholder">' +
675+
' <span md-highlight-text="searchText">{{item.display}}</span>' +
676+
'</md-autocomplete>';
677+
var element = compile(template, scope);
678+
var input = element.find('input');
679+
var ctrl = element.controller('mdAutocomplete');
680+
681+
$material.flushInterimElement();
682+
683+
scope.$apply('searchText = "test"');
684+
685+
ctrl.clear();
686+
687+
expect(ctrl.loading).toBe(true);
688+
689+
$timeout.flush();
690+
691+
expect(ctrl.loading).toBe(false);
692+
}));
693+
694+
});
695+
660696
describe('API access', function() {
661697
it('should clear the selected item', inject(function($timeout) {
662698
var scope = createScope();

src/components/autocomplete/js/autocompleteController.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -613,17 +613,16 @@ function MdAutocompleteCtrl ($scope, $element, $mdUtil, $mdConstant, $mdTheming,
613613
* Clears the searchText value and selected item.
614614
*/
615615
function clearValue () {
616-
// Set the loading to true so we don't see flashes of content
616+
// Set the loading to true so we don't see flashes of content.
617+
// The flashing will only occour when an async request is running.
618+
// So the loading process will stop when the results had been retrieved.
617619
setLoading(true);
618620

619621
// Reset our variables
620622
ctrl.index = 0;
621623
ctrl.matches = [];
622624
$scope.searchText = '';
623625

624-
// Tell the select to fire and select nothing
625-
select(-1);
626-
627626
// Per http://www.w3schools.com/jsref/event_oninput.asp
628627
var eventObj = document.createEvent('CustomEvent');
629628
eventObj.initCustomEvent('input', true, true, { value: $scope.searchText });
@@ -665,9 +664,16 @@ function MdAutocompleteCtrl ($scope, $element, $mdUtil, $mdConstant, $mdTheming,
665664
function handleResults (matches) {
666665
cache[ term ] = matches;
667666
if ((searchText || '') !== ($scope.searchText || '')) return; //-- just cache the results if old request
667+
668668
ctrl.matches = matches;
669669
ctrl.hidden = shouldHide();
670+
671+
// If loading is in progress, then we'll end the progress. This is needed for example,
672+
// when the `clear` button was clicked, because there we always show the loading process, to prevent flashing.
673+
if (ctrl.loading) setLoading(false);
674+
670675
if ($scope.selectOnMatch) selectItemOnMatch();
676+
671677
updateMessages();
672678
positionDropdown();
673679
}

0 commit comments

Comments
 (0)