-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathColor.jsx
276 lines (262 loc) · 7.21 KB
/
Color.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
import { Box, TextField } from "@mui/material";
import React, { useEffect, useState } from "react";
import Checkbox from "../Checkbox/Checkbox";
import PropTypes from "prop-types";
import { RgbaColorPicker } from "react-colorful";
import { colord } from "colord";
import { useDebouncyFn } from "use-debouncy";
// styling
const sx = {
colorPicker: {
"& .react-colorful": {
height: "240px",
width: "100%"
},
"& .react-colorful__alpha": {
borderRadius: "1 0 0 0"
},
"& .react-colorful__alpha-pointer": {
borderRadius: "4 4 0 0",
height: "15px",
width: "15px"
},
"& .react-colorful__hue-pointer": {
borderRadius: "4 4 0 0",
height: "15px",
width: "15px"
},
"& .react-colorful__last-control": {
borderRadius: "0 0 0 0"
},
"& .react-colorful__saturation": {
borderRadius: "0 0 0"
},
"& .react-colorful__saturation-pointer": {
borderRadius: "4 4 0 0",
height: "15px",
width: "15px"
}
}
};
// component to select a color
export default function Color({
onChange = () => {},
showControls = true,
showNoColor = true,
showPicker = true,
value = "rgba(255,0,0,1)"
}) {
// store last value to return back to it when toggling no color
const [lastColor, setLastColor] = useState(value);
useEffect(() => {
if (value.length !== 0) {
setLastColor(value);
}
}, [value]);
// convert rgba string to rgba object
const rgbaObj = colord(value).toRgb();
// check if no color is selected
const noColorChecked = value.length === 0;
// debounce color picker change
const handleColorPickerChange = useDebouncyFn(newRbgaObj => {
const newColor = colord(newRbgaObj).toRgbString();
setColor(newColor);
}, 200);
// handle color change
const setColor = newColor => {
onChange(newColor);
};
// handle no color button click
const handleNoColor = event => {
// return empty string if no value is checked otherwise return last value
const newColor = event.target.checked
? ""
: lastColor.length === 0
? "rgba(255,0,0,1)" // if there was no last value then return default
: lastColor;
setColor(newColor);
};
// handle red change
const handleRedChange = event => {
const newColor = colord({
...rgbaObj,
r: event.target.value.length === 0 ? 0 : event.target.value
}).toRgbString();
setColor(newColor);
};
// handle green change
const handleGreenChange = event => {
const newColor = colord({
...rgbaObj,
g: event.target.value.length === 0 ? 0 : event.target.value
}).toRgbString();
setColor(newColor);
};
// handle Blue change
const handleBlueChange = event => {
const newColor = colord({
...rgbaObj,
b: event.target.value.length === 0 ? 0 : event.target.value
}).toRgbString();
setColor(newColor);
};
// handle alpha change
const handleAlphaChange = event => {
const newColor = colord({
...rgbaObj,
a: event.target.value.length === 0 ? 0 : event.target.value
}).toRgbString();
setColor(newColor);
};
// define components
return (
<Box sx={{ display: "flex", flexDirection: "column" }}>
{showPicker && (
<Box sx={sx.colorPicker}>
{noColorChecked ? (
<Box
sx={{
border: "1px solid #bdbdbd",
borderRadius: "4px",
height: "238px"
}}
/>
) : (
<RgbaColorPicker
color={rgbaObj}
onChange={handleColorPickerChange}
id="colorPicker"
/>
)}
</Box>
)}
{showControls && (
<div>
{showNoColor && (
<Checkbox
checked={noColorChecked}
id="NoColorCheckbox"
label="No Color"
onChange={handleNoColor}
size="small"
/>
)}
<Box sx={{ display: "flex", flexDirection: "row" }}>
<TextField
data-testid="redTextField"
disabled={noColorChecked}
id="red"
type="number"
size="small"
margin="dense"
max="255"
label="Red"
error={rgbaObj.r > 255}
value={noColorChecked ? "" : rgbaObj.r}
sx={{
marginRight: theme => theme.spacing(1),
width: "33%"
}}
InputLabelProps={{
shrink: true
}}
onChange={handleRedChange}
inputProps={{ max: 255, min: 0 }}
/>
<TextField
data-testid="greenTextField"
disabled={noColorChecked}
id="green"
type="number"
size="small"
margin="dense"
max="255"
label="Green"
error={rgbaObj.g > 255}
value={noColorChecked ? "" : rgbaObj.g}
sx={{
marginRight: theme => theme.spacing(1),
width: "33%"
}}
InputLabelProps={{
shrink: true
}}
onChange={handleGreenChange}
inputProps={{ max: 255, min: 0 }}
/>
<TextField
data-testid="blueTextField"
disabled={noColorChecked}
id="blue"
type="number"
size="small"
margin="dense"
max="255"
label="Blue"
error={rgbaObj.b > 255}
value={noColorChecked ? "" : rgbaObj.b}
sx={{ width: "33%" }}
InputLabelProps={{
shrink: true
}}
onChange={handleBlueChange}
inputProps={{ max: 255, min: 0 }}
/>
</Box>
<TextField
data-testid="alphaTextField"
disabled={noColorChecked}
id="alpha"
type="number"
size="small"
margin="dense"
max="1"
label="Transparency"
error={rgbaObj.a > 1}
value={noColorChecked ? "" : rgbaObj.a}
fullWidth
InputLabelProps={{
shrink: true
}}
onChange={handleAlphaChange}
inputProps={{ max: 1, min: 0, step: 0.1 }}
/>
</div>
)}
</Box>
);
}
Color.propTypes = {
/**
* Callback fired when the value is changed.
*
* **Signature**
* ```
* function(color: string) => void
* ```
* color: This is the selected colour in a rgba string e.g "rgba(255,0,0,1)".
*/
onChange: PropTypes.func,
/**
* This determines if the rgba controls are shown.
* @default true
*/
showControls: PropTypes.bool,
/**
* This determines if the No Color control is shown.
* This is only shown if showControls is true.
* @default true
*/
showNoColor: PropTypes.bool,
/**
* This determines if the colorpicker is shown.
* @default true
*/
showPicker: PropTypes.bool,
/**
* This is the rgba color string that is used to
* set the inital value
* @default "rgba(255,0,0,1)"
*/
value: PropTypes.string
};