-
Notifications
You must be signed in to change notification settings - Fork 0
/
ColorPicker.js
73 lines (65 loc) · 1.91 KB
/
ColorPicker.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
import { Fragment } from 'react';
import { ErrorMessage, Field, useField } from 'formik';
function ColorPicker(props) {
const {
name,
label
} = props;
const colors = [
{
id: 1,
name: 'Red',
color: '#EF4444'
},
{
id: 2,
name: 'Green',
color: '#10B981'
},
{
id: 3,
name: 'Blue',
color: '#3B82F6'
}
];
// eslint-disable-next-line
const [field, meta, helpers] = useField(name);
const { value } = meta;
const { setValue } = helpers;
const ColorOptions = ({ field, form, ...props }) => {
if (! colors || ! colors.length) {
return null;
}
return colors.map(color => (
<Fragment key={ color.id }>
<div
key={ color.id }
className={
`w-12 h-12 rounded-full mr-3 border border-black border-opacity-20
${color.id === value ? 'ring-2 ring-offset-2 ring-gray-700' : ''}`
}
style={ { backgroundColor: color.color } }
onClick={ () => setValue(color.id) }
title={ color.name }
/>
<ErrorMessage name={name} component="div" className="mt-2 text-sm text-red-600" id={`${name}-error`} />
</Fragment>
));
}
return (
<>
<label htmlFor={ name } className="block text-sm font-medium text-gray-700">
{ label }
</label>
<div className="mt-4 flex">
<Field
name={ name }
id={ name }
type="number"
component={ ColorOptions }
/>
</div>
</>
);
}
export default ColorPicker;