-
Notifications
You must be signed in to change notification settings - Fork 565
/
Copy pathindex.tsx
233 lines (191 loc) · 5.56 KB
/
index.tsx
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
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { get, identity, invoke, noop } from 'lodash';
const KEY_TAB = 9;
const KEY_ENTER = 13;
const KEY_RIGHT = 39;
const KEY_COMMA = 188;
const startsWith = prefix => text =>
text
.trim()
.toLowerCase()
.startsWith(prefix.trim().toLowerCase());
export class TagInput extends Component {
static displayName = 'TagInput';
static propTypes = {
inputRef: PropTypes.func,
onChange: PropTypes.func,
onSelect: PropTypes.func,
storeFocusInput: PropTypes.func,
storeHasFocus: PropTypes.func,
tagNames: PropTypes.arrayOf(PropTypes.string).isRequired,
value: PropTypes.string.isRequired,
};
static defaultProps = {
inputRef: identity,
onChange: identity,
onSelect: identity,
storeFocusInput: noop,
storeHasFocus: noop,
};
state = {
isComposing: false,
};
componentDidMount() {
this.props.storeFocusInput(this.focusInput);
this.props.storeHasFocus(this.hasFocus);
// Necessary for IE11 support, because contenteditable elements
// do not fire input or change events in IE11.
this.inputObserver = new MutationObserver(this.onInputMutation);
this.inputObserver.observe(this.inputField, {
characterData: true,
childList: true,
subtree: true,
});
}
componentWillUnmount() {
invoke(
this,
'inputField.removeEventListener',
'paste',
this.removePastedFormatting,
false
);
this.inputObserver.disconnect();
}
completeSuggestion = (andThen = identity) => {
const { onChange, tagNames, value } = this.props;
if (!value.length) {
return;
}
const suggestion = tagNames.find(startsWith(value));
if (suggestion) {
onChange(suggestion, () => {
andThen(suggestion);
this.focusInput();
});
}
};
focusInput = () => {
if (!this.inputField) {
return;
}
const input = this.inputField;
input.focus();
const range = document.createRange();
range.selectNodeContents(input);
range.collapse(false);
const selection = window.getSelection();
selection.removeAllRanges();
selection.addRange(range);
};
hasFocus = () => document.activeElement === this.inputField;
interceptKeys = event =>
invoke(
{
[KEY_ENTER]: this.submitTag,
[KEY_COMMA]: this.submitTag,
[KEY_TAB]: this.interceptTabPress,
[KEY_RIGHT]: this.interceptRightArrow,
},
event.keyCode,
event
);
interceptRightArrow = event => {
const { value } = this.props;
// if we aren't already at the right-most extreme
// then don't complete the suggestion; we could
// be moving the cursor around inside the input
const caretPosition = window.getSelection().getRangeAt(0).endOffset;
if (caretPosition !== value.length) {
return;
}
this.completeSuggestion();
event.preventDefault();
event.stopPropagation();
};
interceptTabPress = event => {
this.completeSuggestion(this.submitTag);
event.preventDefault();
event.stopPropagation();
};
onInputMutation = mutationList => {
mutationList.forEach(mutation => {
let value = get(mutation, 'target.data', '');
if (mutation.type === 'childList' && mutation.addedNodes.length) {
value = get(mutation, 'target.innerText', '');
}
this.onInput(value);
});
};
onInput = value => {
if (this.state.isComposing) {
return;
}
this.props.onChange(value.trim(), this.focusInput);
};
onCompositionEnd = e => {
const value = e.target.textContent;
this.setState({ isComposing: false }, () => this.onInput(value));
};
removePastedFormatting = event => {
let clipboardText;
if (get(event, 'clipboardData.getData')) {
clipboardText = event.clipboardData.getData('text/plain');
} else if (get(window, 'clipboardData.getData')) {
clipboardText = window.clipboardData.getData('Text'); // IE11
}
this.onInput(clipboardText);
event.preventDefault();
event.stopPropagation();
};
storeInput = ref => {
this.inputField = ref;
this.props.inputRef(ref);
invoke(
this,
'inputField.addEventListener',
'paste',
this.removePastedFormatting,
false
);
};
submitTag = event => {
const { onSelect, value } = this.props;
value.trim().length && onSelect(value.trim());
// safe invoke since event could be empty
invoke(event, 'preventDefault');
invoke(event, 'stopPropagation');
};
render() {
const { value, tagNames } = this.props;
const suggestion = value.length && tagNames.find(startsWith(value));
const shouldShowPlaceholder = value === '' && !this.state.isComposing;
return (
<div className="tag-input" onClick={this.focusInput}>
{shouldShowPlaceholder && (
<span aria-hidden className="tag-input__placeholder">
Add a tag…
</span>
)}
<div
aria-label="Add a tag…"
ref={this.storeInput}
className="tag-input__entry"
contentEditable="true"
onCompositionStart={() => this.setState({ isComposing: true })}
onCompositionEnd={this.onCompositionEnd}
onKeyDown={this.interceptKeys}
spellCheck={false}
suppressContentEditableWarning
>
{value}
</div>
<div className="tag-input__suggestion" disabled type="text">
{suggestion ? suggestion.substring(value.length) : ''}
</div>
</div>
);
}
}
export default TagInput;