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

Commit 2b26d9a

Browse files
committed
feat(typeahead): add typeaheadFocusOnSelect
- Add ability to disable focusing the input on selection
1 parent 71e0b8a commit 2b26d9a

File tree

5 files changed

+40
-12
lines changed

5 files changed

+40
-12
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -2099,7 +2099,7 @@ describe('datepicker directive', function() {
20992099
});
21002100

21012101
afterEach(function() {
2102-
$document.find('body').find('.dropdown-menu').remove();
2102+
$document.find('body').children().remove();
21032103
});
21042104

21052105
it('should append to the body', function() {

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

+4
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,8 @@ describe('$modal', function () {
396396
lastElement.focus();
397397
triggerKeyDown(lastElement, 9);
398398
expect(document.activeElement.getAttribute('id')).toBe('tab-focus-link');
399+
400+
initialPage.remove();
399401
});
400402

401403
it('should change focus to last element when shift+tab key is pressed', function() {
@@ -413,6 +415,8 @@ describe('$modal', function () {
413415
lastElement.focus();
414416
triggerKeyDown(lastElement, 9, true);
415417
expect(document.activeElement.getAttribute('id')).toBe('tab-focus-button');
418+
419+
initialPage.remove();
416420
});
417421
});
418422

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

+5-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ The typeahead directives provide several attributes:
2727
* `typeahead-editable` <i class="glyphicon glyphicon-eye-open"></i>
2828
_(Defaults: true)_ :
2929
Should it restrict model values to the ones selected from the popup only ?
30-
30+
3131
* `typeahead-focus-first`
3232
_(Defaults: true)_ :
3333
Should the first match automatically be focused as you type?
@@ -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

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

+27-9
Original file line numberDiff line numberDiff line change
@@ -469,54 +469,72 @@ 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+
var inputEl = findInput(element);
476+
angular.element(document.body).append(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+
488+
element.remove();
489+
});
472490
});
473-
491+
474492
describe('select on exact match', function() {
475493
it('should select on an exact match when set', function() {
476494
$scope.onSelect = jasmine.createSpy('onSelect');
477495
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>');
478496
var inputEl = findInput(element);
479497

480498
changeInputValueTo(element, 'bar');
481-
499+
482500
expect($scope.result).toEqual('bar');
483501
expect(inputEl.val()).toEqual('bar');
484502
expect(element).toBeClosed();
485503
expect($scope.onSelect).toHaveBeenCalled();
486504
});
487-
505+
488506
it('should not select on an exact match by default', function() {
489507
$scope.onSelect = jasmine.createSpy('onSelect');
490508
var element = prepareInputEl('<div><input ng-model="result" typeahead-editable="false" typeahead-on-select="onSelect()" typeahead="item for item in source | filter:$viewValue"></div>');
491509
var inputEl = findInput(element);
492-
510+
493511
changeInputValueTo(element, 'bar');
494-
512+
495513
expect($scope.result).toBeUndefined();
496514
expect(inputEl.val()).toEqual('bar');
497515
expect($scope.onSelect.calls.any()).toBe(false);
498516
});
499-
517+
500518
it('should not be case sensitive when select on an exact match', function() {
501519
$scope.onSelect = jasmine.createSpy('onSelect');
502520
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>');
503521
var inputEl = findInput(element);
504522

505523
changeInputValueTo(element, 'BaR');
506-
524+
507525
expect($scope.result).toEqual('bar');
508526
expect(inputEl.val()).toEqual('bar');
509527
expect(element).toBeClosed();
510528
expect($scope.onSelect).toHaveBeenCalled();
511529
});
512-
530+
513531
it('should not auto select when not a match with one potential result left', function() {
514532
$scope.onSelect = jasmine.createSpy('onSelect');
515533
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>');
516534
var inputEl = findInput(element);
517535

518536
changeInputValueTo(element, 'fo');
519-
537+
520538
expect($scope.result).toBeUndefined();
521539
expect(inputEl.val()).toEqual('fo');
522540
expect($scope.onSelect.calls.any()).toBe(false);

Diff for: 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 (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)