@react-fabric/form 1.0.0-beta.6 → 1.0.0-beta.60

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.
Files changed (187) hide show
  1. package/{src/input/ArrayInput.module.css → css/_arrayInput.css} +8 -8
  2. package/css/_colorPicker.css +65 -0
  3. package/{src/internal/Field.module.css → css/_field.css} +66 -30
  4. package/{src/input/File.module.css → css/_file.css} +21 -6
  5. package/css/styles.css +27 -0
  6. package/dist/cjs/builder/FileField.js +40 -28
  7. package/dist/cjs/builder/FormField.js +50 -34
  8. package/dist/cjs/builder/SchemaEditor.js +72 -0
  9. package/dist/cjs/builder/components/AddSchemaFields.js +62 -0
  10. package/dist/cjs/builder/components/SchemaArrayForm.js +145 -0
  11. package/dist/cjs/builder/components/SchemaField.js +414 -0
  12. package/dist/cjs/form/Controller.js +46 -1
  13. package/dist/cjs/form/Form.js +70 -9
  14. package/dist/cjs/hooks/useFileUploader.js +16 -0
  15. package/dist/cjs/hooks/useFormBuilder.js +57 -15
  16. package/dist/cjs/i18n/ar.json.js +5 -4
  17. package/dist/cjs/i18n/en.json.js +5 -5
  18. package/dist/cjs/index.js +12 -2
  19. package/dist/cjs/input/ArrayInput.js +126 -36
  20. package/dist/cjs/input/AvatarInput.js +43 -23
  21. package/dist/cjs/input/Checkbox.js +16 -6
  22. package/dist/cjs/input/ColorInput.js +113 -58
  23. package/dist/cjs/input/ColorPicker.js +60 -76
  24. package/dist/cjs/input/DateInput.js +197 -0
  25. package/dist/cjs/input/Field.js +10 -4
  26. package/dist/cjs/input/File.js +14 -7
  27. package/dist/cjs/input/Hidden.js +13 -2
  28. package/dist/cjs/input/Input.js +24 -10
  29. package/dist/cjs/input/Number.js +22 -10
  30. package/dist/cjs/input/Password.js +22 -12
  31. package/dist/cjs/input/Radio.js +12 -3
  32. package/dist/cjs/input/RadioGroup.js +10 -1
  33. package/dist/cjs/input/{Range.js → RangeSlider.js} +16 -8
  34. package/dist/cjs/input/Search.js +30 -19
  35. package/dist/cjs/input/Slider.js +12 -6
  36. package/dist/cjs/input/Switch.js +20 -9
  37. package/dist/cjs/input/Textarea.js +29 -8
  38. package/dist/cjs/internal/ErrorIcon.js +1 -1
  39. package/dist/cjs/internal/FieldWrapper.js +11 -4
  40. package/dist/cjs/internal/InputWrapper.js +27 -20
  41. package/dist/cjs/select/AutoComplete.js +21 -11
  42. package/dist/cjs/select/DualList.js +34 -12
  43. package/dist/cjs/select/List.js +68 -22
  44. package/dist/cjs/select/Options.js +49 -42
  45. package/dist/cjs/select/Select.js +40 -10
  46. package/dist/cjs/select/useSelect.js +22 -11
  47. package/dist/cjs/types/schema.js +43 -0
  48. package/dist/cjs/utils/eventHandlers.js +15 -5
  49. package/dist/esm/builder/FileField.js +42 -30
  50. package/dist/esm/builder/FormField.js +50 -34
  51. package/dist/esm/builder/SchemaEditor.js +72 -0
  52. package/dist/esm/builder/components/AddSchemaFields.js +62 -0
  53. package/dist/esm/builder/components/SchemaArrayForm.js +145 -0
  54. package/dist/esm/builder/components/SchemaField.js +414 -0
  55. package/dist/esm/form/Controller.js +46 -1
  56. package/dist/esm/form/Form.js +72 -12
  57. package/dist/esm/hooks/useFileUploader.js +16 -0
  58. package/dist/esm/hooks/useFormBuilder.js +55 -13
  59. package/dist/esm/i18n/ar.json.js +5 -4
  60. package/dist/esm/i18n/en.json.js +5 -5
  61. package/dist/esm/index.js +6 -2
  62. package/dist/esm/input/ArrayInput.js +126 -36
  63. package/dist/esm/input/AvatarInput.js +44 -24
  64. package/dist/esm/input/Checkbox.js +16 -6
  65. package/dist/esm/input/ColorInput.js +115 -60
  66. package/dist/esm/input/ColorPicker.js +60 -76
  67. package/dist/esm/input/DateInput.js +197 -0
  68. package/dist/esm/input/Field.js +11 -5
  69. package/dist/esm/input/File.js +16 -9
  70. package/dist/esm/input/Hidden.js +13 -2
  71. package/dist/esm/input/Input.js +26 -12
  72. package/dist/esm/input/Number.js +24 -12
  73. package/dist/esm/input/Password.js +24 -14
  74. package/dist/esm/input/Radio.js +12 -3
  75. package/dist/esm/input/RadioGroup.js +11 -2
  76. package/dist/esm/input/{Range.js → RangeSlider.js} +16 -8
  77. package/dist/esm/input/Search.js +32 -21
  78. package/dist/esm/input/Slider.js +12 -6
  79. package/dist/esm/input/Switch.js +20 -9
  80. package/dist/esm/input/Textarea.js +30 -9
  81. package/dist/esm/internal/ErrorIcon.js +1 -1
  82. package/dist/esm/internal/FieldWrapper.js +11 -4
  83. package/dist/esm/internal/InputWrapper.js +27 -20
  84. package/dist/esm/select/AutoComplete.js +21 -11
  85. package/dist/esm/select/DualList.js +34 -12
  86. package/dist/esm/select/List.js +69 -23
  87. package/dist/esm/select/Options.js +49 -42
  88. package/dist/esm/select/Select.js +40 -10
  89. package/dist/esm/select/useSelect.js +23 -12
  90. package/{src/input/ColorPicker.module.css → dist/esm/types/schema.js} +18 -27
  91. package/dist/esm/utils/eventHandlers.js +15 -5
  92. package/dist/types/builder/FileField.d.ts +4 -2
  93. package/dist/types/builder/FormField.d.ts +3 -1
  94. package/dist/types/builder/SchemaEditor.d.ts +9 -0
  95. package/dist/types/builder/components/AddSchemaFields.d.ts +2 -0
  96. package/dist/types/builder/components/SchemaArrayForm.d.ts +2 -0
  97. package/dist/types/builder/components/SchemaField.d.ts +10 -0
  98. package/dist/types/form/Controller.d.ts +47 -2
  99. package/dist/types/form/Form.d.ts +48 -2
  100. package/dist/types/hooks/useFileUploader.d.ts +16 -0
  101. package/dist/types/hooks/useFormBuilder.d.ts +32 -1
  102. package/dist/types/index.d.ts +6 -3
  103. package/dist/types/input/ArrayInput.d.ts +69 -12
  104. package/dist/types/input/AvatarInput.d.ts +23 -3
  105. package/dist/types/input/Checkbox.d.ts +13 -4
  106. package/dist/types/input/ColorInput.d.ts +15 -2
  107. package/dist/types/input/ColorPicker.d.ts +11 -1
  108. package/dist/types/input/DateInput.d.ts +28 -0
  109. package/dist/types/input/Field.d.ts +5 -1
  110. package/dist/types/input/File.d.ts +9 -0
  111. package/dist/types/input/Hidden.d.ts +13 -1
  112. package/dist/types/input/Input.d.ts +9 -1
  113. package/dist/types/input/Number.d.ts +9 -1
  114. package/dist/types/input/Password.d.ts +7 -1
  115. package/dist/types/input/Radio.d.ts +9 -1
  116. package/dist/types/input/RadioGroup.d.ts +8 -1
  117. package/dist/types/input/RangeSlider.d.ts +67 -0
  118. package/dist/types/input/Search.d.ts +14 -1
  119. package/dist/types/input/Slider.d.ts +8 -2
  120. package/dist/types/input/Switch.d.ts +10 -2
  121. package/dist/types/input/Textarea.d.ts +15 -1
  122. package/dist/types/internal/FieldWrapper.d.ts +1 -1
  123. package/dist/types/internal/InputWrapper.d.ts +1 -1
  124. package/dist/types/ranger/index.d.ts +2 -2
  125. package/dist/types/select/AutoComplete.d.ts +8 -1
  126. package/dist/types/select/DualList.d.ts +17 -2
  127. package/dist/types/select/List.d.ts +9 -1
  128. package/dist/types/select/Options.d.ts +4 -3
  129. package/dist/types/select/Select.d.ts +9 -1
  130. package/dist/types/select/useSelect.d.ts +10 -6
  131. package/dist/types/types/index.d.ts +34 -5
  132. package/dist/types/types/schema.d.ts +78 -33
  133. package/dist/types/utils/eventHandlers.d.ts +2 -2
  134. package/package.json +22 -20
  135. package/src/builder/FileField.tsx +62 -30
  136. package/src/builder/FormField.tsx +52 -32
  137. package/src/builder/SchemaEditor.tsx +79 -0
  138. package/src/builder/components/AddSchemaFields.tsx +74 -0
  139. package/src/builder/components/SchemaArrayForm.tsx +161 -0
  140. package/src/builder/components/SchemaField.tsx +436 -0
  141. package/src/form/Controller.tsx +48 -3
  142. package/src/form/Form.tsx +80 -8
  143. package/src/hooks/useFileUploader.ts +16 -0
  144. package/src/hooks/useFormBuilder.tsx +58 -15
  145. package/src/i18n/ar.json +5 -4
  146. package/src/i18n/en.json +5 -5
  147. package/src/index.ts +12 -3
  148. package/src/input/ArrayInput.tsx +199 -64
  149. package/src/input/AvatarInput.tsx +67 -19
  150. package/src/input/Checkbox.tsx +16 -6
  151. package/src/input/ColorInput.tsx +131 -71
  152. package/src/input/ColorPicker.tsx +54 -72
  153. package/src/input/DateInput.tsx +260 -0
  154. package/src/input/Field.tsx +15 -11
  155. package/src/input/File.tsx +16 -14
  156. package/src/input/Hidden.tsx +17 -2
  157. package/src/input/Input.tsx +27 -13
  158. package/src/input/Number.tsx +22 -13
  159. package/src/input/Password.tsx +24 -16
  160. package/src/input/Radio.tsx +12 -3
  161. package/src/input/RadioGroup.tsx +12 -3
  162. package/src/input/{Range.tsx → RangeSlider.tsx} +17 -9
  163. package/src/input/Search.tsx +33 -22
  164. package/src/input/Slider.tsx +20 -11
  165. package/src/input/Switch.tsx +17 -6
  166. package/src/input/Textarea.tsx +34 -9
  167. package/src/internal/ErrorIcon.tsx +1 -1
  168. package/src/internal/FieldWrapper.tsx +8 -4
  169. package/src/internal/InputWrapper.tsx +49 -27
  170. package/src/select/AutoComplete.tsx +14 -3
  171. package/src/select/DualList.tsx +31 -16
  172. package/src/select/List.tsx +52 -8
  173. package/src/select/Options.tsx +50 -46
  174. package/src/select/Select.tsx +25 -5
  175. package/src/select/useSelect.tsx +26 -9
  176. package/src/types/index.ts +37 -6
  177. package/src/types/schema.ts +96 -41
  178. package/src/utils/eventHandlers.ts +14 -4
  179. package/dist/cjs/input/ArrayInput.module.css +0 -1
  180. package/dist/cjs/input/ColorPicker.module.css +0 -1
  181. package/dist/cjs/input/File.module.css +0 -1
  182. package/dist/cjs/internal/Field.module.css +0 -1
  183. package/dist/esm/input/ArrayInput.module.css +0 -1
  184. package/dist/esm/input/ColorPicker.module.css +0 -1
  185. package/dist/esm/input/File.module.css +0 -1
  186. package/dist/esm/internal/Field.module.css +0 -1
  187. 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 { Range } from "../input/Range";
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 SchemaType } from "../types/schema";
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
- defaultValue,
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 E = useMemo(() => {
65
- if (rest.datatype === "number" || rest.datatype === "decimal")
66
- return Number;
67
- if (rest.datatype === "range") return Range;
68
- if (rest.datatype === "boolean") return Switch;
69
- if (rest.datatype === "text") return Textarea;
70
- if (rest.datatype === "color") return ColorInput;
71
- if (rest.datatype === "string" && rest.options) return Select;
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 === "number")
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 === "decimal")
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 === "text") return { rows: 3 };
98
- if (rest.datatype === "color") return { showPicker: true };
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 === "string" && rest.options)
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 === "avatar")
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 === "file")
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 && !("options" in rest) ? (
158
+ return rest.multiple && !hasOptions ? (
139
159
  <ArrayInput
140
160
  name={fieldName}
141
- label={t(label, 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, label)}
155
- placeholder={t(label, 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,74 @@
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 {
25
+ Button,
26
+ Content,
27
+ Footer,
28
+ Modal,
29
+ type ModalProps,
30
+ } from "@react-fabric/core";
31
+ import { yup } from "@react-fabric/utilities";
32
+ import { useCallback } from "react";
33
+ import { Controller } from "../../form/Controller";
34
+ import { Form } from "../../form/Form";
35
+ import { ArrayInput } from "../../input/ArrayInput";
36
+ import { Input } from "../../input/Input";
37
+
38
+ const fieldSchema = yup.object({
39
+ fields: yup.array(yup.string().trim().required()).required().min(1),
40
+ });
41
+
42
+ export const AddSchemaFields = (props: ModalProps) => {
43
+ const handleSubmit = useCallback((model: KeyValue) => {
44
+ props.onClose(
45
+ model.fields
46
+ .map((f: string) => f.split(",").map((ff) => ff.trim()))
47
+ .flat(2),
48
+ );
49
+ }, []);
50
+
51
+ return (
52
+ <Modal {...props}>
53
+ <Form
54
+ schema={fieldSchema}
55
+ defaultValues={{ fields: [" "] }}
56
+ onSubmit={handleSubmit}
57
+ >
58
+ <Content>
59
+ <ArrayInput minItems={1} name="fields" onAdd={() => String("")}>
60
+ <Controller>
61
+ <Input autoFocus />
62
+ </Controller>
63
+ </ArrayInput>
64
+ <p className="text-sm text-muted">
65
+ Provide list of fields or comma-separated value
66
+ </p>
67
+ </Content>
68
+ <Footer flex justify="end" className="px-2 py-1">
69
+ <Button type="submit">Add</Button>
70
+ </Footer>
71
+ </Form>
72
+ </Modal>
73
+ );
74
+ };
@@ -0,0 +1,161 @@
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 {
28
+ Button,
29
+ Content,
30
+ CoreIcons,
31
+ Header,
32
+ useOverlayService,
33
+ } from "@react-fabric/core";
34
+ import { isArray } from "@react-fabric/utilities";
35
+ import { useCallback, useState } from "react";
36
+ import { useFieldArray, useFormContext } from "react-hook-form";
37
+ import { DATA_TYPES, type SchemaEditorProps } from "../../types/schema";
38
+ import { AddSchemaFields } from "./AddSchemaFields";
39
+ import { SchemaField } from "./SchemaField";
40
+
41
+ // Need to disable prop-types lint due to uknown lint error
42
+ /* eslint-disable react/prop-types */
43
+ export const SchemaArrayForm = ({
44
+ dynamic,
45
+ acceptableTypes,
46
+ optionLists,
47
+ }: Partial<SchemaEditorProps>) => {
48
+ const form = useFormContext();
49
+ const [defaultOpen, setDefaultOpen] = useState(true);
50
+ const { fields, append, remove, move } = useFieldArray({
51
+ name: "schema",
52
+ control: form.control,
53
+ });
54
+
55
+ const handleTypeChange = useCallback((index: number, val: KeyValue) => {
56
+ form.setValue(`schema.${index}`, val);
57
+ }, []);
58
+
59
+ const handleRemove = useCallback((index: number) => {
60
+ remove(index);
61
+ }, []);
62
+
63
+ const [AddModal, openAddfields] = useOverlayService(AddSchemaFields);
64
+ const handleAdd = useCallback(() => {
65
+ void openAddfields().then((fields) => {
66
+ if (isArray(fields)) {
67
+ const existing = form
68
+ .getValues()
69
+ .schema.map((field: KeyValue) => field.id);
70
+ fields.forEach((field) => {
71
+ if (!existing.includes(field)) {
72
+ append({
73
+ id: field,
74
+ datatype: DATA_TYPES.STRING,
75
+ label: "",
76
+ });
77
+ }
78
+ });
79
+ }
80
+ });
81
+ }, []);
82
+
83
+ const Wrapper = useCallback(
84
+ // eslint-disable-next-line @typescript-eslint/promise-function-async
85
+ ({ children }: { children: React.ReactNode }) => {
86
+ if (dynamic) {
87
+ // pass id list to dnd context
88
+ const idMap = fields.map((item) => item.id);
89
+ const handleDragEnd = (e: DragEndEvent) => {
90
+ if (e.over && e.active.id !== e.over.id) {
91
+ // find index of item and drop over
92
+ const active = idMap.indexOf(`${e.active.id}`);
93
+ const end = idMap.indexOf(`${e.over.id}`);
94
+ move(active, end);
95
+ }
96
+ };
97
+ return (
98
+ <DndContext
99
+ onDragEnd={handleDragEnd}
100
+ modifiers={[restrictToVerticalAxis]}
101
+ >
102
+ <SortableContext items={idMap}>{children}</SortableContext>
103
+ </DndContext>
104
+ );
105
+ }
106
+
107
+ return children;
108
+ },
109
+ [dynamic, fields],
110
+ );
111
+ // @ts-expect-error ignore
112
+ Wrapper.displayName = "SortableWrapper";
113
+
114
+ return (
115
+ <Wrapper>
116
+ <Header flex justify="end" className="gap-1 px-2">
117
+ {dynamic && (
118
+ <Button
119
+ size="sm"
120
+ variant="link"
121
+ icon={CoreIcons.insert}
122
+ aria-label="add field"
123
+ onClick={handleAdd}
124
+ />
125
+ )}
126
+ <Button
127
+ size="sm"
128
+ variant="link"
129
+ icon={CoreIcons.colapseAllVar}
130
+ aria-label="collapse all"
131
+ onClick={() => setDefaultOpen(false)}
132
+ />
133
+ <Button
134
+ size="sm"
135
+ variant="link"
136
+ icon={CoreIcons.expandAllVar}
137
+ aria-label="expand all"
138
+ onClick={() => setDefaultOpen(true)}
139
+ />
140
+ </Header>
141
+ <Content className="divide-y">
142
+ {fields.map((field, index) => (
143
+ <SchemaField
144
+ key={index}
145
+ defaultOpen={defaultOpen}
146
+ id={form.getValues().schema[index].id}
147
+ fieldId={field.id}
148
+ field={`schema.${index}`}
149
+ dynamic={dynamic}
150
+ optionLists={optionLists}
151
+ onRemove={() => handleRemove(index)}
152
+ getModel={() => form.getValues().schema[index]}
153
+ onTypeChange={(val: KeyValue) => handleTypeChange(index, val)}
154
+ acceptableTypes={acceptableTypes ?? Object.values(DATA_TYPES)}
155
+ />
156
+ ))}
157
+ </Content>
158
+ {AddModal}
159
+ </Wrapper>
160
+ );
161
+ };