Skip to content
This repository was archived by the owner on May 29, 2019. It is now read-only.

feat(typeahead): add noResults indicator binding #2792

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions src/typeahead/test/typeahead.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,40 @@ describe('typeahead tests', function () {
expect($scope.result).toEqual('AL');
expect(inputEl.val()).toEqual('AL');
});

it('should bind no results indicator as true when no matches returned', inject(function ($timeout) {

$scope.isNoResults = false;
$scope.loadMatches = function (viewValue) {
return $timeout(function () {
return [];
}, 1000);
};

var element = prepareInputEl('<div><input ng-model="result" typeahead="item for item in loadMatches()" typeahead-no-results="isNoResults"></div>');
changeInputValueTo(element, 'foo');

expect($scope.isNoResults).toBeFalsy();
$timeout.flush();
expect($scope.isNoResults).toBeTruthy();
}));

it('should bind no results indicator as false when matches are returned', inject(function ($timeout) {

$scope.isNoResults = false;
$scope.loadMatches = function (viewValue) {
return $timeout(function () {
return [viewValue];
}, 1000);
};

var element = prepareInputEl('<div><input ng-model="result" typeahead="item for item in loadMatches()" typeahead-no-results="isNoResults"></div>');
changeInputValueTo(element, 'foo');

expect($scope.isNoResults).toBeFalsy();
$timeout.flush();
expect($scope.isNoResults).toBeFalsy();
}));
});

describe('pop-up interaction', function () {
Expand Down
8 changes: 7 additions & 1 deletion src/typeahead/typeahead.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ angular.module('ui.bootstrap.typeahead', ['ui.bootstrap.position', 'ui.bootstrap
//a callback executed when a match is selected
var onSelectCallback = $parse(attrs.typeaheadOnSelect);

//binding to a variable that indicates if there were no results after the query is completed
var isNoResultsSetter = $parse(attrs.typeaheadNoResults).assign || angular.noop;

var inputFormatter = attrs.typeaheadInputFormatter ? $parse(attrs.typeaheadInputFormatter) : undefined;

var appendToBody = attrs.typeaheadAppendToBody ? originalScope.$eval(attrs.typeaheadAppendToBody) : false;
Expand Down Expand Up @@ -123,14 +126,15 @@ angular.module('ui.bootstrap.typeahead', ['ui.bootstrap.position', 'ui.bootstrap

var locals = {$viewValue: inputValue};
isLoadingSetter(originalScope, true);
isNoResultsSetter(originalScope, false);
$q.when(parserResult.source(originalScope, locals)).then(function(matches) {

//it might happen that several async queries were in progress if a user were typing fast
//but we are interested only in responses that correspond to the current view value
var onCurrentRequest = (inputValue === modelCtrl.$viewValue);
if (onCurrentRequest && hasFocus) {
if (matches.length > 0) {

isNoResultsSetter(originalScope, false);
scope.activeIdx = 0;
scope.matches.length = 0;

Expand All @@ -154,6 +158,7 @@ angular.module('ui.bootstrap.typeahead', ['ui.bootstrap.position', 'ui.bootstrap
element.attr('aria-expanded', true);
} else {
resetMatches();
isNoResultsSetter(originalScope, true);
}
}
if (onCurrentRequest) {
Expand All @@ -162,6 +167,7 @@ angular.module('ui.bootstrap.typeahead', ['ui.bootstrap.position', 'ui.bootstrap
}, function(){
resetMatches();
isLoadingSetter(originalScope, false);
isNoResultsSetter(originalScope, true);
});
};

Expand Down