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

Commit 68cac59

Browse files
DSI-Entrepriseswesleycho
authored andcommitted
feat(typeahead): add 'select on blur' option.
- Add `select-on-blur` option Closes #3445
1 parent 054341b commit 68cac59

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

src/typeahead/docs/readme.md

+4
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,7 @@ The typeahead directives provide several attributes:
5555
* `typeahead-focus-first`
5656
_(Defaults: true)_ :
5757
Should the first match automatically be focused as you type?
58+
59+
* `select-on`blur`
60+
_(Defaults: false)_ :
61+
On blur, select the currently highlighted match

src/typeahead/test/typeahead.spec.js

+26
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,32 @@ describe('typeahead tests', function () {
384384
expect(element).toBeClosed();
385385
});
386386

387+
it('should not select any match on blur without \'select-on-blur=true\' option', function () {
388+
389+
var element = prepareInputEl('<div><input ng-model="result" typeahead="item for item in source | filter:$viewValue"></div>');
390+
var inputEl = findInput(element);
391+
392+
changeInputValueTo(element, 'b');
393+
inputEl.blur(); // input loses focus
394+
395+
// no change
396+
expect($scope.result).toEqual('b');
397+
expect(inputEl.val()).toEqual('b');
398+
});
399+
400+
it('should select a match on blur with \'select-on-blur=true\' option', function () {
401+
402+
var element = prepareInputEl('<div><input ng-model="result" typeahead="item for item in source | filter:$viewValue" typeahead-select-on-blur="true"></div>');
403+
var inputEl = findInput(element);
404+
405+
changeInputValueTo(element, 'b');
406+
inputEl.blur(); // input loses focus
407+
408+
// first element should be selected
409+
expect($scope.result).toEqual('bar');
410+
expect(inputEl.val()).toEqual('bar');
411+
});
412+
387413
it('should select match on click', function () {
388414

389415
var element = prepareInputEl('<div><input ng-model="result" typeahead="item for item in source | filter:$viewValue"></div>');

src/typeahead/typeahead.js

+8
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ angular.module('ui.bootstrap.typeahead', ['ui.bootstrap.position', 'ui.bootstrap
5959
//a callback executed when a match is selected
6060
var onSelectCallback = $parse(attrs.typeaheadOnSelect);
6161

62+
//should it select highlighted popup value when losing focus?
63+
var isSelectOnBlur = angular.isDefined(attrs.typeaheadSelectOnBlur) ? originalScope.$eval(attrs.typeaheadSelectOnBlur) : false;
64+
6265
var inputFormatter = attrs.typeaheadInputFormatter ? $parse(attrs.typeaheadInputFormatter) : undefined;
6366

6467
var appendToBody = attrs.typeaheadAppendToBody ? originalScope.$eval(attrs.typeaheadAppendToBody) : false;
@@ -357,6 +360,11 @@ angular.module('ui.bootstrap.typeahead', ['ui.bootstrap.position', 'ui.bootstrap
357360
});
358361

359362
element.bind('blur', function (evt) {
363+
if (isSelectOnBlur && scope.activeIdx >= 0) {
364+
scope.$apply(function () {
365+
scope.select(scope.activeIdx);
366+
});
367+
}
360368
hasFocus = false;
361369
});
362370

0 commit comments

Comments
 (0)