forked from lorenzopolidori/react-tagsinput
-
Notifications
You must be signed in to change notification settings - Fork 1
/
react-tagsinput.js
662 lines (561 loc) · 18 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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
(function (global, factory) {
if (typeof define === "function" && define.amd) {
define('ReactTagsInput', ['module', 'exports', 'react', 'prop-types'], factory);
} else if (typeof exports !== "undefined") {
factory(module, exports, require('react'), require('prop-types'));
} else {
var mod = {
exports: {}
};
factory(mod, mod.exports, global.React, global.propTypes);
global.ReactTagsInput = mod.exports;
}
})(this, function (module, exports, _react, _propTypes) {
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
var _react2 = _interopRequireDefault(_react);
var _propTypes2 = _interopRequireDefault(_propTypes);
function _interopRequireDefault(obj) {
return obj && obj.__esModule ? obj : {
default: obj
};
}
function _defineProperty(obj, key, value) {
if (key in obj) {
Object.defineProperty(obj, key, {
value: value,
enumerable: true,
configurable: true,
writable: true
});
} else {
obj[key] = value;
}
return obj;
}
function _classCallCheck(instance, Constructor) {
if (!(instance instanceof Constructor)) {
throw new TypeError("Cannot call a class as a function");
}
}
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;
};
}();
function _possibleConstructorReturn(self, call) {
if (!self) {
throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
}
return call && (typeof call === "object" || typeof call === "function") ? call : self;
}
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 _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;
};
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 uniq(arr) {
var out = [];
for (var i = 0; i < arr.length; i++) {
if (out.indexOf(arr[i]) === -1) {
out.push(arr[i]);
}
}
return out;
}
/* istanbul ignore next */
function getClipboardData(e) {
if (window.clipboardData) {
return window.clipboardData.getData('Text');
}
if (e.clipboardData) {
return e.clipboardData.getData('text/plain');
}
return '';
}
function defaultRenderTag(props) {
var tag = props.tag,
key = props.key,
disabled = props.disabled,
onRemove = props.onRemove,
classNameRemove = props.classNameRemove,
getTagDisplayValue = props.getTagDisplayValue,
other = _objectWithoutProperties(props, ['tag', 'key', 'disabled', 'onRemove', 'classNameRemove', 'getTagDisplayValue']);
return _react2.default.createElement(
'span',
_extends({ key: key }, other),
getTagDisplayValue(tag),
!disabled && _react2.default.createElement('a', { className: classNameRemove, onClick: function onClick(e) {
return onRemove(key);
} })
);
}
function defaultRenderInput(_ref) {
var addTag = _ref.addTag,
props = _objectWithoutProperties(_ref, ['addTag']);
var onChange = props.onChange,
value = props.value,
other = _objectWithoutProperties(props, ['onChange', 'value']);
return _react2.default.createElement('input', _extends({ type: 'text', onChange: onChange, value: value }, other));
}
function defaultRenderLayout(tagComponents, inputComponent) {
return _react2.default.createElement(
'span',
null,
tagComponents,
inputComponent
);
}
function defaultPasteSplit(data) {
return data.split(' ').map(function (d) {
return d.trim();
});
}
var defaultInputProps = {
className: 'react-tagsinput-input',
placeholder: 'Add a tag'
};
var TagsInput = function (_React$Component) {
_inherits(TagsInput, _React$Component);
/* istanbul ignore next */
function TagsInput() {
_classCallCheck(this, TagsInput);
var _this = _possibleConstructorReturn(this, (TagsInput.__proto__ || Object.getPrototypeOf(TagsInput)).call(this));
_this.state = { tag: '', isFocused: false };
_this.focus = _this.focus.bind(_this);
_this.blur = _this.blur.bind(_this);
return _this;
}
_createClass(TagsInput, [{
key: '_getTagDisplayValue',
value: function _getTagDisplayValue(tag) {
var tagDisplayProp = this.props.tagDisplayProp;
if (tagDisplayProp) {
return tag[tagDisplayProp];
}
return tag;
}
}, {
key: '_makeTag',
value: function _makeTag(tag) {
var tagDisplayProp = this.props.tagDisplayProp;
if (tagDisplayProp) {
return _defineProperty({}, tagDisplayProp, tag);
}
return tag;
}
}, {
key: '_removeTag',
value: function _removeTag(index) {
var value = this.props.value.concat([]);
if (index > -1 && index < value.length) {
var changed = value.splice(index, 1);
this.props.onChange(value, changed, [index]);
}
}
}, {
key: '_clearInput',
value: function _clearInput() {
if (this.hasControlledInput()) {
this.props.onChangeInput('');
} else {
this.setState({ tag: '' });
}
}
}, {
key: '_tag',
value: function _tag() {
if (this.hasControlledInput()) {
return this.props.inputValue;
}
return this.state.tag;
}
}, {
key: '_addTags',
value: function _addTags(tags) {
var _this2 = this;
var _props = this.props,
onChange = _props.onChange,
onValidationReject = _props.onValidationReject,
onlyUnique = _props.onlyUnique,
maxTags = _props.maxTags,
value = _props.value;
if (onlyUnique) {
tags = uniq(tags);
tags = tags.filter(function (tag) {
return value.every(function (currentTag) {
return _this2._getTagDisplayValue(currentTag) !== _this2._getTagDisplayValue(tag);
});
});
}
var rejectedTags = tags.filter(function (tag) {
return !_this2._validate(_this2._getTagDisplayValue(tag));
});
tags = tags.filter(function (tag) {
return _this2._validate(_this2._getTagDisplayValue(tag));
});
tags = tags.filter(function (tag) {
var tagDisplayValue = _this2._getTagDisplayValue(tag);
if (typeof tagDisplayValue.trim === 'function') {
return tagDisplayValue.trim().length > 0;
} else {
return tagDisplayValue;
}
});
if (maxTags >= 0) {
var remainingLimit = Math.max(maxTags - value.length, 0);
tags = tags.slice(0, remainingLimit);
}
if (onValidationReject && rejectedTags.length > 0) {
onValidationReject(rejectedTags);
}
if (tags.length > 0) {
var newValue = value.concat(tags);
var indexes = [];
for (var i = 0; i < tags.length; i++) {
indexes.push(value.length + i);
}
onChange(newValue, tags, indexes);
this._clearInput();
return true;
}
if (rejectedTags.length > 0) {
return false;
}
this._clearInput();
return false;
}
}, {
key: '_validate',
value: function _validate(tag) {
var _props2 = this.props,
validate = _props2.validate,
validationRegex = _props2.validationRegex;
return validate(tag) && validationRegex.test(tag);
}
}, {
key: '_shouldPreventDefaultEventOnAdd',
value: function _shouldPreventDefaultEventOnAdd(added, empty, keyCode) {
if (added) {
return true;
}
if (keyCode === 13) {
return this.props.preventSubmit || !this.props.preventSubmit && !empty;
}
return false;
}
}, {
key: 'focus',
value: function focus() {
if (this.input && typeof this.input.focus === 'function') {
this.input.focus();
}
this.handleOnFocus();
}
}, {
key: 'blur',
value: function blur() {
if (this.input && typeof this.input.blur === 'function') {
this.input.blur();
}
this.handleOnBlur();
}
}, {
key: 'accept',
value: function accept() {
var tag = this._tag();
if (tag !== '') {
tag = this._makeTag(tag);
return this._addTags([tag]);
}
return false;
}
}, {
key: 'addTag',
value: function addTag(tag) {
return this._addTags([tag]);
}
}, {
key: 'clearInput',
value: function clearInput() {
this._clearInput();
}
}, {
key: 'handlePaste',
value: function handlePaste(e) {
var _this3 = this;
var _props3 = this.props,
addOnPaste = _props3.addOnPaste,
pasteSplit = _props3.pasteSplit;
if (!addOnPaste) {
return;
}
e.preventDefault();
var data = getClipboardData(e);
var tags = pasteSplit(data).map(function (tag) {
return _this3._makeTag(tag);
});
this._addTags(tags);
}
}, {
key: 'handleKeyDown',
value: function handleKeyDown(e) {
if (e.defaultPrevented) {
return;
}
var _props4 = this.props,
value = _props4.value,
removeKeys = _props4.removeKeys,
addKeys = _props4.addKeys;
var tag = this._tag();
var empty = tag === '';
var keyCode = e.keyCode;
var key = e.key;
var add = addKeys.indexOf(keyCode) !== -1 || addKeys.indexOf(key) !== -1;
var remove = removeKeys.indexOf(keyCode) !== -1 || removeKeys.indexOf(key) !== -1;
if (add) {
var added = this.accept();
if (this._shouldPreventDefaultEventOnAdd(added, empty, keyCode)) {
e.preventDefault();
}
}
if (remove && value.length > 0 && empty) {
e.preventDefault();
this._removeTag(value.length - 1);
}
}
}, {
key: 'handleClick',
value: function handleClick(e) {
if (e.target === this.div) {
this.focus();
}
}
}, {
key: 'handleChange',
value: function handleChange(e) {
var onChangeInput = this.props.onChangeInput;
var onChange = this.props.inputProps.onChange;
var tag = e.target.value;
if (onChange) {
onChange(e);
}
if (this.hasControlledInput()) {
onChangeInput(tag);
} else {
this.setState({ tag: tag });
}
}
}, {
key: 'handleOnFocus',
value: function handleOnFocus(e) {
var onFocus = this.props.inputProps.onFocus;
if (onFocus) {
onFocus(e);
}
this.setState({ isFocused: true });
}
}, {
key: 'handleOnBlur',
value: function handleOnBlur(e) {
var onBlur = this.props.inputProps.onBlur;
this.setState({ isFocused: false });
if (e == null) {
return;
}
if (onBlur) {
onBlur(e);
}
if (this.props.addOnBlur) {
var tag = this._makeTag(e.target.value);
this._addTags([tag]);
}
}
}, {
key: 'handleRemove',
value: function handleRemove(tag) {
this._removeTag(tag);
}
}, {
key: 'inputProps',
value: function inputProps() {
var _props$inputProps = this.props.inputProps,
onChange = _props$inputProps.onChange,
onFocus = _props$inputProps.onFocus,
onBlur = _props$inputProps.onBlur,
otherInputProps = _objectWithoutProperties(_props$inputProps, ['onChange', 'onFocus', 'onBlur']);
var props = _extends({}, defaultInputProps, otherInputProps);
if (this.props.disabled) {
props.disabled = true;
}
return props;
}
}, {
key: 'inputValue',
value: function inputValue(props) {
return props.currentValue || props.inputValue || '';
}
}, {
key: 'hasControlledInput',
value: function hasControlledInput() {
var _props5 = this.props,
inputValue = _props5.inputValue,
onChangeInput = _props5.onChangeInput;
return typeof onChangeInput === 'function' && typeof inputValue === 'string';
}
}, {
key: 'componentDidMount',
value: function componentDidMount() {
if (this.hasControlledInput()) {
return;
}
this.setState({
tag: this.inputValue(this.props)
});
}
}, {
key: 'componentDidUpdate',
value: function componentDidUpdate(prevProps) {
/* istanbul ignore next */
if (this.hasControlledInput()) {
return;
}
if (!this.inputValue(this.props)) {
return;
}
if (this.inputValue(prevProps) !== this.inputValue(this.props)) {
this.setState({ tag: this.inputValue(this.props) });
}
}
}, {
key: 'render',
value: function render() {
var _this4 = this;
var _props6 = this.props,
value = _props6.value,
onChange = _props6.onChange,
tagProps = _props6.tagProps,
renderLayout = _props6.renderLayout,
renderTag = _props6.renderTag,
renderInput = _props6.renderInput,
addKeys = _props6.addKeys,
removeKeys = _props6.removeKeys,
className = _props6.className,
focusedClassName = _props6.focusedClassName,
addOnBlur = _props6.addOnBlur,
addOnPaste = _props6.addOnPaste,
inputProps = _props6.inputProps,
pasteSplit = _props6.pasteSplit,
onlyUnique = _props6.onlyUnique,
maxTags = _props6.maxTags,
validate = _props6.validate,
validationRegex = _props6.validationRegex,
disabled = _props6.disabled,
tagDisplayProp = _props6.tagDisplayProp,
inputValue = _props6.inputValue,
onChangeInput = _props6.onChangeInput,
other = _objectWithoutProperties(_props6, ['value', 'onChange', 'tagProps', 'renderLayout', 'renderTag', 'renderInput', 'addKeys', 'removeKeys', 'className', 'focusedClassName', 'addOnBlur', 'addOnPaste', 'inputProps', 'pasteSplit', 'onlyUnique', 'maxTags', 'validate', 'validationRegex', 'disabled', 'tagDisplayProp', 'inputValue', 'onChangeInput']);
var isFocused = this.state.isFocused;
if (isFocused) {
className += ' ' + focusedClassName;
}
var tagComponents = value.map(function (tag, index) {
return renderTag(_extends({
key: index,
tag: tag,
onRemove: _this4.handleRemove.bind(_this4),
disabled: disabled,
getTagDisplayValue: _this4._getTagDisplayValue.bind(_this4)
}, tagProps));
});
var inputComponent = renderInput(_extends({
ref: function ref(r) {
_this4.input = r;
},
value: this._tag(),
onPaste: this.handlePaste.bind(this),
onKeyDown: this.handleKeyDown.bind(this),
onChange: this.handleChange.bind(this),
onFocus: this.handleOnFocus.bind(this),
onBlur: this.handleOnBlur.bind(this),
addTag: this.addTag.bind(this)
}, this.inputProps()));
return _react2.default.createElement(
'div',
{ ref: function ref(r) {
_this4.div = r;
}, onClick: this.handleClick.bind(this), className: className },
renderLayout(tagComponents, inputComponent)
);
}
}]);
return TagsInput;
}(_react2.default.Component);
TagsInput.defaultProps = {
className: 'react-tagsinput',
focusedClassName: 'react-tagsinput--focused',
addKeys: [9, 13],
addOnBlur: false,
addOnPaste: false,
inputProps: {},
removeKeys: [8],
renderInput: defaultRenderInput,
renderTag: defaultRenderTag,
renderLayout: defaultRenderLayout,
pasteSplit: defaultPasteSplit,
tagProps: { className: 'react-tagsinput-tag', classNameRemove: 'react-tagsinput-remove' },
onlyUnique: false,
maxTags: -1,
validate: function validate() {
return true;
},
validationRegex: /.*/,
disabled: false,
tagDisplayProp: null,
preventSubmit: true
};
exports.default = TagsInput;
module.exports = exports['default'];
});