@react-fabric/form 1.0.0-beta.6 → 1.0.0-beta.61
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/{src/input/ArrayInput.module.css → css/_arrayInput.css} +8 -8
- package/css/_colorPicker.css +65 -0
- package/{src/internal/Field.module.css → css/_field.css} +66 -30
- package/{src/input/File.module.css → css/_file.css} +21 -6
- package/css/styles.css +27 -0
- package/dist/cjs/builder/FileField.js +40 -28
- package/dist/cjs/builder/FormField.js +50 -34
- package/dist/cjs/builder/SchemaEditor.js +72 -0
- package/dist/cjs/builder/components/AddSchemaFields.js +63 -0
- package/dist/cjs/builder/components/SchemaArrayForm.js +145 -0
- package/dist/cjs/builder/components/SchemaField.js +414 -0
- package/dist/cjs/form/Controller.js +46 -1
- package/dist/cjs/form/Form.js +71 -13
- package/dist/cjs/hooks/useFileUploader.js +16 -0
- package/dist/cjs/hooks/useFormBuilder.js +67 -18
- package/dist/cjs/i18n/ar.json.js +5 -4
- package/dist/cjs/i18n/en.json.js +5 -5
- package/dist/cjs/index.js +12 -2
- package/dist/cjs/input/ArrayInput.js +128 -38
- package/dist/cjs/input/AvatarInput.js +43 -23
- package/dist/cjs/input/Checkbox.js +18 -8
- package/dist/cjs/input/ColorInput.js +114 -59
- package/dist/cjs/input/ColorPicker.js +60 -76
- package/dist/cjs/input/DateInput.js +197 -0
- package/dist/cjs/input/Field.js +10 -4
- package/dist/cjs/input/File.js +15 -8
- package/dist/cjs/input/Hidden.js +13 -2
- package/dist/cjs/input/Input.js +24 -10
- package/dist/cjs/input/Number.js +22 -10
- package/dist/cjs/input/Password.js +23 -13
- package/dist/cjs/input/Radio.js +14 -5
- package/dist/cjs/input/RadioGroup.js +10 -1
- package/dist/cjs/input/{Range.js → RangeSlider.js} +16 -8
- package/dist/cjs/input/Search.js +32 -22
- package/dist/cjs/input/Slider.js +12 -6
- package/dist/cjs/input/Switch.js +20 -9
- package/dist/cjs/input/Textarea.js +29 -8
- package/dist/cjs/internal/ErrorIcon.js +2 -2
- package/dist/cjs/internal/FieldWrapper.js +11 -4
- package/dist/cjs/internal/InputWrapper.js +29 -22
- package/dist/cjs/select/AutoComplete.js +22 -13
- package/dist/cjs/select/DualList.js +34 -12
- package/dist/cjs/select/List.js +49 -21
- package/dist/cjs/select/Options.js +49 -42
- package/dist/cjs/select/Select.js +21 -10
- package/dist/cjs/select/useSelect.js +22 -11
- package/dist/cjs/types/schema.js +43 -0
- package/dist/cjs/utils/eventHandlers.js +15 -5
- package/dist/esm/builder/FileField.js +42 -30
- package/dist/esm/builder/FormField.js +50 -34
- package/dist/esm/builder/SchemaEditor.js +72 -0
- package/dist/esm/builder/components/AddSchemaFields.js +63 -0
- package/dist/esm/builder/components/SchemaArrayForm.js +145 -0
- package/dist/esm/builder/components/SchemaField.js +414 -0
- package/dist/esm/form/Controller.js +46 -1
- package/dist/esm/form/Form.js +73 -16
- package/dist/esm/hooks/useFileUploader.js +16 -0
- package/dist/esm/hooks/useFormBuilder.js +65 -16
- package/dist/esm/i18n/ar.json.js +5 -4
- package/dist/esm/i18n/en.json.js +5 -5
- package/dist/esm/index.js +6 -2
- package/dist/esm/input/ArrayInput.js +128 -38
- package/dist/esm/input/AvatarInput.js +43 -23
- package/dist/esm/input/Checkbox.js +19 -9
- package/dist/esm/input/ColorInput.js +116 -61
- package/dist/esm/input/ColorPicker.js +60 -76
- package/dist/esm/input/DateInput.js +197 -0
- package/dist/esm/input/Field.js +11 -5
- package/dist/esm/input/File.js +18 -11
- package/dist/esm/input/Hidden.js +13 -2
- package/dist/esm/input/Input.js +26 -12
- package/dist/esm/input/Number.js +24 -12
- package/dist/esm/input/Password.js +26 -16
- package/dist/esm/input/Radio.js +15 -6
- package/dist/esm/input/RadioGroup.js +11 -2
- package/dist/esm/input/{Range.js → RangeSlider.js} +16 -8
- package/dist/esm/input/Search.js +35 -25
- package/dist/esm/input/Slider.js +12 -6
- package/dist/esm/input/Switch.js +20 -9
- package/dist/esm/input/Textarea.js +30 -9
- package/dist/esm/internal/ErrorIcon.js +3 -3
- package/dist/esm/internal/FieldWrapper.js +11 -4
- package/dist/esm/internal/InputWrapper.js +30 -23
- package/dist/esm/select/AutoComplete.js +23 -14
- package/dist/esm/select/DualList.js +35 -13
- package/dist/esm/select/List.js +50 -22
- package/dist/esm/select/Options.js +49 -42
- package/dist/esm/select/Select.js +22 -11
- package/dist/esm/select/useSelect.js +23 -12
- package/{src/input/ColorPicker.module.css → dist/esm/types/schema.js} +18 -27
- package/dist/esm/utils/eventHandlers.js +15 -5
- package/dist/types/builder/FileField.d.ts +4 -2
- package/dist/types/builder/FormField.d.ts +3 -1
- package/dist/types/builder/SchemaEditor.d.ts +9 -0
- package/dist/types/builder/components/AddSchemaFields.d.ts +2 -0
- package/dist/types/builder/components/SchemaArrayForm.d.ts +2 -0
- package/dist/types/builder/components/SchemaField.d.ts +10 -0
- package/dist/types/form/Controller.d.ts +47 -2
- package/dist/types/form/Form.d.ts +49 -6
- package/dist/types/hooks/useFileUploader.d.ts +16 -0
- package/dist/types/hooks/useFormBuilder.d.ts +38 -1
- package/dist/types/index.d.ts +6 -3
- package/dist/types/input/ArrayInput.d.ts +69 -12
- package/dist/types/input/AvatarInput.d.ts +23 -3
- package/dist/types/input/Checkbox.d.ts +13 -4
- package/dist/types/input/ColorInput.d.ts +15 -2
- package/dist/types/input/ColorPicker.d.ts +11 -1
- package/dist/types/input/DateInput.d.ts +28 -0
- package/dist/types/input/Field.d.ts +5 -1
- package/dist/types/input/File.d.ts +9 -0
- package/dist/types/input/Hidden.d.ts +13 -1
- package/dist/types/input/Input.d.ts +9 -1
- package/dist/types/input/Number.d.ts +9 -1
- package/dist/types/input/Password.d.ts +7 -1
- package/dist/types/input/Radio.d.ts +9 -1
- package/dist/types/input/RadioGroup.d.ts +8 -1
- package/dist/types/input/RangeSlider.d.ts +67 -0
- package/dist/types/input/Search.d.ts +14 -1
- package/dist/types/input/Slider.d.ts +8 -2
- package/dist/types/input/Switch.d.ts +10 -2
- package/dist/types/input/Textarea.d.ts +15 -1
- package/dist/types/internal/FieldWrapper.d.ts +1 -1
- package/dist/types/internal/InputWrapper.d.ts +1 -1
- package/dist/types/ranger/index.d.ts +2 -2
- package/dist/types/select/AutoComplete.d.ts +8 -1
- package/dist/types/select/DualList.d.ts +21 -7
- package/dist/types/select/List.d.ts +9 -1
- package/dist/types/select/Options.d.ts +4 -3
- package/dist/types/select/Select.d.ts +9 -1
- package/dist/types/select/useSelect.d.ts +10 -6
- package/dist/types/types/index.d.ts +38 -10
- package/dist/types/types/schema.d.ts +78 -33
- package/dist/types/utils/eventHandlers.d.ts +2 -2
- package/package.json +22 -20
- package/src/builder/FileField.tsx +56 -30
- package/src/builder/FormField.tsx +52 -32
- package/src/builder/SchemaEditor.tsx +79 -0
- package/src/builder/components/AddSchemaFields.tsx +75 -0
- package/src/builder/components/SchemaArrayForm.tsx +155 -0
- package/src/builder/components/SchemaField.tsx +435 -0
- package/src/form/Controller.tsx +48 -3
- package/src/form/Form.tsx +84 -13
- package/src/hooks/useFileUploader.ts +16 -0
- package/src/hooks/useFormBuilder.tsx +70 -19
- package/src/i18n/ar.json +5 -4
- package/src/i18n/en.json +5 -5
- package/src/index.ts +12 -3
- package/src/input/ArrayInput.tsx +195 -66
- package/src/input/AvatarInput.tsx +66 -18
- package/src/input/Checkbox.tsx +22 -9
- package/src/input/ColorInput.tsx +128 -72
- package/src/input/ColorPicker.tsx +54 -72
- package/src/input/DateInput.tsx +260 -0
- package/src/input/Field.tsx +15 -11
- package/src/input/File.tsx +18 -16
- package/src/input/Hidden.tsx +17 -2
- package/src/input/Input.tsx +27 -13
- package/src/input/Number.tsx +22 -13
- package/src/input/Password.tsx +28 -18
- package/src/input/Radio.tsx +15 -6
- package/src/input/RadioGroup.tsx +12 -3
- package/src/input/{Range.tsx → RangeSlider.tsx} +17 -9
- package/src/input/Search.tsx +36 -25
- package/src/input/Slider.tsx +20 -11
- package/src/input/Switch.tsx +17 -6
- package/src/input/Textarea.tsx +34 -9
- package/src/internal/ErrorIcon.tsx +3 -3
- package/src/internal/FieldWrapper.tsx +8 -4
- package/src/internal/InputWrapper.tsx +56 -30
- package/src/select/AutoComplete.tsx +16 -6
- package/src/select/DualList.tsx +40 -22
- package/src/select/List.tsx +52 -8
- package/src/select/Options.tsx +50 -46
- package/src/select/Select.tsx +29 -10
- package/src/select/useSelect.tsx +26 -9
- package/src/types/index.ts +42 -10
- package/src/types/schema.ts +96 -41
- package/src/utils/eventHandlers.ts +14 -4
- package/dist/cjs/input/ArrayInput.module.css +0 -1
- package/dist/cjs/input/ColorPicker.module.css +0 -1
- package/dist/cjs/input/File.module.css +0 -1
- package/dist/cjs/internal/Field.module.css +0 -1
- package/dist/esm/input/ArrayInput.module.css +0 -1
- package/dist/esm/input/ColorPicker.module.css +0 -1
- package/dist/esm/input/File.module.css +0 -1
- package/dist/esm/internal/Field.module.css +0 -1
- package/dist/types/input/Range.d.ts +0 -59
|
@@ -21,6 +21,7 @@
|
|
|
21
21
|
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
22
22
|
*/
|
|
23
23
|
|
|
24
|
+
import { isEmpty } from "@react-fabric/utilities";
|
|
24
25
|
import { useMemo } from "react";
|
|
25
26
|
import { useTranslation } from "react-i18next";
|
|
26
27
|
import { Controller } from "../form/Controller";
|
|
@@ -28,13 +29,15 @@ import { type UploadHandler } from "../hooks/useFileUploader";
|
|
|
28
29
|
import { ArrayInput } from "../input/ArrayInput";
|
|
29
30
|
import { AvatarInput } from "../input/AvatarInput";
|
|
30
31
|
import { ColorInput } from "../input/ColorInput";
|
|
32
|
+
import { DateInput } from "../input/DateInput";
|
|
31
33
|
import { Input } from "../input/Input";
|
|
32
34
|
import { Number } from "../input/Number";
|
|
33
|
-
import {
|
|
35
|
+
import { RangeSlider } from "../input/RangeSlider";
|
|
34
36
|
import { Switch } from "../input/Switch";
|
|
35
37
|
import { Textarea } from "../input/Textarea";
|
|
36
38
|
import { Select } from "../select/Select";
|
|
37
|
-
import { type
|
|
39
|
+
import { type BaseSelectProps } from "../types";
|
|
40
|
+
import { DATA_TYPES, type SchemaType } from "../types/schema";
|
|
38
41
|
import { FileField } from "./FileField";
|
|
39
42
|
|
|
40
43
|
export const FormField = ({
|
|
@@ -44,7 +47,7 @@ export const FormField = ({
|
|
|
44
47
|
inline,
|
|
45
48
|
prefix,
|
|
46
49
|
autoFocus,
|
|
47
|
-
|
|
50
|
+
optionLists,
|
|
48
51
|
fileUrl,
|
|
49
52
|
uploadHandler,
|
|
50
53
|
...rest
|
|
@@ -52,6 +55,7 @@ export const FormField = ({
|
|
|
52
55
|
inline?: boolean;
|
|
53
56
|
prefix?: string;
|
|
54
57
|
autoFocus?: boolean;
|
|
58
|
+
optionLists?: KeyValue<BaseSelectProps<any>>;
|
|
55
59
|
fileUrl?: (path: string) => string;
|
|
56
60
|
uploadHandler?: UploadHandler;
|
|
57
61
|
}) => {
|
|
@@ -61,32 +65,35 @@ export const FormField = ({
|
|
|
61
65
|
[id, prefix],
|
|
62
66
|
);
|
|
63
67
|
|
|
64
|
-
const
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
68
|
+
const hasOptions = useMemo(
|
|
69
|
+
() =>
|
|
70
|
+
rest.datatype === DATA_TYPES.STRING &&
|
|
71
|
+
((rest.optionList === "--CUSTOM--" && !isEmpty(rest.options)) ||
|
|
72
|
+
!isEmpty(optionLists?.[rest.optionList ?? ""])),
|
|
73
|
+
[rest],
|
|
74
|
+
);
|
|
75
|
+
|
|
76
|
+
const E = useMemo<AnyObject>(() => {
|
|
77
|
+
if (rest.datatype === DATA_TYPES.NUMBER) return Number;
|
|
78
|
+
if (rest.datatype === DATA_TYPES.RANGE) return RangeSlider;
|
|
79
|
+
if (rest.datatype === DATA_TYPES.BOOL) return Switch;
|
|
80
|
+
if (rest.datatype === DATA_TYPES.TEXT) return Textarea;
|
|
81
|
+
if (rest.datatype === DATA_TYPES.DATE) return DateInput;
|
|
82
|
+
if (rest.datatype === DATA_TYPES.DATETIME) return DateInput;
|
|
83
|
+
if (rest.datatype === DATA_TYPES.COLOR) return ColorInput;
|
|
84
|
+
if (hasOptions) return Select;
|
|
72
85
|
|
|
73
86
|
return Input;
|
|
74
|
-
}, [rest.datatype]);
|
|
87
|
+
}, [rest.datatype, hasOptions]);
|
|
75
88
|
|
|
76
89
|
const props = useMemo(() => {
|
|
77
|
-
if (rest.datatype ===
|
|
90
|
+
if (rest.datatype === DATA_TYPES.NUMBER)
|
|
78
91
|
return {
|
|
79
92
|
min: rest.min,
|
|
80
93
|
max: rest.max,
|
|
81
94
|
step: rest.step ?? 1,
|
|
82
95
|
};
|
|
83
|
-
if (rest.datatype ===
|
|
84
|
-
return {
|
|
85
|
-
min: rest.min,
|
|
86
|
-
max: rest.max,
|
|
87
|
-
step: rest.step ?? 0.1,
|
|
88
|
-
};
|
|
89
|
-
if (rest.datatype === "range")
|
|
96
|
+
if (rest.datatype === DATA_TYPES.RANGE)
|
|
90
97
|
return {
|
|
91
98
|
min: rest.min ?? 1,
|
|
92
99
|
max: rest.max ?? 99,
|
|
@@ -94,20 +101,33 @@ export const FormField = ({
|
|
|
94
101
|
showValue: true,
|
|
95
102
|
showLabels: true,
|
|
96
103
|
};
|
|
97
|
-
if (rest.datatype ===
|
|
98
|
-
|
|
104
|
+
if (rest.datatype === DATA_TYPES.DATE)
|
|
105
|
+
return {
|
|
106
|
+
min: rest.min,
|
|
107
|
+
max: rest.max,
|
|
108
|
+
type: "date",
|
|
109
|
+
};
|
|
110
|
+
if (rest.datatype === DATA_TYPES.DATETIME)
|
|
111
|
+
return {
|
|
112
|
+
min: rest.min,
|
|
113
|
+
max: rest.max,
|
|
114
|
+
type: "datetime",
|
|
115
|
+
};
|
|
116
|
+
if (rest.datatype === DATA_TYPES.TEXT) return { rows: 3 };
|
|
117
|
+
if (rest.datatype === DATA_TYPES.COLOR) return { showPicker: true };
|
|
99
118
|
|
|
100
|
-
if (rest.datatype ===
|
|
119
|
+
if (rest.datatype === DATA_TYPES.STRING && hasOptions)
|
|
101
120
|
return {
|
|
102
|
-
options: rest.options,
|
|
103
121
|
multiple: rest.multiple,
|
|
122
|
+
allowCreate: rest.allowCustom,
|
|
104
123
|
searchable: true,
|
|
124
|
+
...(optionLists?.[rest.optionList ?? ""] ?? { options: rest.options }),
|
|
105
125
|
};
|
|
106
126
|
|
|
107
127
|
return {};
|
|
108
|
-
}, [rest]);
|
|
128
|
+
}, [rest, hasOptions, optionLists]);
|
|
109
129
|
|
|
110
|
-
if (rest.datatype ===
|
|
130
|
+
if (rest.datatype === DATA_TYPES.AVATAR)
|
|
111
131
|
return (
|
|
112
132
|
uploadHandler && (
|
|
113
133
|
<Controller name={fieldName}>
|
|
@@ -120,7 +140,7 @@ export const FormField = ({
|
|
|
120
140
|
)
|
|
121
141
|
);
|
|
122
142
|
|
|
123
|
-
if (rest.datatype ===
|
|
143
|
+
if (rest.datatype === DATA_TYPES.FILE)
|
|
124
144
|
return (
|
|
125
145
|
uploadHandler && (
|
|
126
146
|
<Controller name={fieldName}>
|
|
@@ -135,12 +155,12 @@ export const FormField = ({
|
|
|
135
155
|
)
|
|
136
156
|
);
|
|
137
157
|
|
|
138
|
-
return rest.multiple && !
|
|
158
|
+
return rest.multiple && !hasOptions ? (
|
|
139
159
|
<ArrayInput
|
|
140
160
|
name={fieldName}
|
|
141
|
-
label={t(label
|
|
161
|
+
label={t(`fb.${id}`, label || id)}
|
|
142
162
|
required={required}
|
|
143
|
-
onAdd={() => ""}
|
|
163
|
+
onAdd={() => String("")}
|
|
144
164
|
>
|
|
145
165
|
<Controller>
|
|
146
166
|
<E {...props} />
|
|
@@ -151,8 +171,8 @@ export const FormField = ({
|
|
|
151
171
|
<E
|
|
152
172
|
{...props}
|
|
153
173
|
autoFocus={autoFocus}
|
|
154
|
-
label={t(label
|
|
155
|
-
placeholder={t(label
|
|
174
|
+
label={t(`fb.${id}`, label || id)}
|
|
175
|
+
placeholder={t(`fb.${id}`, label || id)}
|
|
156
176
|
required={required}
|
|
157
177
|
inline={inline}
|
|
158
178
|
/>
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* React Fabric
|
|
3
|
+
* @version: 1.0.0
|
|
4
|
+
*
|
|
5
|
+
*
|
|
6
|
+
* The MIT License (MIT)
|
|
7
|
+
* Copyright (c) 2025 Adarsh Pastakia
|
|
8
|
+
*
|
|
9
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software
|
|
10
|
+
* and associated documentation files (the "Software"), to deal in the Software without restriction,
|
|
11
|
+
* including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
12
|
+
* and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
|
|
13
|
+
* subject to the following conditions:
|
|
14
|
+
*
|
|
15
|
+
* The above copyright notice and this permission notice shall be included in all copies or substantial
|
|
16
|
+
* portions of the Software.
|
|
17
|
+
*
|
|
18
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
|
|
19
|
+
* TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
20
|
+
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
|
21
|
+
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
import { Footer } from "@react-fabric/core";
|
|
25
|
+
import { EMPTY_ARRAY } from "@react-fabric/utilities";
|
|
26
|
+
import { useCallback, useEffect, useRef } from "react";
|
|
27
|
+
import { Form, type FormRef } from "../form/Form";
|
|
28
|
+
import {
|
|
29
|
+
DATA_TYPES,
|
|
30
|
+
type FormSchema,
|
|
31
|
+
type SchemaEditorProps,
|
|
32
|
+
} from "../types/schema";
|
|
33
|
+
import { SchemaArrayForm } from "./components/SchemaArrayForm";
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* SchemaEditor component allows users to edit a schema definition.
|
|
37
|
+
* It provides a form interface to modify schema properties dynamically.
|
|
38
|
+
*
|
|
39
|
+
* @param {SchemaEditorProps} props - The properties for the SchemaEditor component.
|
|
40
|
+
* @returns {JSX.Element} The rendered SchemaEditor component.
|
|
41
|
+
*/
|
|
42
|
+
export const SchemaEditor = ({
|
|
43
|
+
schemaDef,
|
|
44
|
+
dynamic,
|
|
45
|
+
acceptableTypes,
|
|
46
|
+
optionLists = EMPTY_ARRAY,
|
|
47
|
+
children,
|
|
48
|
+
onSubmit,
|
|
49
|
+
onChange,
|
|
50
|
+
}: SchemaEditorProps) => {
|
|
51
|
+
const formRef = useRef<FormRef<{ schema: FormSchema }>>(null);
|
|
52
|
+
|
|
53
|
+
const handleChange = useCallback((model: { schema: FormSchema }) => {
|
|
54
|
+
onChange?.(model.schema);
|
|
55
|
+
}, []);
|
|
56
|
+
|
|
57
|
+
const handleSubmit = useCallback((model: { schema: FormSchema }) => {
|
|
58
|
+
onSubmit?.(model.schema);
|
|
59
|
+
}, []);
|
|
60
|
+
|
|
61
|
+
useEffect(() => {
|
|
62
|
+
formRef.current?.setValues({ schema: schemaDef });
|
|
63
|
+
}, [schemaDef]);
|
|
64
|
+
|
|
65
|
+
return (
|
|
66
|
+
<Form formRef={formRef} onChange={handleChange} onSubmit={handleSubmit}>
|
|
67
|
+
<SchemaArrayForm
|
|
68
|
+
dynamic={dynamic}
|
|
69
|
+
optionLists={optionLists}
|
|
70
|
+
acceptableTypes={acceptableTypes ?? Object.values(DATA_TYPES)}
|
|
71
|
+
/>
|
|
72
|
+
{children && (
|
|
73
|
+
<Footer flex className="border-t bg-base px-6 py-2">
|
|
74
|
+
{children}
|
|
75
|
+
</Footer>
|
|
76
|
+
)}
|
|
77
|
+
</Form>
|
|
78
|
+
);
|
|
79
|
+
};
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* React Fabric
|
|
3
|
+
* @version: 1.0.0
|
|
4
|
+
*
|
|
5
|
+
*
|
|
6
|
+
* The MIT License (MIT)
|
|
7
|
+
* Copyright (c) 2025 Adarsh Pastakia
|
|
8
|
+
*
|
|
9
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software
|
|
10
|
+
* and associated documentation files (the "Software"), to deal in the Software without restriction,
|
|
11
|
+
* including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
12
|
+
* and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
|
|
13
|
+
* subject to the following conditions:
|
|
14
|
+
*
|
|
15
|
+
* The above copyright notice and this permission notice shall be included in all copies or substantial
|
|
16
|
+
* portions of the Software.
|
|
17
|
+
*
|
|
18
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
|
|
19
|
+
* TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
20
|
+
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
|
21
|
+
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
import { yupResolver } from "@hookform/resolvers/yup";
|
|
25
|
+
import {
|
|
26
|
+
Button,
|
|
27
|
+
Content,
|
|
28
|
+
Footer,
|
|
29
|
+
Modal,
|
|
30
|
+
type ModalProps,
|
|
31
|
+
} from "@react-fabric/core";
|
|
32
|
+
import { yup } from "@react-fabric/utilities";
|
|
33
|
+
import { useCallback } from "react";
|
|
34
|
+
import { Controller } from "../../form/Controller";
|
|
35
|
+
import { Form } from "../../form/Form";
|
|
36
|
+
import { ArrayInput } from "../../input/ArrayInput";
|
|
37
|
+
import { Input } from "../../input/Input";
|
|
38
|
+
|
|
39
|
+
const fieldSchema = yup.object({
|
|
40
|
+
fields: yup.array(yup.string().trim().required()).required().min(1),
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
export const AddSchemaFields = (props: ModalProps) => {
|
|
44
|
+
const handleSubmit = useCallback((model: KeyValue) => {
|
|
45
|
+
props.onClose(
|
|
46
|
+
model.fields
|
|
47
|
+
.map((f: string) => f.split(",").map((ff) => ff.trim()))
|
|
48
|
+
.flat(2),
|
|
49
|
+
);
|
|
50
|
+
}, []);
|
|
51
|
+
|
|
52
|
+
return (
|
|
53
|
+
<Modal {...props}>
|
|
54
|
+
<Form
|
|
55
|
+
resolver={yupResolver(fieldSchema)}
|
|
56
|
+
defaultValues={{ fields: [" "] }}
|
|
57
|
+
onSubmit={handleSubmit}
|
|
58
|
+
>
|
|
59
|
+
<Content>
|
|
60
|
+
<ArrayInput minItems={1} name="fields" onAdd={() => String("")}>
|
|
61
|
+
<Controller>
|
|
62
|
+
<Input autoFocus />
|
|
63
|
+
</Controller>
|
|
64
|
+
</ArrayInput>
|
|
65
|
+
<p className="text-sm text-muted">
|
|
66
|
+
Provide list of fields or comma-separated value
|
|
67
|
+
</p>
|
|
68
|
+
</Content>
|
|
69
|
+
<Footer flex justify="end" className="px-2 py-1">
|
|
70
|
+
<Button type="submit">Add</Button>
|
|
71
|
+
</Footer>
|
|
72
|
+
</Form>
|
|
73
|
+
</Modal>
|
|
74
|
+
);
|
|
75
|
+
};
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* React Fabric
|
|
3
|
+
* @version: 1.0.0
|
|
4
|
+
*
|
|
5
|
+
*
|
|
6
|
+
* The MIT License (MIT)
|
|
7
|
+
* Copyright (c) 2025 Adarsh Pastakia
|
|
8
|
+
*
|
|
9
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software
|
|
10
|
+
* and associated documentation files (the "Software"), to deal in the Software without restriction,
|
|
11
|
+
* including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
12
|
+
* and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
|
|
13
|
+
* subject to the following conditions:
|
|
14
|
+
*
|
|
15
|
+
* The above copyright notice and this permission notice shall be included in all copies or substantial
|
|
16
|
+
* portions of the Software.
|
|
17
|
+
*
|
|
18
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
|
|
19
|
+
* TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
20
|
+
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
|
21
|
+
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
import { DndContext, type DragEndEvent } from "@dnd-kit/core";
|
|
25
|
+
import { restrictToVerticalAxis } from "@dnd-kit/modifiers";
|
|
26
|
+
import { SortableContext } from "@dnd-kit/sortable";
|
|
27
|
+
import { Button, Content, Header, useOverlayService } from "@react-fabric/core";
|
|
28
|
+
import { isArray } from "@react-fabric/utilities";
|
|
29
|
+
import { useCallback, useState } from "react";
|
|
30
|
+
import { useFieldArray, useFormContext } from "react-hook-form";
|
|
31
|
+
import { DATA_TYPES, type SchemaEditorProps } from "../../types/schema";
|
|
32
|
+
import { AddSchemaFields } from "./AddSchemaFields";
|
|
33
|
+
import { SchemaField } from "./SchemaField";
|
|
34
|
+
|
|
35
|
+
// Need to disable prop-types lint due to uknown lint error
|
|
36
|
+
/* eslint-disable react/prop-types */
|
|
37
|
+
export const SchemaArrayForm = ({
|
|
38
|
+
dynamic,
|
|
39
|
+
acceptableTypes,
|
|
40
|
+
optionLists,
|
|
41
|
+
}: Partial<SchemaEditorProps>) => {
|
|
42
|
+
const form = useFormContext();
|
|
43
|
+
const [defaultOpen, setDefaultOpen] = useState(true);
|
|
44
|
+
const { fields, append, remove, move } = useFieldArray({
|
|
45
|
+
name: "schema",
|
|
46
|
+
control: form.control,
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
const handleTypeChange = useCallback((index: number, val: KeyValue) => {
|
|
50
|
+
form.setValue(`schema.${index}`, val);
|
|
51
|
+
}, []);
|
|
52
|
+
|
|
53
|
+
const handleRemove = useCallback((index: number) => {
|
|
54
|
+
remove(index);
|
|
55
|
+
}, []);
|
|
56
|
+
|
|
57
|
+
const [AddModal, openAddfields] = useOverlayService(AddSchemaFields);
|
|
58
|
+
const handleAdd = useCallback(() => {
|
|
59
|
+
void openAddfields().then((fields) => {
|
|
60
|
+
if (isArray(fields)) {
|
|
61
|
+
const existing = form
|
|
62
|
+
.getValues()
|
|
63
|
+
.schema.map((field: KeyValue) => field.id);
|
|
64
|
+
fields.forEach((field) => {
|
|
65
|
+
if (!existing.includes(field)) {
|
|
66
|
+
append({
|
|
67
|
+
id: field,
|
|
68
|
+
datatype: DATA_TYPES.STRING,
|
|
69
|
+
label: "",
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
});
|
|
75
|
+
}, []);
|
|
76
|
+
|
|
77
|
+
const Wrapper = useCallback(
|
|
78
|
+
// eslint-disable-next-line @typescript-eslint/promise-function-async
|
|
79
|
+
({ children }: { children: React.ReactNode }) => {
|
|
80
|
+
if (dynamic) {
|
|
81
|
+
// pass id list to dnd context
|
|
82
|
+
const idMap = fields.map((item) => item.id);
|
|
83
|
+
const handleDragEnd = (e: DragEndEvent) => {
|
|
84
|
+
if (e.over && e.active.id !== e.over.id) {
|
|
85
|
+
// find index of item and drop over
|
|
86
|
+
const active = idMap.indexOf(`${e.active.id}`);
|
|
87
|
+
const end = idMap.indexOf(`${e.over.id}`);
|
|
88
|
+
move(active, end);
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
return (
|
|
92
|
+
<DndContext
|
|
93
|
+
onDragEnd={handleDragEnd}
|
|
94
|
+
modifiers={[restrictToVerticalAxis]}
|
|
95
|
+
>
|
|
96
|
+
<SortableContext items={idMap}>{children}</SortableContext>
|
|
97
|
+
</DndContext>
|
|
98
|
+
);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
return children;
|
|
102
|
+
},
|
|
103
|
+
[dynamic, fields],
|
|
104
|
+
);
|
|
105
|
+
// @ts-expect-error ignore
|
|
106
|
+
Wrapper.displayName = "SortableWrapper";
|
|
107
|
+
|
|
108
|
+
return (
|
|
109
|
+
<Wrapper>
|
|
110
|
+
<Header flex justify="end" className="gap-1 px-2">
|
|
111
|
+
{dynamic && (
|
|
112
|
+
<Button
|
|
113
|
+
size="sm"
|
|
114
|
+
variant="link"
|
|
115
|
+
icon="icon-[mdi--plus-circle-outline]"
|
|
116
|
+
aria-label="add field"
|
|
117
|
+
onClick={handleAdd}
|
|
118
|
+
/>
|
|
119
|
+
)}
|
|
120
|
+
<Button
|
|
121
|
+
size="sm"
|
|
122
|
+
variant="link"
|
|
123
|
+
icon="icon-[mdi--arrow-collapse-vertical]"
|
|
124
|
+
aria-label="collapse all"
|
|
125
|
+
onClick={() => setDefaultOpen(false)}
|
|
126
|
+
/>
|
|
127
|
+
<Button
|
|
128
|
+
size="sm"
|
|
129
|
+
variant="link"
|
|
130
|
+
icon="icon-[mdi--arrow-expand-vertical]"
|
|
131
|
+
aria-label="expand all"
|
|
132
|
+
onClick={() => setDefaultOpen(true)}
|
|
133
|
+
/>
|
|
134
|
+
</Header>
|
|
135
|
+
<Content className="divide-y">
|
|
136
|
+
{fields.map((field, index) => (
|
|
137
|
+
<SchemaField
|
|
138
|
+
key={index}
|
|
139
|
+
defaultOpen={defaultOpen}
|
|
140
|
+
id={form.getValues().schema[index].id}
|
|
141
|
+
fieldId={field.id}
|
|
142
|
+
field={`schema.${index}`}
|
|
143
|
+
dynamic={dynamic}
|
|
144
|
+
optionLists={optionLists}
|
|
145
|
+
onRemove={() => handleRemove(index)}
|
|
146
|
+
getModel={() => form.getValues().schema[index]}
|
|
147
|
+
onTypeChange={(val: KeyValue) => handleTypeChange(index, val)}
|
|
148
|
+
acceptableTypes={acceptableTypes ?? Object.values(DATA_TYPES)}
|
|
149
|
+
/>
|
|
150
|
+
))}
|
|
151
|
+
</Content>
|
|
152
|
+
{AddModal}
|
|
153
|
+
</Wrapper>
|
|
154
|
+
);
|
|
155
|
+
};
|