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

fix(typeahead): Fix shift tab #5494

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion src/typeahead/test/typeahead.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};

Expand Down Expand Up @@ -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('<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>');
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) {
Expand Down
9 changes: 7 additions & 2 deletions src/typeahead/typeahead.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down