forked from claus/react-dat-gui
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDatSelect.js
More file actions
89 lines (81 loc) · 2.19 KB
/
DatSelect.js
File metadata and controls
89 lines (81 loc) · 2.19 KB
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
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import isString from 'lodash.isstring';
import result from 'lodash.result';
import cx from 'classnames';
export default class DatSelect extends Component {
static propTypes = {
className: PropTypes.string,
style: PropTypes.object,
data: PropTypes.object.isRequired,
path: PropTypes.string,
label: PropTypes.string,
options: PropTypes.array.isRequired,
optionLabels: PropTypes.array,
labelWidth: PropTypes.string.isRequired,
liveUpdate: PropTypes.bool.isRequired,
onUpdate: PropTypes.func,
_onUpdateValue: PropTypes.func.isRequired
};
static defaultProps = {
className: null,
style: null,
path: null,
label: null,
optionLabels: null,
onUpdate: () => null
};
constructor() {
super();
this.state = {
value: null,
options: null
};
}
static getDerivedStateFromProps(nextProps) {
const nextValue = result(nextProps.data, nextProps.path);
return {
value: nextValue,
options: nextProps.options
};
}
handleChange = event => {
const { value } = event.target;
const { liveUpdate, _onUpdateValue, onUpdate, path } = this.props;
_onUpdateValue(path, value);
if (liveUpdate) onUpdate(value);
};
render() {
const {
path,
label,
labelWidth,
optionLabels,
className,
style
} = this.props;
const { value, options } = this.state;
const labelText = isString(label) ? label : path;
return (
<li className={cx('cr', 'select', className)} style={style}>
<label>
<span className="label-text" style={{ width: labelWidth }}>
{labelText}
</span>
<select
value={value}
onChange={this.handleChange}
style={{ width: `calc(100% - ${labelWidth})` }}
>
{options.map((item, index) => (
// eslint-disable-next-line react/no-array-index-key
<option key={index} value={item}>
{optionLabels ? optionLabels[index] : item}
</option>
))}
</select>
</label>
</li>
);
}
}