forked from rjsf-team/react-jsonschema-form
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ColorWidget.tsx
69 lines (64 loc) · 1.51 KB
/
ColorWidget.tsx
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
import React from "react";
import {
ColorPicker,
IColorPickerProps,
IColor,
getColorFromString,
Label,
} from "@fluentui/react";
import {
FormContextType,
RJSFSchema,
StrictRJSFSchema,
WidgetProps,
} from "@rjsf/utils";
import _pick from "lodash/pick";
const styles_red = {
// TODO: get this color from theme.
color: "rgb(164, 38, 44)",
fontSize: 12,
fontWeight: "normal" as any,
fontFamily: `"Segoe UI", "Segoe UI Web (West European)", "Segoe UI", -apple-system, BlinkMacSystemFont, Roboto, "Helvetica Neue", sans-serif;`,
};
const allowedProps: (keyof IColorPickerProps)[] = [
"componentRef",
"color",
"strings",
"onChange",
"alphaType",
"alphaSliderHidden",
"hexLabel",
"redLabel",
"greenLabel",
"blueLabel",
"alphaLabel",
"className",
"theme",
"styles",
"showPreview",
];
export default function ColorWidget<
T = any,
S extends StrictRJSFSchema = RJSFSchema,
F extends FormContextType = any
>({ schema, options, value, required, label, onChange }: WidgetProps<T, S, F>) {
const updateColor = (_ev: any, colorObj: IColor) => {
onChange(colorObj.hex);
};
const uiProps = _pick((options.props as object) || {}, allowedProps);
return (
<>
<Label>
{label || schema.title}
{required && <span style={styles_red}> *</span>}
</Label>
<ColorPicker
color={getColorFromString(value) as any}
onChange={updateColor}
alphaType={"alpha"}
showPreview={true}
{...uiProps}
/>
</>
);
}