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

Commit fc2a8b7

Browse files
author
Luke Hutton
committed
feat(typeahead): respect min length when show results on focus set
1 parent 0257665 commit fc2a8b7

File tree

2 files changed

+9
-7
lines changed

2 files changed

+9
-7
lines changed

src/typeahead/test/typeahead.spec.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1522,25 +1522,26 @@ describe('typeahead tests', function() {
15221522
});
15231523

15241524
describe('showResultsOnFocus set to true', function() {
1525-
it('should open typeahead when input is focused and input is empty string', function() {
1526-
var element = prepareInputEl('<div><input ng-model="result" uib-typeahead="item for item in source | filter:$viewValue" typeahead-show-results-on-focus="true"></div>');
1525+
it('should open typeahead when input is focused and input is set and longer than min length threshold', function() {
1526+
var element = prepareInputEl('<div><input ng-model="result" uib-typeahead="item for item in source | filter:$viewValue" typeahead-min-length="1" typeahead-show-results-on-focus="true"></div>');
15271527
var inputEl = findInput(element);
1528-
changeInputValueTo(element, '');
1528+
changeInputValueTo(element, 'ba');
15291529
inputEl.focus();
15301530
$timeout.flush();
15311531
$scope.$digest();
15321532
expect(element).toBeOpenWithActive(3, 0);
15331533
});
15341534

1535-
it('should open typeahead when input is focused and input is not empty string', function() {
1536-
var element = prepareInputEl('<div><input ng-model="result" uib-typeahead="item for item in source | filter:$viewValue" typeahead-show-results-on-focus="true"></div>');
1535+
it('should not open typeahead when input is focused and input is set and not longer than min length threshold', function() {
1536+
var element = prepareInputEl('<div><input ng-model="result" uib-typeahead="item for item in source | filter:$viewValue" typeahead-min-length="2" typeahead-show-results-on-focus="true"></div>');
15371537
var inputEl = findInput(element);
15381538
changeInputValueTo(element, 'b');
15391539
inputEl.focus();
15401540
$timeout.flush();
15411541
$scope.$digest();
1542-
expect(element).toBeOpenWithActive(2, 0);
1542+
expect(element).toBeClosed();
15431543
});
1544+
15441545
});
15451546

15461547
describe('event listeners', function() {

src/typeahead/typeahead.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,8 @@ angular.module('ui.bootstrap.typeahead', ['ui.bootstrap.debounce', 'ui.bootstrap
435435

436436
element.on('focus', function (evt) {
437437
hasFocus = true;
438-
if (minLength === 0 && !modelCtrl.$viewValue || showResultsOnFocus) {
438+
if (minLength === 0 && !modelCtrl.$viewValue ||
439+
showResultsOnFocus && modelCtrl.$viewValue && modelCtrl.$viewValue.length >= minLength) {
439440
$timeout(function() {
440441
getMatchesAsync(modelCtrl.$viewValue, evt);
441442
}, 0);

0 commit comments

Comments
 (0)