This repository was archived by the owner on May 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathDatBoolean.js
74 lines (63 loc) · 1.73 KB
/
DatBoolean.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
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 DatBoolean extends Component {
static propTypes = {
className: PropTypes.string,
style: PropTypes.object,
data: PropTypes.object.isRequired,
path: PropTypes.string,
label: PropTypes.string,
labelWidth: PropTypes.string.isRequired,
_onUpdateValue: PropTypes.func.isRequired
};
static defaultProps = {
className: null,
style: null,
path: null,
label: null
};
constructor(props) {
super(props);
this.state = {
value: null
};
}
static getDerivedStateFromProps(nextProps, prevState) {
const nextValue = result(nextProps.data, nextProps.path);
if (prevState.value === nextValue) return null;
return {
value: nextValue
};
}
handleChange = event => {
const value = event.target.checked;
const { _onUpdateValue, path } = this.props;
_onUpdateValue(path, value);
};
render() {
const { path, label, labelWidth, className, style } = this.props;
const labelText = isString(label) ? label : path;
return (
<li className={cx('cr', 'boolean', className)} style={style}>
<label>
<span className="label-text" style={{ width: labelWidth }}>
{labelText}
</span>
<span
className="checkbox-container"
style={{ width: `calc(100% - ${labelWidth})` }}
>
<input
type="checkbox"
checked={this.state.value}
onChange={this.handleChange}
/>
</span>
</label>
</li>
);
}
}