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

Commit 493510d

Browse files
dlukezwesleycho
authored andcommitted
fix(typeahead): close dropdown on tab with no selection
- When no item is selected and tab is hit, close the dropdown Closes #3340
1 parent f141201 commit 493510d

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

src/typeahead/test/typeahead.spec.js

+12-2
Original file line numberDiff line numberDiff line change
@@ -863,7 +863,7 @@ describe('typeahead tests', function () {
863863
});
864864
});
865865

866-
it('should not capture enter or tab until an item is focused', function () {
866+
it('should not capture enter or tab when an item is not focused', function () {
867867
$scope.select_count = 0;
868868
$scope.onSelect = function ($item, $model, $label) {
869869
$scope.select_count = $scope.select_count + 1;
@@ -876,10 +876,20 @@ describe('typeahead tests', function () {
876876
expect($scope.keyDownEvent.isDefaultPrevented()).toBeFalsy();
877877
expect($scope.select_count).toEqual(0);
878878

879-
// tab key should not be captured when nothing is focused
879+
// tab key should close the dropdown when nothing is focused
880880
triggerKeyDown(element, 9);
881881
expect($scope.keyDownEvent.isDefaultPrevented()).toBeFalsy();
882882
expect($scope.select_count).toEqual(0);
883+
expect(element).toBeClosed();
884+
});
885+
886+
it('should capture enter or tab when an item is focused', function () {
887+
$scope.select_count = 0;
888+
$scope.onSelect = function ($item, $model, $label) {
889+
$scope.select_count = $scope.select_count + 1;
890+
};
891+
var element = prepareInputEl('<div><input ng-model="result" ng-keydown="keyDownEvent = $event" typeahead="item for item in source | filter:$viewValue" typeahead-on-select="onSelect($item, $model, $label)" typeahead-focus-first="false"></div>');
892+
changeInputValueTo(element, 'b');
883893

884894
// down key should be captured and focus first element
885895
triggerKeyDown(element, 40);

src/typeahead/typeahead.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,14 @@ angular.module('ui.bootstrap.typeahead', ['ui.bootstrap.position', 'ui.bootstrap
332332
}
333333

334334
// if there's nothing selected (i.e. focusFirst) and enter is hit, don't do anything
335-
if (scope.activeIdx == -1 && (evt.which === 13 || evt.which === 9)) {
335+
if (scope.activeIdx === -1 && evt.which === 13) {
336+
return;
337+
}
338+
339+
// if there's nothing selected (i.e. focusFirst) and tab is hit, clear the results
340+
if (scope.activeIdx === -1 && evt.which === 9) {
341+
resetMatches();
342+
scope.$digest();
336343
return;
337344
}
338345

0 commit comments

Comments
 (0)