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 pathDatColor.js
115 lines (101 loc) · 2.78 KB
/
DatColor.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
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
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import isString from 'lodash.isstring';
import result from 'lodash.result';
import cx from 'classnames';
import ColorPicker from './Picker';
export default class DatColor 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() {
super();
this.state = {
value: null,
displayColorPicker: false
};
}
static getDerivedStateFromProps(nextProps, prevState) {
const nextValue = result(nextProps.data, nextProps.path);
return {
...prevState,
value: nextValue
};
}
handleClickColorPicker = () =>
this.setState(prevState => ({
...prevState,
displayColorPicker: !prevState.displayColorPicker
}));
handleCloseColorPicker = () =>
this.setState({
displayColorPicker: false
});
handleChangeColor = color => {
const value = isString(color) ? color : color.hex;
const { _onUpdateValue, path } = this.props;
_onUpdateValue(path, value);
};
renderPicker() {
const { value, displayColorPicker } = this.state;
return !displayColorPicker ? null : (
<div className="popover">
{/* eslint-disable-next-line jsx-a11y/control-has-associated-label */}
<div
className="cover"
onClick={this.handleCloseColorPicker}
onKeyPress={this.handleCloseColorPicker}
role="button"
tabIndex={0}
/>
<ColorPicker color={value} onChange={this.handleChangeColor} />
</div>
);
}
render() {
const { path, label, labelWidth, className, style } = this.props;
const { value } = this.state;
const labelText = isString(label) ? label : path;
return (
<li
className={cx('cr', 'color', className)}
style={{ borderLeftColor: `${value}`, ...style }}
>
<label>
<span className="label-text" style={{ width: labelWidth }}>
{labelText}
</span>
<div
style={{
backgroundColor: value,
width: `calc(100% - ${labelWidth})`
}}
>
<div
className="swatch"
onClick={this.handleClickColorPicker}
onKeyPress={this.handleClickColorPicker}
role="button"
tabIndex={0}
>
{value}
</div>
{this.renderPicker()}
</div>
</label>
</li>
);
}
}