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

Commit 6d705b9

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 2a884c6 commit 6d705b9

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(matchLowercase ? term.toLowerCase() : 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];
@@ -750,6 +755,37 @@ describe('<md-autocomplete>', function() {
750755
}));
751756
});
752757

758+
describe('Async matching', function() {
759+
760+
it('should probably stop the loading indicator when clearing', inject(function($timeout, $material) {
761+
var scope = createScope();
762+
var template =
763+
'<md-autocomplete ' +
764+
' md-search-text="searchText"' +
765+
' md-items="item in asyncMatch(searchText)" ' +
766+
' md-item-text="item.display" ' +
767+
' placeholder="placeholder">' +
768+
' <span md-highlight-text="searchText">{{item.display}}</span>' +
769+
'</md-autocomplete>';
770+
var element = compile(template, scope);
771+
var input = element.find('input');
772+
var ctrl = element.controller('mdAutocomplete');
773+
774+
$material.flushInterimElement();
775+
776+
scope.$apply('searchText = "test"');
777+
778+
ctrl.clear();
779+
780+
expect(ctrl.loading).toBe(true);
781+
782+
$timeout.flush();
783+
784+
expect(ctrl.loading).toBe(false);
785+
}));
786+
787+
});
788+
753789
describe('API access', function() {
754790
it('should clear the selected item', inject(function($timeout) {
755791
var scope = createScope();

src/components/autocomplete/js/autocompleteController.js

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

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

626-
// Tell the select to fire and select nothing
627-
select(-1);
628-
629628
// Per http://www.w3schools.com/jsref/event_oninput.asp
630629
var eventObj = document.createEvent('CustomEvent');
631630
eventObj.initCustomEvent('input', true, true, { value: $scope.searchText });
@@ -667,9 +666,16 @@ function MdAutocompleteCtrl ($scope, $element, $mdUtil, $mdConstant, $mdTheming,
667666
function handleResults (matches) {
668667
cache[ term ] = matches;
669668
if ((searchText || '') !== ($scope.searchText || '')) return; //-- just cache the results if old request
669+
670670
ctrl.matches = matches;
671671
ctrl.hidden = shouldHide();
672+
673+
// If loading is in progress, then we'll end the progress. This is needed for example,
674+
// when the `clear` button was clicked, because there we always show the loading process, to prevent flashing.
675+
if (ctrl.loading) setLoading(false);
676+
672677
if ($scope.selectOnMatch) selectItemOnMatch();
678+
673679
updateMessages();
674680
positionDropdown();
675681
}

0 commit comments

Comments
 (0)