diff --git a/src/typeahead/test/typeahead.spec.js b/src/typeahead/test/typeahead.spec.js index 09261c17a1..885546bcc8 100644 --- a/src/typeahead/test/typeahead.spec.js +++ b/src/typeahead/test/typeahead.spec.js @@ -863,7 +863,7 @@ 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; @@ -876,10 +876,20 @@ describe('typeahead tests', function () { 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 7abac01f2f..0a63f63247 100644 --- a/src/typeahead/typeahead.js +++ b/src/typeahead/typeahead.js @@ -332,7 +332,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; }