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

fix(tagsInput): Fixes the "jumping focus" when tab key is hit #821

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
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