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

Commit

Permalink
Update to support comma and space on mobile keyboards
Browse files Browse the repository at this point in the history
  • Loading branch information
phazei committed Dec 2, 2019
1 parent 87d0e6b commit 3f75b51
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 1 deletion.
23 changes: 23 additions & 0 deletions src/tags-input.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ export default function TagsInputDirective($timeout, $document, $window, $q, tag
self.clearSelection();
events.trigger('tag-removed', { $tag: tag });
return tag;
}).catch(() => {
//can't remove tag
});
};

Expand Down Expand Up @@ -314,6 +316,9 @@ export default function TagsInputDirective($timeout, $document, $window, $q, tag

scope.eventHandlers = {
input: {
textInput: function($event) {
events.trigger('text-input', $event);
},
keydown($event) {
events.trigger('input-keydown', $event);
},
Expand Down Expand Up @@ -399,6 +404,24 @@ export default function TagsInputDirective($timeout, $document, $window, $q, tag
element.triggerHandler('blur');
setElementValidity();
})
.on('text-input', function(event) {
// on mobile keydown doesn't provide keyCodes for space or comma (most keys really),
// this will translate it so proper handling is triggered if those are pressed

let originalKey = event.originalEvent.data;
let keyCode = null;

if (originalKey === " ") {
keyCode = tiConstants.KEYS.space;
} else if (originalKey === ",") {
keyCode = tiConstants.KEYS.comma;
}

if (keyCode) {
event.keyCode = keyCode;
events.trigger('input-keydown', event);
}
})
.on('input-keydown', event => {
let key = event.keyCode;

Expand Down
3 changes: 2 additions & 1 deletion templates/tags-input.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
ng-model="newTag.text"
ng-model-options="{getterSetter: true}"
ng-keydown="eventHandlers.input.keydown($event)"
ng-on-text_input="eventHandlers.input.textInput($event)"
ng-focus="eventHandlers.input.focus($event)"
ng-blur="eventHandlers.input.blur($event)"
ng-paste="eventHandlers.input.paste($event)"
Expand All @@ -22,4 +23,4 @@
ti-bind-attrs="{type: options.type, placeholder: options.placeholder, tabindex: options.tabindex, spellcheck: options.spellcheck}"
ti-autosize>
</div>
</div>
</div>
32 changes: 32 additions & 0 deletions test/tags-input.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,13 @@ describe('tags-input directive', () => {
return event;
}

function sendTextInput(data, properties) {
let event = jQuery.Event('textInput', angular.extend( { originalEvent: {data: data} }, properties || {}));
getInput().trigger(event);

return event;
}

function sendBackspace() {
let event = sendKeyDown(constants.KEYS.backspace);

Expand Down Expand Up @@ -577,6 +584,31 @@ describe('tags-input directive', () => {
});
});

describe('add-on-comma option nokeypress', () => {
it('adds a new tag when the comma key is pressed and the option is true with no keypress event', () => {
// Arrange
compile('add-on-comma="true"');

// Act
let tag = 'foo';
tag.split('').forEach((char, index) => {
sendKeyPress(tag.charCodeAt(index));
});
sendTextInput(',');

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

it('initializes the option to true', () => {
// Arrange/Act
compile();

// Assert
expect(isolateScope.options.addOnComma).toBe(true);
});
});

describe('add-on-blur option', () => {
it('initializes the option to true', () => {
// Arrange/Act
Expand Down

0 comments on commit 3f75b51

Please sign in to comment.