Skip to content
This repository has been archived by the owner on Nov 22, 2021. It is now read-only.

Commit

Permalink
fix(tagsInput): Fixes the "jumping focus" when tab key is hit
Browse files Browse the repository at this point in the history
When the tab key is hit (in order to add the tag to the list), the
"jumping focus" problem will no longer occur.

Closes #820.
  • Loading branch information
belfz committed Sep 4, 2017
1 parent 87d0e6b commit c0d470c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/tags-input.js
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ export default function TagsInputDirective($timeout, $document, $window, $q, tag
});
},
link(scope, element, attrs, ngModelCtrl) {
let hotkeys = [tiConstants.KEYS.enter, tiConstants.KEYS.comma, tiConstants.KEYS.space, tiConstants.KEYS.backspace,
let hotkeys = [tiConstants.KEYS.enter, tiConstants.KEYS.tab, tiConstants.KEYS.comma, tiConstants.KEYS.space, tiConstants.KEYS.backspace,
tiConstants.KEYS.delete, tiConstants.KEYS.left, tiConstants.KEYS.right];
let tagList = scope.tagList;
let events = scope.events;
Expand Down Expand Up @@ -412,6 +412,10 @@ export default function TagsInputDirective($timeout, $document, $window, $q, tag
[tiConstants.KEYS.space]: options.addOnSpace
};

if (key === tiConstants.KEYS.tab && scope.newTag.text().length > 0) {
addKeys[tiConstants.KEYS.tab] = options.addOnBlur;
}

let shouldAdd = !options.addFromAutocompleteOnly && addKeys[key];
let shouldRemove = (key === tiConstants.KEYS.backspace || key === tiConstants.KEYS.delete) && tagList.selected;
let shouldEditLastTag = key === tiConstants.KEYS.backspace && scope.newTag.text().length === 0 && options.enableEditingLastTag;
Expand Down
11 changes: 11 additions & 0 deletions test/tags-input.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -643,6 +643,17 @@ describe('tags-input directive', () => {
// Assert
expect($scope.tags).toBeUndefined();
});

it('adds a new tag when the tab key is pressed and the input has text', () => {
// Arrange
isolateScope.newTag.text('foo');

// Act
newTag('foo', constants.KEYS.tab);

// Assert
expect($scope.tags).toEqual([{ text: 'foo' }]);
});
});

describe('option is off', () => {
Expand Down

0 comments on commit c0d470c

Please sign in to comment.