From b5ecda30c339177294af919c2b35c91e123dc780 Mon Sep 17 00:00:00 2001 From: Jesus Rodriguez Date: Sun, 16 Aug 2015 19:12:57 +0200 Subject: [PATCH] feat(typeahead): add `typeaheadFocusOnSelect` Closes #4212 Closes #4211 Fixes #4206 --- src/typeahead/docs/readme.md | 4 ++++ src/typeahead/test/typeahead.spec.js | 34 ++++++++++++++++++++-------- src/typeahead/typeahead.js | 4 +++- 3 files changed, 32 insertions(+), 10 deletions(-) diff --git a/src/typeahead/docs/readme.md b/src/typeahead/docs/readme.md index 71892ac6dd..dc37115517 100644 --- a/src/typeahead/docs/readme.md +++ b/src/typeahead/docs/readme.md @@ -67,3 +67,7 @@ The typeahead directives provide several attributes: * `typeahead-select-on-blur` _(Defaults: false)_ : On blur, select the currently highlighted match + +* `typeahead-focus-on-select` + _(Defaults: true) : + On selection, focus the input element the typeahead directive is associated with diff --git a/src/typeahead/test/typeahead.spec.js b/src/typeahead/test/typeahead.spec.js index 92a75de3f4..a5904c20ce 100644 --- a/src/typeahead/test/typeahead.spec.js +++ b/src/typeahead/test/typeahead.spec.js @@ -469,8 +469,24 @@ describe('typeahead tests', function() { $timeout.flush(); expect($scope.isNoResults).toBeFalsy(); })); + + it('should not focus the input if `typeahead-focus-on-select` is false', function() { + var element = prepareInputEl('
'); + $document.find('body').append(element); + var inputEl = findInput(element); + + changeInputValueTo(element, 'b'); + var match = $(findMatches(element)[1]).find('a')[0]; + + $(match).click(); + $scope.$digest(); + $timeout.flush(); + + expect(document.activeElement).not.toBe(inputEl[0]); + expect($scope.result).toEqual('baz'); + }); }); - + describe('select on exact match', function() { it('should select on an exact match when set', function() { $scope.onSelect = jasmine.createSpy('onSelect'); @@ -478,45 +494,45 @@ describe('typeahead tests', function() { var inputEl = findInput(element); changeInputValueTo(element, 'bar'); - + expect($scope.result).toEqual('bar'); expect(inputEl.val()).toEqual('bar'); expect(element).toBeClosed(); expect($scope.onSelect).toHaveBeenCalled(); }); - + it('should not select on an exact match by default', function() { $scope.onSelect = jasmine.createSpy('onSelect'); var element = prepareInputEl('
'); var inputEl = findInput(element); - + changeInputValueTo(element, 'bar'); - + expect($scope.result).toBeUndefined(); expect(inputEl.val()).toEqual('bar'); expect($scope.onSelect.calls.any()).toBe(false); }); - + it('should not be case sensitive when select on an exact match', function() { $scope.onSelect = jasmine.createSpy('onSelect'); var element = prepareInputEl('
'); var inputEl = findInput(element); changeInputValueTo(element, 'BaR'); - + expect($scope.result).toEqual('bar'); expect(inputEl.val()).toEqual('bar'); expect(element).toBeClosed(); expect($scope.onSelect).toHaveBeenCalled(); }); - + it('should not auto select when not a match with one potential result left', function() { $scope.onSelect = jasmine.createSpy('onSelect'); var element = prepareInputEl('
'); var inputEl = findInput(element); changeInputValueTo(element, 'fo'); - + expect($scope.result).toBeUndefined(); expect(inputEl.val()).toEqual('fo'); expect($scope.onSelect.calls.any()).toBe(false); diff --git a/src/typeahead/typeahead.js b/src/typeahead/typeahead.js index db661ba0ca..c17ad20f2e 100644 --- a/src/typeahead/typeahead.js +++ b/src/typeahead/typeahead.js @@ -340,7 +340,9 @@ angular.module('ui.bootstrap.typeahead', ['ui.bootstrap.position', 'ui.bootstrap //return focus to the input element if a match was selected via a mouse click event // use timeout to avoid $rootScope:inprog error - $timeout(function() { element[0].focus(); }, 0, false); + if (scope.$eval(attrs.typeaheadFocusOnSelect) !== false) { + $timeout(function() { element[0].focus(); }, 0, false); + } }; //bind keyboard events: arrows up(38) / down(40), enter(13) and tab(9), esc(27)