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

Commit b5ecda3

Browse files
committed
feat(typeahead): add typeaheadFocusOnSelect
Closes #4212 Closes #4211 Fixes #4206
1 parent f8eab55 commit b5ecda3

File tree

3 files changed

+32
-10
lines changed

3 files changed

+32
-10
lines changed

src/typeahead/docs/readme.md

+4
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,7 @@ The typeahead directives provide several attributes:
6767
* `typeahead-select-on-blur`
6868
_(Defaults: false)_ :
6969
On blur, select the currently highlighted match
70+
71+
* `typeahead-focus-on-select`
72+
_(Defaults: true) :
73+
On selection, focus the input element the typeahead directive is associated with

src/typeahead/test/typeahead.spec.js

+25-9
Original file line numberDiff line numberDiff line change
@@ -469,54 +469,70 @@ describe('typeahead tests', function() {
469469
$timeout.flush();
470470
expect($scope.isNoResults).toBeFalsy();
471471
}));
472+
473+
it('should not focus the input if `typeahead-focus-on-select` is false', function() {
474+
var element = prepareInputEl('<div><input ng-model="result" typeahead="item for item in source | filter:$viewValue" typeahead-focus-on-select="false"></div>');
475+
$document.find('body').append(element);
476+
var inputEl = findInput(element);
477+
478+
changeInputValueTo(element, 'b');
479+
var match = $(findMatches(element)[1]).find('a')[0];
480+
481+
$(match).click();
482+
$scope.$digest();
483+
$timeout.flush();
484+
485+
expect(document.activeElement).not.toBe(inputEl[0]);
486+
expect($scope.result).toEqual('baz');
487+
});
472488
});
473-
489+
474490
describe('select on exact match', function() {
475491
it('should select on an exact match when set', function() {
476492
$scope.onSelect = jasmine.createSpy('onSelect');
477493
var element = prepareInputEl('<div><input ng-model="result" typeahead-editable="false" typeahead-on-select="onSelect()" typeahead="item for item in source | filter:$viewValue" typeahead-select-on-exact="true"></div>');
478494
var inputEl = findInput(element);
479495

480496
changeInputValueTo(element, 'bar');
481-
497+
482498
expect($scope.result).toEqual('bar');
483499
expect(inputEl.val()).toEqual('bar');
484500
expect(element).toBeClosed();
485501
expect($scope.onSelect).toHaveBeenCalled();
486502
});
487-
503+
488504
it('should not select on an exact match by default', function() {
489505
$scope.onSelect = jasmine.createSpy('onSelect');
490506
var element = prepareInputEl('<div><input ng-model="result" typeahead-editable="false" typeahead-on-select="onSelect()" typeahead="item for item in source | filter:$viewValue"></div>');
491507
var inputEl = findInput(element);
492-
508+
493509
changeInputValueTo(element, 'bar');
494-
510+
495511
expect($scope.result).toBeUndefined();
496512
expect(inputEl.val()).toEqual('bar');
497513
expect($scope.onSelect.calls.any()).toBe(false);
498514
});
499-
515+
500516
it('should not be case sensitive when select on an exact match', function() {
501517
$scope.onSelect = jasmine.createSpy('onSelect');
502518
var element = prepareInputEl('<div><input ng-model="result" typeahead-editable="false" typeahead-on-select="onSelect()" typeahead="item for item in source | filter:$viewValue" typeahead-select-on-exact="true"></div>');
503519
var inputEl = findInput(element);
504520

505521
changeInputValueTo(element, 'BaR');
506-
522+
507523
expect($scope.result).toEqual('bar');
508524
expect(inputEl.val()).toEqual('bar');
509525
expect(element).toBeClosed();
510526
expect($scope.onSelect).toHaveBeenCalled();
511527
});
512-
528+
513529
it('should not auto select when not a match with one potential result left', function() {
514530
$scope.onSelect = jasmine.createSpy('onSelect');
515531
var element = prepareInputEl('<div><input ng-model="result" typeahead-editable="false" typeahead-on-select="onSelect()" typeahead="item for item in source | filter:$viewValue" typeahead-select-on-exact="true"></div>');
516532
var inputEl = findInput(element);
517533

518534
changeInputValueTo(element, 'fo');
519-
535+
520536
expect($scope.result).toBeUndefined();
521537
expect(inputEl.val()).toEqual('fo');
522538
expect($scope.onSelect.calls.any()).toBe(false);

src/typeahead/typeahead.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,9 @@ angular.module('ui.bootstrap.typeahead', ['ui.bootstrap.position', 'ui.bootstrap
340340

341341
//return focus to the input element if a match was selected via a mouse click event
342342
// use timeout to avoid $rootScope:inprog error
343-
$timeout(function() { element[0].focus(); }, 0, false);
343+
if (scope.$eval(attrs.typeaheadFocusOnSelect) !== false) {
344+
$timeout(function() { element[0].focus(); }, 0, false);
345+
}
344346
};
345347

346348
//bind keyboard events: arrows up(38) / down(40), enter(13) and tab(9), esc(27)

0 commit comments

Comments
 (0)