forked from olahol/react-tagsinput
-
Notifications
You must be signed in to change notification settings - Fork 0
/
react-tagsinput.js
281 lines (242 loc) · 10.4 KB
/
react-tagsinput.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
(function (global, factory) {
if (typeof define === 'function' && define.amd) {
define('ReactTagsInput', ['exports', 'module', 'react'], factory);
} else if (typeof exports !== 'undefined' && typeof module !== 'undefined') {
factory(exports, module, require('react'));
} else {
var mod = {
exports: {}
};
factory(mod.exports, mod, global.React);
global.ReactTagsInput = mod.exports;
}
})(this, function (exports, module, _react) {
'use strict';
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ('value' in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })();
var _get = function get(_x, _x2, _x3) { var _again = true; _function: while (_again) { var object = _x, property = _x2, receiver = _x3; _again = false; if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { _x = parent; _x2 = property; _x3 = receiver; _again = true; desc = parent = undefined; continue _function; } } else if ('value' in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } } };
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } }
function _inherits(subClass, superClass) { if (typeof superClass !== 'function' && superClass !== null) { throw new TypeError('Super expression must either be null or a function, not ' + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
var _React = _interopRequireDefault(_react);
function defaultRenderTag(props) {
var tag = props.tag;
var key = props.key;
var onRemove = props.onRemove;
var classNameRemove = props.classNameRemove;
var other = _objectWithoutProperties(props, ['tag', 'key', 'onRemove', 'classNameRemove']);
return _React['default'].createElement(
'span',
_extends({ key: key }, other),
tag,
_React['default'].createElement('a', { className: classNameRemove, onClick: function (e) {
return onRemove(key);
} })
);
}
defaultRenderTag.propTypes = {
key: _React['default'].PropTypes.number,
tag: _React['default'].PropTypes.string,
onRemove: _React['default'].PropTypes['function'],
classNameRemove: _React['default'].PropTypes.string
};
function defaultRenderInput(props) {
var onChange = props.onChange;
var value = props.value;
var other = _objectWithoutProperties(props, ['onChange', 'value']);
return _React['default'].createElement('input', _extends({ type: 'text', onChange: onChange, value: value }, other));
}
defaultRenderInput.propTypes = {
value: _React['default'].PropTypes.string,
onChange: _React['default'].PropTypes['function']
};
function defaultRenderLayout(tagComponents, inputComponent) {
return _React['default'].createElement(
'span',
null,
tagComponents,
inputComponent
);
}
var TagsInput = (function (_React$Component) {
_inherits(TagsInput, _React$Component);
function TagsInput() {
_classCallCheck(this, TagsInput);
_get(Object.getPrototypeOf(TagsInput.prototype), 'constructor', this).call(this);
this.state = { tag: '' };
this.focus = this.focus.bind(this);
this.blur = this.blur.bind(this);
}
_createClass(TagsInput, [{
key: '_removeTag',
value: function _removeTag(index) {
var value = this.props.value.concat([]);
if (index > -1 && index < value.length) {
value.splice(index, 1);
this.props.onChange(value);
}
}
}, {
key: '_clearInput',
value: function _clearInput() {
this.setState({ tag: '' });
}
}, {
key: '_maxTags',
value: function _maxTags(tags) {
return this.props.maxTags !== -1 ? tags < this.props.maxTags : true;
}
}, {
key: '_addTag',
value: function _addTag(tag) {
var onlyUnique = this.props.onlyUnique;
var isUnique = this.props.value.indexOf(tag) === -1;
var limit = this._maxTags(this.props.value.length);
if (tag !== '' && limit && (isUnique || !onlyUnique)) {
var value = this.props.value.concat([tag]);
this.props.onChange(value);
this._clearInput();
}
}
}, {
key: 'focus',
value: function focus() {
this.refs.input.focus();
}
}, {
key: 'blur',
value: function blur() {
this.refs.input.focus();
}
}, {
key: 'handleKeyDown',
value: function handleKeyDown(e) {
var _props = this.props;
var value = _props.value;
var removeKeys = _props.removeKeys;
var addKeys = _props.addKeys;
var validationRegex = _props.validationRegex;
var tag = this.state.tag;
var empty = tag === '';
var add = addKeys.indexOf(e.keyCode) !== -1;
var remove = removeKeys.indexOf(e.keyCode) !== -1;
if (add) {
e.preventDefault();
if (validationRegex.test(tag)) {
this._addTag(tag);
}
}
if (remove && value.length > 0 && empty) {
e.preventDefault();
this._removeTag(value.length - 1);
}
}
}, {
key: 'handleClick',
value: function handleClick(e) {
if (e.target === this.refs.div) {
this.focus();
}
}
}, {
key: 'handleChange',
value: function handleChange(e) {
var onChange = this.props.inputProps.onChange;
var tag = e.target.value;
if (onChange) {
onChange(e);
}
this.setState({ tag: tag });
}
}, {
key: 'handleOnBlur',
value: function handleOnBlur(e) {
if (this.props.addOnBlur) {
this._addTag(e.target.value);
}
}
}, {
key: 'handleRemove',
value: function handleRemove(tag) {
this._removeTag(tag);
}
}, {
key: 'inputProps',
value: function inputProps() {
var _props$inputProps = this.props.inputProps;
var onChange = _props$inputProps.onChange;
var otherInputProps = _objectWithoutProperties(_props$inputProps, ['onChange']);
return otherInputProps;
}
}, {
key: 'render',
value: function render() {
var _this = this;
var _props2 = this.props;
var value = _props2.value;
var onChange = _props2.onChange;
var inputProps = _props2.inputProps;
var tagProps = _props2.tagProps;
var renderLayout = _props2.renderLayout;
var renderTag = _props2.renderTag;
var renderInput = _props2.renderInput;
var addKeys = _props2.addKeys;
var removeKeys = _props2.removeKeys;
var other = _objectWithoutProperties(_props2, ['value', 'onChange', 'inputProps', 'tagProps', 'renderLayout', 'renderTag', 'renderInput', 'addKeys', 'removeKeys']);
var tag = this.state.tag;
var tagComponents = value.map(function (tag, index) {
return renderTag(_extends({ key: index, tag: tag, onRemove: _this.handleRemove.bind(_this) }, tagProps));
});
var inputComponent = renderInput(_extends({
ref: 'input',
value: tag,
onKeyDown: this.handleKeyDown.bind(this),
onChange: this.handleChange.bind(this),
onBlur: this.handleOnBlur.bind(this)
}, this.inputProps()));
return _React['default'].createElement(
'div',
_extends({ ref: 'div', onClick: this.handleClick.bind(this) }, other),
renderLayout(tagComponents, inputComponent)
);
}
}], [{
key: 'propTypes',
value: {
addKeys: _React['default'].PropTypes.array,
addOnBlur: _React['default'].PropTypes.bool,
inputProps: _React['default'].PropTypes.object,
onChange: _React['default'].PropTypes.func.isRequired,
removeKeys: _React['default'].PropTypes.array,
renderInput: _React['default'].PropTypes.func,
renderTag: _React['default'].PropTypes.func,
renderLayout: _React['default'].PropTypes.func,
tagProps: _React['default'].PropTypes.object,
onlyUnique: _React['default'].PropTypes.bool,
value: _React['default'].PropTypes.array.isRequired,
maxTags: _React['default'].PropTypes.number,
validationRegex: _React['default'].PropTypes.instanceOf(RegExp)
},
enumerable: true
}, {
key: 'defaultProps',
value: {
className: 'react-tagsinput',
addKeys: [9, 13],
inputProps: { className: 'react-tagsinput-input' },
removeKeys: [8],
renderInput: defaultRenderInput,
renderTag: defaultRenderTag,
renderLayout: defaultRenderLayout,
tagProps: { className: 'react-tagsinput-tag', classNameRemove: 'react-tagsinput-remove' },
onlyUnique: false,
maxTags: -1,
validationRegex: /.*/
},
enumerable: true
}]);
return TagsInput;
})(_React['default'].Component);
module.exports = TagsInput;
});