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

Commit 3f75b51

Browse files
committed
Update to support comma and space on mobile keyboards
1 parent 87d0e6b commit 3f75b51

3 files changed

Lines changed: 57 additions & 1 deletion

File tree

src/tags-input.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,8 @@ export default function TagsInputDirective($timeout, $document, $window, $q, tag
113113
self.clearSelection();
114114
events.trigger('tag-removed', { $tag: tag });
115115
return tag;
116+
}).catch(() => {
117+
//can't remove tag
116118
});
117119
};
118120

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

315317
scope.eventHandlers = {
316318
input: {
319+
textInput: function($event) {
320+
events.trigger('text-input', $event);
321+
},
317322
keydown($event) {
318323
events.trigger('input-keydown', $event);
319324
},
@@ -399,6 +404,24 @@ export default function TagsInputDirective($timeout, $document, $window, $q, tag
399404
element.triggerHandler('blur');
400405
setElementValidity();
401406
})
407+
.on('text-input', function(event) {
408+
// on mobile keydown doesn't provide keyCodes for space or comma (most keys really),
409+
// this will translate it so proper handling is triggered if those are pressed
410+
411+
let originalKey = event.originalEvent.data;
412+
let keyCode = null;
413+
414+
if (originalKey === " ") {
415+
keyCode = tiConstants.KEYS.space;
416+
} else if (originalKey === ",") {
417+
keyCode = tiConstants.KEYS.comma;
418+
}
419+
420+
if (keyCode) {
421+
event.keyCode = keyCode;
422+
events.trigger('input-keydown', event);
423+
}
424+
})
402425
.on('input-keydown', event => {
403426
let key = event.keyCode;
404427

templates/tags-input.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
ng-model="newTag.text"
1414
ng-model-options="{getterSetter: true}"
1515
ng-keydown="eventHandlers.input.keydown($event)"
16+
ng-on-text_input="eventHandlers.input.textInput($event)"
1617
ng-focus="eventHandlers.input.focus($event)"
1718
ng-blur="eventHandlers.input.blur($event)"
1819
ng-paste="eventHandlers.input.paste($event)"
@@ -22,4 +23,4 @@
2223
ti-bind-attrs="{type: options.type, placeholder: options.placeholder, tabindex: options.tabindex, spellcheck: options.spellcheck}"
2324
ti-autosize>
2425
</div>
25-
</div>
26+
</div>

test/tags-input.spec.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,13 @@ describe('tags-input directive', () => {
8787
return event;
8888
}
8989

90+
function sendTextInput(data, properties) {
91+
let event = jQuery.Event('textInput', angular.extend( { originalEvent: {data: data} }, properties || {}));
92+
getInput().trigger(event);
93+
94+
return event;
95+
}
96+
9097
function sendBackspace() {
9198
let event = sendKeyDown(constants.KEYS.backspace);
9299

@@ -577,6 +584,31 @@ describe('tags-input directive', () => {
577584
});
578585
});
579586

587+
describe('add-on-comma option nokeypress', () => {
588+
it('adds a new tag when the comma key is pressed and the option is true with no keypress event', () => {
589+
// Arrange
590+
compile('add-on-comma="true"');
591+
592+
// Act
593+
let tag = 'foo';
594+
tag.split('').forEach((char, index) => {
595+
sendKeyPress(tag.charCodeAt(index));
596+
});
597+
sendTextInput(',');
598+
599+
// Assert
600+
expect($scope.tags).toEqual([{ text: 'foo' }]);
601+
});
602+
603+
it('initializes the option to true', () => {
604+
// Arrange/Act
605+
compile();
606+
607+
// Assert
608+
expect(isolateScope.options.addOnComma).toBe(true);
609+
});
610+
});
611+
580612
describe('add-on-blur option', () => {
581613
it('initializes the option to true', () => {
582614
// Arrange/Act

0 commit comments

Comments
 (0)