-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathDropdownInput.jsx
370 lines (315 loc) · 7.59 KB
/
DropdownInput.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
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
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import Popup from 'cspace-layout/lib/components/Popup';
import classNames from 'classnames';
import TextInput from './TextInput';
import styles from '../../styles/cspace-input/DropdownInput.css';
const propTypes = {
// TODO: Stop using propTypes in isInput, and in render method of cspace-ui Field component.
// Until then, propTypes need to be hoisted from the base component.
// eslint-disable-next-line react/forbid-foreign-prop-types
...TextInput.propTypes,
children: PropTypes.node,
className: PropTypes.string,
embedded: PropTypes.bool,
open: PropTypes.bool,
openOnFocus: PropTypes.bool,
openOnMouseDown: PropTypes.bool,
isOpenableWhenReadOnly: PropTypes.bool,
api: PropTypes.func,
/*
* A function that may be called to give focus to the contents of the popup. This is supplied as
* a prop because DropdownInput does not know the contents of the popup. Only the caller knows
* which element in the popup should receive focus.
*/
focusPopup: PropTypes.func,
onBlur: PropTypes.func,
onBeforeClose: PropTypes.func,
onClose: PropTypes.func,
onKeyDown: PropTypes.func,
onMount: PropTypes.func,
onOpen: PropTypes.func,
};
const defaultProps = {
children: undefined,
className: undefined,
embedded: undefined,
open: undefined,
openOnFocus: undefined,
openOnMouseDown: true,
isOpenableWhenReadOnly: undefined,
api: undefined,
focusPopup: undefined,
onBlur: undefined,
onBeforeClose: undefined,
onClose: undefined,
onKeyDown: undefined,
onMount: undefined,
onOpen: undefined,
};
export default class DropdownInput extends Component {
constructor(props) {
super(props);
this.handleInputBlur = this.handleInputBlur.bind(this);
this.handleInputFocus = this.handleInputFocus.bind(this);
this.handleInputMouseDown = this.handleInputMouseDown.bind(this);
this.handleInputKeyDown = this.handleInputKeyDown.bind(this);
this.handlePopupBlur = this.handlePopupBlur.bind(this);
this.handlePopupKeyDown = this.handlePopupKeyDown.bind(this);
this.handlePopupMount = this.handlePopupMount.bind(this);
this.handleRef = this.handleRef.bind(this);
this.state = {
open: props.open,
};
}
componentDidMount() {
const {
api,
onMount,
} = this.props;
if (api) {
api({
close: this.close.bind(this),
});
}
if (onMount) {
onMount({
focusInput: this.focusInput.bind(this),
});
}
}
// eslint-disable-next-line camelcase
UNSAFE_componentWillReceiveProps(nextProps) {
if (nextProps.open) {
this.open();
} else {
this.asyncClose();
}
}
componentDidUpdate(prevProps, prevState) {
const {
open: prevOpen,
} = prevState;
const {
open,
} = this.state;
if (prevOpen && !open) {
const {
onClose,
} = this.props;
if (onClose) {
onClose();
}
} else if (!prevOpen && open) {
const {
onOpen,
} = this.props;
if (onOpen) {
onOpen();
}
}
}
handleInputBlur(event) {
const {
onBlur,
} = this.props;
if (onBlur) {
onBlur(event);
}
if (!this.domNode.contains(event.relatedTarget)) {
this.asyncClose();
}
}
handleInputFocus() {
const {
openOnFocus,
} = this.props;
if (openOnFocus) {
this.open();
}
}
handleInputMouseDown() {
const {
openOnMouseDown,
} = this.props;
if (openOnMouseDown) {
this.open();
}
}
handleInputKeyDown(event) {
const {
onKeyDown,
} = this.props;
if (event.key === 'ArrowDown') {
const {
open,
} = this.state;
// Prevent the page from scrolling.
event.preventDefault();
if (open) {
this.focusPopup();
} else {
this.open();
this.focusPopupNeeded = true;
}
} else if (event.key === 'Escape') {
this.asyncClose(true);
}
if (onKeyDown) {
onKeyDown(event);
}
}
handlePopupBlur(event) {
if (!this.domNode.contains(event.relatedTarget)) {
this.asyncClose();
}
}
handlePopupKeyDown(event) {
if (event.key === 'Escape') {
this.asyncClose();
this.focusInput();
}
}
handlePopupMount() {
if (this.focusPopupNeeded) {
this.focusPopup();
this.focusPopupNeeded = false;
}
}
handleRef(ref) {
this.domNode = ref;
}
asyncClose(isCancelled) {
setTimeout(() => {
this.close(isCancelled);
}, 0);
}
close(isCancelled) {
const {
open,
} = this.state;
if (open) {
const {
onBeforeClose,
} = this.props;
if (onBeforeClose) {
onBeforeClose(isCancelled);
}
this.setState({
open: false,
});
}
}
focusInput() {
// FIXME: This breaks the TextInput component abstraction by selecting the rendered DOM node
// directly and calling focus() on it. But fixing this would require saving a ref to the
// TextInput; making TextInput a class instead of a function; and adding a focus() method to
// that class, which could be called here. LineInput and MultilineInput would also need to
// become classes instead of functions, each with their own focus() method, in order to
// properly implement TextInput.focus(). This seems like overkill at the moment.
this.domNode.querySelector('input, textarea').focus();
}
focusPopup() {
const {
focusPopup,
} = this.props;
if (focusPopup) {
focusPopup();
}
}
open() {
const {
open,
} = this.state;
if (!open) {
this.setState({
open: true,
});
}
}
renderInput() {
const {
children,
className,
focusPopup,
isOpenableWhenReadOnly,
openOnFocus,
openOnMouseDown,
onBeforeClose,
onClose,
onKeyDown,
onMount,
onOpen,
...remainingProps
} = this.props;
return (
<TextInput
// eslint-disable-next-line react/jsx-props-no-spreading
{...remainingProps}
onBlur={this.handleInputBlur}
onFocus={this.handleInputFocus}
onKeyDown={this.handleInputKeyDown}
onMouseDown={this.handleInputMouseDown}
/>
);
}
renderDropdown() {
const {
open,
} = this.state;
const {
children,
} = this.props;
if (open) {
return (
<Popup
onBlur={this.handlePopupBlur}
onKeyDown={this.handlePopupKeyDown}
onMount={this.handlePopupMount}
>
{children}
</Popup>
);
}
return null;
}
render() {
const {
open,
} = this.state;
const {
className,
embedded,
readOnly,
isOpenableWhenReadOnly,
} = this.props;
const classes = classNames(className, {
[styles.normal]: !embedded,
[styles.embedded]: embedded,
[styles.open]: open,
});
let readOnlyProps;
if (readOnly && isOpenableWhenReadOnly) {
readOnlyProps = {
tabIndex: 0,
onBlur: this.handleInputBlur,
onFocus: this.handleInputFocus,
onKeyDown: this.handleInputKeyDown,
onMouseDown: this.handleInputMouseDown,
};
}
return (
<div
ref={this.handleRef}
className={classes}
// eslint-disable-next-line react/jsx-props-no-spreading
{...readOnlyProps}
>
{this.renderInput()}
{this.renderDropdown()}
</div>
);
}
}
DropdownInput.propTypes = propTypes;
DropdownInput.defaultProps = defaultProps;