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

feat(typeahead): handles min-length of 0 #1624

Closed
wants to merge 2 commits 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
9 changes: 9 additions & 0 deletions src/typeahead/test/typeahead.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -594,4 +594,13 @@ describe('typeahead tests', function () {
});
});

describe('minLength set to 0', function () {
it('should open typeahead if input is changed to empty string if defined threshold is 0', function () {
var element = prepareInputEl('<div><input ng-model="result" typeahead="item for item in source | filter:$viewValue" typeahead-min-length="0"></div>');
changeInputValueTo(element, '');

expect(element).toBeOpenWithActive(3, 0);
});
});

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should it not also check if the input is focus'd?
Otherwise any typeahead directive would stay open?

});
7 changes: 4 additions & 3 deletions src/typeahead/typeahead.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ angular.module('ui.bootstrap.typeahead', ['ui.bootstrap.position', 'ui.bootstrap
//SUPPORTED ATTRIBUTES (OPTIONS)

//minimal no of characters that needs to be entered before typeahead kicks-in
var minSearch = originalScope.$eval(attrs.typeaheadMinLength) || 1;
var minSearch = originalScope.$eval(attrs.typeaheadMinLength);
minSearch = minSearch === 0 ? minSearch : minSearch || 1;

//minimal wait time after last character typed before typehead kicks-in
var waitTime = originalScope.$eval(attrs.typeaheadWaitMs) || 0;
Expand Down Expand Up @@ -150,7 +151,7 @@ angular.module('ui.bootstrap.typeahead', ['ui.bootstrap.position', 'ui.bootstrap

hasFocus = true;

if (inputValue && inputValue.length >= minSearch) {
if (minSearch === 0 || inputValue && inputValue.length >= minSearch) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there check for invalid inputs here (line 44) - strings, numbers as strings, negative numbers ...?

Maybe new tests to take care of invalid inputs, to be on the safe side?

if (waitTime > 0) {
if (timeoutPromise) {
$timeout.cancel(timeoutPromise);//cancel previous timeout
Expand Down Expand Up @@ -221,7 +222,7 @@ angular.module('ui.bootstrap.typeahead', ['ui.bootstrap.position', 'ui.bootstrap

resetMatches();

//return focus to the input element if a mach was selected via a mouse click event
//return focus to the input element if a match was selected via a mouse click event
element[0].focus();
};

Expand Down