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

Commit

Permalink
feat(typeahead): add min-length === 0 support
Browse files Browse the repository at this point in the history
- Adds support for `typeahead-min-length` being equal to `0`

Closes #764
Closes #2324
Closes #4789
  • Loading branch information
chenyuzhcy authored and wesleycho committed Oct 30, 2015
1 parent 20a8701 commit d859f42
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/typeahead/docs/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ The typeahead directives provide several attributes:

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

* `typeahead-no-results` <i class="glyphicon glyphicon-eye-open"></i>
_(Defaults: angular.noop)_ :
Expand Down
8 changes: 8 additions & 0 deletions src/typeahead/test/typeahead.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1204,6 +1204,14 @@ describe('typeahead tests', function() {
changeInputValueTo(element, '');
expect(element).toBeOpenWithActive(3, 0);
});

it('should open typeahead when input is focused and value is empty if defined threshold is 0', function () {
var element = prepareInputEl('<div><input ng-model="result" uib-typeahead="item for item in source | filter:$viewValue" typeahead-min-length="0"></div>');
var inputEl = findInput(element);
inputEl.focus();
$scope.$digest();
expect(element).toBeOpenWithActive(3, 0);
});
});

describe('event listeners', function() {
Expand Down
10 changes: 9 additions & 1 deletion src/typeahead/typeahead.js
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,13 @@ angular.module('ui.bootstrap.typeahead', ['ui.bootstrap.position'])
}
});

element.bind('focus', function () {
hasFocus = true;
if (minLength === 0 && !modelCtrl.$viewValue) {
getMatchesAsync(modelCtrl.$viewValue);
}
});

element.bind('blur', function() {
if (isSelectOnBlur && scope.matches.length && scope.activeIdx !== -1 && !selected) {
selected = true;
Expand All @@ -393,7 +400,8 @@ angular.module('ui.bootstrap.typeahead', ['ui.bootstrap.position'])
});
}
if (!isEditable && modelCtrl.$error.editable) {
element.val('');
modelCtrl.$viewValue = '';
element.val('');
}
hasFocus = false;
selected = false;
Expand Down

1 comment on commit d859f42

@dani3l
Copy link

@dani3l dani3l commented on d859f42 Nov 4, 2015

Choose a reason for hiding this comment

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

finally! thank you chen! :)

Please sign in to comment.