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

Commit a269ccc

Browse files
committed
Added preventSubmit prop to allow for submiting a form when pressing return with no tags
1 parent 07e2421 commit a269ccc

File tree

5 files changed

+102
-8
lines changed

5 files changed

+102
-8
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ Highly customizable [React](http://facebook.github.io/react/index.html) componen
4444
* [renderTag](#rendertag)
4545
* [renderInput](#renderinput)
4646
* [renderLayout](#renderlayout)
47+
* [preventSubmit](#preventSubmit)
4748
* [Methods](#methods)
4849
* [focus()](#focus)
4950
* [blur()](#blur)
@@ -365,6 +366,13 @@ function defaultRenderLayout (tagComponents, inputComponent) {
365366
}
366367
```
367368

369+
##### preventSubmit
370+
371+
A `boolean` to prevent the default submit event when adding an 'empty' tag.
372+
Default: `true`
373+
374+
Set to `false` if you want the default submit to fire when pressing enter again after adding a tag.
375+
368376
### Methods
369377

370378
##### focus()

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
"react-dom": "15.x.x",
2626
"react-input-autosize": "^1.0.0",
2727
"reactpack": "^0.9.0",
28+
"sinon": "^2.1.0",
2829
"standard": "^8.6.0"
2930
},
3031
"scripts": {

react-tagsinput.js

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,19 @@
330330
return false;
331331
}
332332
}, {
333+
key: '_shouldPreventDefaultEventOnAdd',
334+
value: function _shouldPreventDefaultEventOnAdd(added, empty, keyCode) {
335+
if (added) {
336+
return true;
337+
}
338+
339+
if (keyCode === 13) {
340+
return this.props.preventSubmit || !this.props.preventSubmit && !empty;
341+
}
342+
343+
return false;
344+
}
345+
}, {
333346
key: 'focus',
334347
value: function focus() {
335348
if (this.refs.input && typeof this.refs.input.focus === 'function') {
@@ -412,8 +425,7 @@
412425

413426
if (add) {
414427
var added = this.accept();
415-
// Special case for preventing forms submitting.
416-
if (added || keyCode === 13) {
428+
if (this._shouldPreventDefaultEventOnAdd(added, empty, keyCode)) {
417429
e.preventDefault();
418430
}
419431
}
@@ -633,7 +645,8 @@
633645
maxTags: _react2.default.PropTypes.number,
634646
validationRegex: _react2.default.PropTypes.instanceOf(RegExp),
635647
disabled: _react2.default.PropTypes.bool,
636-
tagDisplayProp: _react2.default.PropTypes.string
648+
tagDisplayProp: _react2.default.PropTypes.string,
649+
preventSubmit: _react2.default.PropTypes.bool
637650
};
638651
TagsInput.defaultProps = {
639652
className: 'react-tagsinput',
@@ -652,7 +665,8 @@
652665
maxTags: -1,
653666
validationRegex: /.*/,
654667
disabled: false,
655-
tagDisplayProp: null
668+
tagDisplayProp: null,
669+
preventSubmit: true
656670
};
657671
exports.default = TagsInput;
658672
module.exports = exports['default'];

src/index.js

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,8 @@ class TagsInput extends React.Component {
105105
maxTags: React.PropTypes.number,
106106
validationRegex: React.PropTypes.instanceOf(RegExp),
107107
disabled: React.PropTypes.bool,
108-
tagDisplayProp: React.PropTypes.string
108+
tagDisplayProp: React.PropTypes.string,
109+
preventSubmit: React.PropTypes.bool
109110
}
110111

111112
static defaultProps = {
@@ -125,7 +126,8 @@ class TagsInput extends React.Component {
125126
maxTags: -1,
126127
validationRegex: /.*/,
127128
disabled: false,
128-
tagDisplayProp: null
129+
tagDisplayProp: null,
130+
preventSubmit: true
129131
}
130132

131133
_getTagDisplayValue (tag) {
@@ -219,6 +221,18 @@ class TagsInput extends React.Component {
219221
return false
220222
}
221223

224+
_shouldPreventDefaultEventOnAdd (added, empty, keyCode) {
225+
if (added) {
226+
return true
227+
}
228+
229+
if (keyCode === 13) {
230+
return (this.props.preventSubmit || !this.props.preventSubmit && !empty)
231+
}
232+
233+
return false
234+
}
235+
222236
focus () {
223237
if (this.refs.input && typeof this.refs.input.focus === 'function') {
224238
this.refs.input.focus()
@@ -283,8 +297,7 @@ class TagsInput extends React.Component {
283297

284298
if (add) {
285299
let added = this.accept()
286-
// Special case for preventing forms submitting.
287-
if (added || keyCode === 13) {
300+
if (this._shouldPreventDefaultEventOnAdd(added, empty, keyCode)) {
288301
e.preventDefault()
289302
}
290303
}

test/index.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const TagsInput = require("../src");
88
const React = require("react");
99
const TestUtils = require("react-addons-test-utils");
1010
const assert = require("assert");
11+
const sinon = require('sinon');
1112

1213
class TestComponent extends React.Component {
1314
constructor() {
@@ -512,10 +513,67 @@ describe("TagsInput", () => {
512513
remove(comp);
513514
});
514515

516+
515517
it("should disable input when component is disabled", () => {
516518
let comp = TestUtils.renderIntoDocument(<TestComponent disabled={true} />);
517519
assert.ok(comp.tagsinput().refs.input.disabled, "input should be disabled");
518520
});
521+
522+
describe('preventSubmit', () => {
523+
524+
function addTagWithEventSpy(comp, tag, preventDefaultSpy) {
525+
change(comp, tag);
526+
TestUtils.Simulate.keyDown(comp.input(), { keyCode: 13, preventDefault: preventDefaultSpy });
527+
}
528+
529+
describe("when to to true", () => {
530+
it("should prevent default submit event on enter key when adding a tag ", () => {
531+
let comp = TestUtils.renderIntoDocument(<TestComponent preventSubmit={true} />);
532+
const preventDefault = sinon.spy();
533+
534+
addTagWithEventSpy(comp, "Tag", preventDefault);
535+
assert.equal(preventDefault.called, true, "preventDefault was not called when it should be");
536+
});
537+
538+
it("should prevent default submit on enter key when tag is empty when prop is true", () => {
539+
let comp = TestUtils.renderIntoDocument(<TestComponent preventSubmit={true} />);
540+
const preventDefault = sinon.spy();
541+
542+
addTagWithEventSpy(comp, "", preventDefault);
543+
assert.equal(preventDefault.called, true, "preventDefault was not called when it should be");
544+
});
545+
546+
});
547+
548+
describe("when set to false", () => {
549+
it("should not prevent default submit on enter key when tag is empty", () => {
550+
let comp = TestUtils.renderIntoDocument(<TestComponent preventSubmit={false} />);
551+
const preventDefault = sinon.spy();
552+
553+
addTagWithEventSpy(comp, "", preventDefault);
554+
assert.equal(preventDefault.called, false, "preventDefault was called when it should not be");
555+
});
556+
557+
it("should still prevent default submit on enter key when tag is not empty and added", () => {
558+
let comp = TestUtils.renderIntoDocument(<TestComponent preventSubmit={false} />);
559+
const preventDefault = sinon.spy();
560+
561+
addTagWithEventSpy(comp, "A tag", preventDefault);
562+
assert.equal(preventDefault.called, true, "preventDefault was not called when it should have been");
563+
});
564+
565+
it("should still prevent default submit event if a tag is rejected (unique etc..)", () => {
566+
let comp = TestUtils.renderIntoDocument(<TestComponent preventSubmit={false} onlyUnique={true} />);
567+
const preventDefault = sinon.spy();
568+
569+
add(comp, "Tag", 13);
570+
addTagWithEventSpy(comp, "Tag", preventDefault);
571+
572+
assert.equal(preventDefault.called, true, "preventDefault was not called when it should have been");
573+
});
574+
});
575+
});
576+
519577
});
520578

521579
describe("methods", () => {

0 commit comments

Comments
 (0)