-
Notifications
You must be signed in to change notification settings - Fork 31
/
TagInput.jsx
325 lines (277 loc) · 7.8 KB
/
TagInput.jsx
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
/**
* @component
*/
import classNames from 'clsx';
import PropTypes from 'prop-types';
import React, { Component } from 'react';
import Input from '../../react-chayns-input/component/Input';
import getInputSize from '../utils/getInputSize';
import Tag from './Tag';
const KEY_BACKSPACE = 8;
const KEY_ENTER = 13;
const KEY_COMMA = 188;
const BIGGEST_LETTER = 'm';
/**
* A text input that allows values to be grouped as tags.
*/
export default class TagInput extends Component {
constructor(props) {
super(props);
this.setInputRef = this.setInputRef.bind(this);
this.handleKeyUp = this.handleKeyUp.bind(this);
this.handleTagRemove = this.handleTagRemove.bind(this);
this.handleChange = this.handleChange.bind(this);
this.handleBlur = this.handleBlur.bind(this);
this.handleClick = this.handleClick.bind(this);
this.state = {
selectedIndex: null,
};
}
componentDidUpdate(prevProps) {
const { value, triggerEventOnValueChange } = this.props;
if (triggerEventOnValueChange && value !== prevProps.value) {
this.handleChange(value);
}
}
handleChange(...value) {
const { onChange } = this.props;
this.setState({
selectedIndex: null,
});
if (onChange) {
onChange(...value);
}
}
handleKeyDown = (event) => {
const { onKeyDown } = this.props;
if (onKeyDown) {
onKeyDown(event);
}
if (event.keyCode === KEY_COMMA) {
event.preventDefault();
}
};
handleKeyUp(event) {
const { value, disableRemove } = this.props;
if (event.keyCode === KEY_BACKSPACE && !value && !disableRemove) {
this.handleRemoveLast();
return;
}
if (![KEY_ENTER, KEY_COMMA].includes(event.keyCode) || !value) {
return;
}
const tag = {
text: value,
};
this.handleTagAdd(tag);
}
handleTagAdd(tag) {
const { onAddTag } = this.props;
if (onAddTag) {
onAddTag(tag);
}
}
handleTagRemove(tag, ev) {
const { onRemoveTag } = this.props;
if (onRemoveTag) {
onRemoveTag(tag, ev);
}
}
handleRemoveLast() {
const { tags } = this.props;
const { selectedIndex } = this.state;
const lastTagIndex = tags.length - 1;
if (selectedIndex !== lastTagIndex) {
this.setState({
selectedIndex: lastTagIndex,
});
return;
}
this.setState({
selectedIndex: null,
});
this.handleTagRemove(tags[lastTagIndex]);
}
handleClick() {
if (this.input) {
this.input.focus();
}
}
handleBlur() {
const { value } = this.props;
if (value) {
const tag = {
text: value,
};
this.handleTagAdd(tag);
}
}
setInputRef(ref) {
this.input = ref;
}
focus() {
if (!this.input) {
return false;
}
this.input.focus();
return true;
}
render() {
const {
tags,
placeholder,
value,
className,
style,
disableRemove,
design,
max,
addTagOnBlur,
left,
...props
} = this.props;
const { selectedIndex } = this.state;
const { width } = getInputSize(
`${value || tags.length > 0 ? value : placeholder}${BIGGEST_LETTER}`
);
const inputStyle = {
width: `${width + 20}px`,
minWidth: '20px',
};
return (
<div
onClick={this.handleClick}
className={classNames(className, 'cc__tag-input input', {
'cc__tag-input--border-design':
design === Input.BORDER_DESIGN,
})}
style={style}
>
{left}
{tags &&
tags.map((tag, index) => (
<Tag
key={tag.id || index}
value={tag}
onDelete={this.handleTagRemove}
selected={selectedIndex === index}
disableRemove={disableRemove}
>
{tag.text}
</Tag>
))}
{max && tags && max === tags?.length ? (
false
) : (
<div className="cc__tag-input__input">
<Input
{...props}
inputRef={this.setInputRef}
value={value}
onChange={this.handleChange}
onKeyUp={this.handleKeyUp}
onKeyDown={this.handleKeyDown}
placeholder={
!tags || !tags.length ? placeholder : null
}
style={inputStyle}
onBlur={addTagOnBlur ? this.handleBlur : undefined}
/>
</div>
)}
</div>
);
}
}
TagInput.DEFAULT_DESIGN = 0;
TagInput.BORDER_DESIGN = 1;
TagInput.propTypes = {
/**
* An array of the current tags.
*/
tags: PropTypes.arrayOf(
PropTypes.shape({
text: PropTypes.oneOfType([PropTypes.string, PropTypes.node]),
})
),
/**
* The text value of the tag input.
*/
value: PropTypes.string,
/**
* A callback that is invoked when the user adds a new tag (hits the
* `enter`-key).
*/
onAddTag: PropTypes.func,
/**
* A callback that is invoked when the user removes a tag.
*/
onRemoveTag: PropTypes.func,
/**
* A callback that is invoked when the user changes the text inside the tag
* input.
*/
onChange: PropTypes.func,
/**
* A placeholder that is shown when the tag input is empty (does neither
* container a tag or text).
*/
placeholder: PropTypes.string,
/**
* A classname string that will be applied to the outer-most wrapper of the
* input.
*/
className: PropTypes.string,
/**
* A React style object that will be applied to the outer-most wrapper of
* the input.
*/
style: PropTypes.objectOf(
PropTypes.oneOfType([PropTypes.string, PropTypes.number])
),
triggerEventOnValueChange: PropTypes.bool,
/**
* Prevents removal of selected users and hides remove icon.
*/
disableRemove: PropTypes.bool,
/**
* The design of the input. Use either `TagInput.DEFAULT_DESIGN` or
* `TagInput.BORDER_DESIGN`.
*/
design: PropTypes.number,
/**
* The maximum number of tags selected at once.
*/
max: PropTypes.number,
/**
* Adds a tag on blur
*/
addTagOnBlur: PropTypes.bool,
/**
* A string or `ReactNode` that will be rendered on the left side of the
* tag input.
*/
left: PropTypes.node,
/**
* A callback for the `keydown`-event.
*/
onKeyDown: PropTypes.func,
};
TagInput.defaultProps = {
tags: null,
onAddTag: null,
onRemoveTag: null,
placeholder: null,
onChange: null,
value: '',
className: null,
style: null,
triggerEventOnValueChange: false,
disableRemove: false,
design: TagInput.DEFAULT_DESIGN,
max: null,
addTagOnBlur: false,
left: null,
onKeyDown: null,
};
TagInput.displayName = 'TagInput';