From 2fb9677e487a5747beacd93ff85109f16bb317bd Mon Sep 17 00:00:00 2001 From: Daniel Gornstein Date: Tue, 16 Feb 2016 18:17:52 -0800 Subject: [PATCH] fix(typeahead): Fix shift tab Stops the tab completion action from happening when shift key is also pressed. Fixes #5493 --- src/typeahead/test/typeahead.spec.js | 23 ++++++++++++++++++++++- src/typeahead/typeahead.js | 9 +++++++-- 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/src/typeahead/test/typeahead.spec.js b/src/typeahead/test/typeahead.spec.js index f4d1806f14..850215e5ae 100644 --- a/src/typeahead/test/typeahead.spec.js +++ b/src/typeahead/test/typeahead.spec.js @@ -64,10 +64,14 @@ describe('typeahead tests', function() { return findDropDown(element).find('li'); }; - var triggerKeyDown = function(element, keyCode) { + var triggerKeyDown = function(element, keyCode, options) { + options = options || {}; var inputEl = findInput(element); var e = $.Event('keydown'); e.which = keyCode; + if (options.shiftKey) { + e.shiftKey = true; + } inputEl.trigger(e); }; @@ -1351,6 +1355,23 @@ describe('typeahead tests', function() { expect(element).toBeClosed(); }); + it("should not capture tab when shift key is pressed", 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); + + triggerKeyDown(element, 9, {shiftKey: true}); + 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) { diff --git a/src/typeahead/typeahead.js b/src/typeahead/typeahead.js index 2b94f0666a..ac856c8ebf 100644 --- a/src/typeahead/typeahead.js +++ b/src/typeahead/typeahead.js @@ -370,8 +370,13 @@ angular.module('ui.bootstrap.typeahead', ['ui.bootstrap.debounce', 'ui.bootstrap return; } - // if there's nothing selected (i.e. focusFirst) and enter or tab is hit, clear the results - if (scope.activeIdx === -1 && (evt.which === 9 || evt.which === 13)) { + /** + * if there's nothing selected (i.e. focusFirst) and enter or tab is hit + * or + * shift + tab is pressed to bring focus to the previous element + * then clear the results + */ + if (scope.activeIdx === -1 && (evt.which === 9 || evt.which === 13) || evt.which === 9 && !!evt.shiftKey) { resetMatches(); scope.$digest(); return;