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

Commit

Permalink
fix(autocomplete): autocomplete now selects the first item by default…
Browse files Browse the repository at this point in the history
… and no longer hides results when only one item is visible

Closes #1858
  • Loading branch information
Robert Messerle committed Mar 18, 2015
1 parent de423ae commit 4c2b086
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions src/components/autocomplete/js/autocompleteController.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,10 @@
}

function handleSearchText (searchText, previousSearchText) {
self.index = -1;
self.index = 0;
//-- clear selected item if search text no longer matches it
if (searchText !== getDisplayValue($scope.selectedItem)) $scope.selectedItem = null;
//-- cancel results if search text is not long enough
if (!searchText || searchText.length < Math.max(parseInt($scope.minLength, 10), 1)) {
self.loading = false;
self.matches = [];
Expand All @@ -85,15 +88,17 @@
return;
}
var term = searchText.toLowerCase();
//-- cancel promise if a promise is in progress
if (promise && promise.cancel) {
promise.cancel();
promise = null;
}
//-- if results are cached, pull in cached results
if (!$scope.noCache && cache[term]) {
self.matches = cache[term];
updateMessages();
} else {
self.fetch(searchText);
fetchResults(searchText);
}
self.hidden = shouldHide();
if ($scope.textChange && searchText !== previousSearchText)
Expand Down Expand Up @@ -165,7 +170,7 @@
case $mdConstant.KEY_CODE.ESCAPE:
self.matches = [];
self.hidden = true;
self.index = -1;
self.index = 0;
break;
case $mdConstant.KEY_CODE.TAB:
break;
Expand All @@ -180,7 +185,9 @@
}

function shouldHide () {
return self.matches.length === 1 && $scope.searchText === getDisplayValue(self.matches[0]);
return self.matches.length === 1
&& $scope.searchText === getDisplayValue(self.matches[0])
&& $scope.selectedItem === self.matches[0];
}

function getCurrentDisplayValue () {
Expand All @@ -195,7 +202,7 @@
$scope.selectedItem = self.matches[index];
$scope.searchText = getDisplayValue($scope.selectedItem) || $scope.searchText;
self.hidden = true;
self.index = -1;
self.index = 0;
self.matches = [];
}

Expand Down

0 comments on commit 4c2b086

Please sign in to comment.