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

Commit d859f42

Browse files
chenyuzhcywesleycho
authored andcommitted
feat(typeahead): add min-length === 0 support
- Adds support for `typeahead-min-length` being equal to `0` Closes #764 Closes #2324 Closes #4789
1 parent 20a8701 commit d859f42

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

Diff for: src/typeahead/docs/readme.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ The typeahead directives provide several attributes:
4545

4646
* `typeahead-min-length` <i class="glyphicon glyphicon-eye-open"></i>
4747
_(Defaults: 1)_ :
48-
Minimal no of characters that needs to be entered before typeahead kicks-in. Must be greater than or equal to 1.
48+
Minimal no of characters that needs to be entered before typeahead kicks-in. Must be greater than or equal to 0.
4949

5050
* `typeahead-no-results` <i class="glyphicon glyphicon-eye-open"></i>
5151
_(Defaults: angular.noop)_ :

Diff for: src/typeahead/test/typeahead.spec.js

+8
Original file line numberDiff line numberDiff line change
@@ -1204,6 +1204,14 @@ describe('typeahead tests', function() {
12041204
changeInputValueTo(element, '');
12051205
expect(element).toBeOpenWithActive(3, 0);
12061206
});
1207+
1208+
it('should open typeahead when input is focused and value is empty if defined threshold is 0', function () {
1209+
var element = prepareInputEl('<div><input ng-model="result" uib-typeahead="item for item in source | filter:$viewValue" typeahead-min-length="0"></div>');
1210+
var inputEl = findInput(element);
1211+
inputEl.focus();
1212+
$scope.$digest();
1213+
expect(element).toBeOpenWithActive(3, 0);
1214+
});
12071215
});
12081216

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

Diff for: src/typeahead/typeahead.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,13 @@ angular.module('ui.bootstrap.typeahead', ['ui.bootstrap.position'])
385385
}
386386
});
387387

388+
element.bind('focus', function () {
389+
hasFocus = true;
390+
if (minLength === 0 && !modelCtrl.$viewValue) {
391+
getMatchesAsync(modelCtrl.$viewValue);
392+
}
393+
});
394+
388395
element.bind('blur', function() {
389396
if (isSelectOnBlur && scope.matches.length && scope.activeIdx !== -1 && !selected) {
390397
selected = true;
@@ -393,7 +400,8 @@ angular.module('ui.bootstrap.typeahead', ['ui.bootstrap.position'])
393400
});
394401
}
395402
if (!isEditable && modelCtrl.$error.editable) {
396-
element.val('');
403+
modelCtrl.$viewValue = '';
404+
element.val('');
397405
}
398406
hasFocus = false;
399407
selected = false;

0 commit comments

Comments
 (0)