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

Commit 64e3127

Browse files
deegwesleycho
authored andcommitted
fix(typeahead): fix shift tab
Stops the tab completion action from happening when shift key is also pressed. Closes #5494 Fixes #5493
1 parent d1553a4 commit 64e3127

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

src/typeahead/test/typeahead.spec.js

+22-1
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,14 @@ describe('typeahead tests', function() {
6464
return findDropDown(element).find('li');
6565
};
6666

67-
var triggerKeyDown = function(element, keyCode) {
67+
var triggerKeyDown = function(element, keyCode, options) {
68+
options = options || {};
6869
var inputEl = findInput(element);
6970
var e = $.Event('keydown');
7071
e.which = keyCode;
72+
if (options.shiftKey) {
73+
e.shiftKey = true;
74+
}
7175
inputEl.trigger(e);
7276
};
7377

@@ -1351,6 +1355,23 @@ describe('typeahead tests', function() {
13511355
expect(element).toBeClosed();
13521356
});
13531357

1358+
it("should not capture tab when shift key is pressed", function(){
1359+
$scope.select_count = 0;
1360+
$scope.onSelect = function($item, $model, $label) {
1361+
$scope.select_count = $scope.select_count + 1;
1362+
};
1363+
var element = prepareInputEl('<div><input ng-model="result" ng-keydown="keyDownEvent = $event" uib-typeahead="item for item in source | filter:$viewValue" typeahead-on-select="onSelect($item, $model, $label)" typeahead-focus-first="false"></div>');
1364+
changeInputValueTo(element, 'b');
1365+
1366+
// down key should be captured and focus first element
1367+
triggerKeyDown(element, 40);
1368+
1369+
triggerKeyDown(element, 9, {shiftKey: true});
1370+
expect($scope.keyDownEvent.isDefaultPrevented()).toBeFalsy();
1371+
expect($scope.select_count).toEqual(0);
1372+
expect(element).toBeClosed();
1373+
});
1374+
13541375
it('should capture enter or tab when an item is focused', function() {
13551376
$scope.select_count = 0;
13561377
$scope.onSelect = function($item, $model, $label) {

src/typeahead/typeahead.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -370,8 +370,13 @@ angular.module('ui.bootstrap.typeahead', ['ui.bootstrap.debounce', 'ui.bootstrap
370370
return;
371371
}
372372

373-
// if there's nothing selected (i.e. focusFirst) and enter or tab is hit, clear the results
374-
if (scope.activeIdx === -1 && (evt.which === 9 || evt.which === 13)) {
373+
/**
374+
* if there's nothing selected (i.e. focusFirst) and enter or tab is hit
375+
* or
376+
* shift + tab is pressed to bring focus to the previous element
377+
* then clear the results
378+
*/
379+
if (scope.activeIdx === -1 && (evt.which === 9 || evt.which === 13) || evt.which === 9 && !!evt.shiftKey) {
375380
resetMatches();
376381
scope.$digest();
377382
return;

0 commit comments

Comments
 (0)