Skip to content

Commit caa86aa

Browse files
author
Ola Holmström
committed
2 parents 10dd19e + 7e2f1f0 commit caa86aa

4 files changed

Lines changed: 46 additions & 24 deletions

File tree

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ An array of key codes that add a tag, default is `[9, 13]` (Tab and Enter).
6666

6767
Allow only unique tags, default is `false`.
6868

69+
##### maxTags
70+
71+
Allow limit number of tags, default is `-1` for infinite.
72+
6973
##### addOnBlur
7074

7175
Add a tag if input blurs.

react-tagsinput.js

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -106,11 +106,13 @@
106106
}, {
107107
key: '_addTag',
108108
value: function _addTag(tag) {
109-
var onlyUnique = this.props.onlyUnique;
109+
var _props = this.props;
110+
var onlyUnique = _props.onlyUnique;
111+
var maxTags = _props.maxTags;
110112

111113
var isUnique = this.props.value.indexOf(tag) === -1;
112-
113-
if (tag !== '' && (isUnique || !onlyUnique)) {
114+
var limit = maxTags ? this.props.value.length < maxTags : true;
115+
if (tag !== '' && limit && (isUnique || !onlyUnique)) {
114116
var value = this.props.value.concat([tag]);
115117
this.props.onChange(value);
116118
this._clearInput();
@@ -129,10 +131,10 @@
129131
}, {
130132
key: 'handleKeyDown',
131133
value: function handleKeyDown(e) {
132-
var _props = this.props;
133-
var value = _props.value;
134-
var removeKeys = _props.removeKeys;
135-
var addKeys = _props.addKeys;
134+
var _props2 = this.props;
135+
var value = _props2.value;
136+
var removeKeys = _props2.removeKeys;
137+
var addKeys = _props2.addKeys;
136138
var tag = this.state.tag;
137139

138140
var empty = tag === '';
@@ -196,18 +198,18 @@
196198
value: function render() {
197199
var _this = this;
198200

199-
var _props2 = this.props;
200-
var value = _props2.value;
201-
var onChange = _props2.onChange;
202-
var inputProps = _props2.inputProps;
203-
var tagProps = _props2.tagProps;
204-
var renderLayout = _props2.renderLayout;
205-
var renderTag = _props2.renderTag;
206-
var renderInput = _props2.renderInput;
207-
var addKeys = _props2.addKeys;
208-
var removeKeys = _props2.removeKeys;
201+
var _props3 = this.props;
202+
var value = _props3.value;
203+
var onChange = _props3.onChange;
204+
var inputProps = _props3.inputProps;
205+
var tagProps = _props3.tagProps;
206+
var renderLayout = _props3.renderLayout;
207+
var renderTag = _props3.renderTag;
208+
var renderInput = _props3.renderInput;
209+
var addKeys = _props3.addKeys;
210+
var removeKeys = _props3.removeKeys;
209211

210-
var other = _objectWithoutProperties(_props2, ['value', 'onChange', 'inputProps', 'tagProps', 'renderLayout', 'renderTag', 'renderInput', 'addKeys', 'removeKeys']);
212+
var other = _objectWithoutProperties(_props3, ['value', 'onChange', 'inputProps', 'tagProps', 'renderLayout', 'renderTag', 'renderInput', 'addKeys', 'removeKeys']);
211213

212214
var tag = this.state.tag;
213215

@@ -242,7 +244,8 @@
242244
renderLayout: _React['default'].PropTypes.func,
243245
tagProps: _React['default'].PropTypes.object,
244246
onlyUnique: _React['default'].PropTypes.bool,
245-
value: _React['default'].PropTypes.array.isRequired
247+
value: _React['default'].PropTypes.array.isRequired,
248+
maxTags: _React['default'].PropTypes.number
246249
},
247250
enumerable: true
248251
}, {
@@ -256,7 +259,8 @@
256259
renderTag: defaultRenderTag,
257260
renderLayout: defaultRenderLayout,
258261
tagProps: { className: 'react-tagsinput-tag', classNameRemove: 'react-tagsinput-remove' },
259-
onlyUnique: false
262+
onlyUnique: false,
263+
maxTags: 0
260264
},
261265
enumerable: true
262266
}]);

src/index.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ class TagsInput extends React.Component {
5757
renderLayout: React.PropTypes.func,
5858
tagProps: React.PropTypes.object,
5959
onlyUnique: React.PropTypes.bool,
60-
value: React.PropTypes.array.isRequired
60+
value: React.PropTypes.array.isRequired,
61+
maxTags: React.PropTypes.number
6162
}
6263

6364
static defaultProps = {
@@ -69,7 +70,8 @@ class TagsInput extends React.Component {
6970
renderTag: defaultRenderTag,
7071
renderLayout: defaultRenderLayout,
7172
tagProps: {className: 'react-tagsinput-tag', classNameRemove: 'react-tagsinput-remove'},
72-
onlyUnique: false
73+
onlyUnique: false,
74+
maxTags: -1
7375
}
7476

7577
_removeTag (index) {
@@ -84,11 +86,15 @@ class TagsInput extends React.Component {
8486
this.setState({tag: ''})
8587
}
8688

89+
_maxTags (tags) {
90+
return this.props.maxTags !== -1 ? tags < this.props.maxTags : true
91+
}
92+
8793
_addTag (tag) {
8894
let {onlyUnique} = this.props
8995
let isUnique = this.props.value.indexOf(tag) === -1
90-
91-
if (tag !== '' && (isUnique || !onlyUnique)) {
96+
let limit = this._maxTags(this.props.value.length)
97+
if (tag !== '' && limit && (isUnique || !onlyUnique)) {
9298
let value = this.props.value.concat([tag])
9399
this.props.onChange(value)
94100
this._clearInput()

test/index.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,14 @@ describe("TagsInput", () => {
205205
assert.equal(comp.len(), 0, "there should be no tags");
206206
});
207207

208+
it("should limit tags added", () => {
209+
let comp = TestUtils.renderIntoDocument(<TestComponent maxTags={0} />);
210+
let tag = randstring();
211+
add(comp, tag);
212+
add(comp, tag);
213+
assert.equal(comp.len(), 0, "there should be 0 tags");
214+
});
215+
208216
it("should add props to tag", () => {
209217
let comp = TestUtils.renderIntoDocument(<TestComponent tagProps={{className: "test"}} />);
210218
let tag = randstring();

0 commit comments

Comments
 (0)