From 549c185c3c9da29a3337165def40fa85cc2e61a2 Mon Sep 17 00:00:00 2001 From: Daniel Zimmermann Date: Thu, 26 Feb 2015 17:25:19 +1100 Subject: [PATCH] fix(typeahead): close dropdown on tab with focus-first="false" --- src/typeahead/test/typeahead.spec.js | 16 +++++++++++++--- src/typeahead/typeahead.js | 11 +++++++++-- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/src/typeahead/test/typeahead.spec.js b/src/typeahead/test/typeahead.spec.js index 8f2c7323c1..6d72676b60 100644 --- a/src/typeahead/test/typeahead.spec.js +++ b/src/typeahead/test/typeahead.spec.js @@ -734,23 +734,33 @@ describe('typeahead tests', function () { }); }); - it('should not capture enter or tab until an item is focused', function () { + it('should not capture enter or tab when an item is not focused', function () { $scope.select_count = 0; $scope.onSelect = function ($item, $model, $label) { $scope.select_count = $scope.select_count + 1; }; var element = prepareInputEl('
'); changeInputValueTo(element, 'b'); - + // enter key should not be captured when nothing is focused triggerKeyDown(element, 13); expect($scope.keyDownEvent.isDefaultPrevented()).toBeFalsy(); expect($scope.select_count).toEqual(0); - // tab key should not be captured when nothing is focused + // tab key should close the dropdown when nothing is focused triggerKeyDown(element, 9); expect($scope.keyDownEvent.isDefaultPrevented()).toBeFalsy(); expect($scope.select_count).toEqual(0); + expect(element).toBeClosed(); + }); + + it('should capture enter or tab when an item is focused', function () { + $scope.select_count = 0; + $scope.onSelect = function ($item, $model, $label) { + $scope.select_count = $scope.select_count + 1; + }; + var element = prepareInputEl('
'); + changeInputValueTo(element, 'b'); // down key should be captured and focus first element triggerKeyDown(element, 40); diff --git a/src/typeahead/typeahead.js b/src/typeahead/typeahead.js index 8b080d615f..6bbd2df183 100644 --- a/src/typeahead/typeahead.js +++ b/src/typeahead/typeahead.js @@ -172,7 +172,7 @@ angular.module('ui.bootstrap.typeahead', ['ui.bootstrap.position', 'ui.bootstrap //we need to propagate user's query so we can higlight matches scope.query = undefined; - //Declare the timeout promise var outside the function scope so that stacked calls can be cancelled later + //Declare the timeout promise var outside the function scope so that stacked calls can be cancelled later var timeoutPromise; var scheduleSearchWithTimeout = function(inputValue) { @@ -275,7 +275,14 @@ angular.module('ui.bootstrap.typeahead', ['ui.bootstrap.position', 'ui.bootstrap } // if there's nothing selected (i.e. focusFirst) and enter is hit, don't do anything - if (scope.activeIdx == -1 && (evt.which === 13 || evt.which === 9)) { + if (scope.activeIdx == -1 && evt.which === 13) { + return; + } + + // if there's nothing selected (i.e. focusFirst) and tab is hit, clear the results + if (scope.activeIdx == -1 && evt.which === 9) { + resetMatches(); + scope.$digest(); return; }