@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.
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 +63 -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 +71 -13
  14. package/dist/cjs/hooks/useFileUploader.js +16 -0
  15. package/dist/cjs/hooks/useFormBuilder.js +67 -18
  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 +128 -38
  20. package/dist/cjs/input/AvatarInput.js +43 -23
  21. package/dist/cjs/input/Checkbox.js +18 -8
  22. package/dist/cjs/input/ColorInput.js +114 -59
  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 +15 -8
  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 +23 -13
  31. package/dist/cjs/input/Radio.js +14 -5
  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 +32 -22
  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 +2 -2
  39. package/dist/cjs/internal/FieldWrapper.js +11 -4
  40. package/dist/cjs/internal/InputWrapper.js +29 -22
  41. package/dist/cjs/select/AutoComplete.js +22 -13
  42. package/dist/cjs/select/DualList.js +34 -12
  43. package/dist/cjs/select/List.js +49 -21
  44. package/dist/cjs/select/Options.js +49 -42
  45. package/dist/cjs/select/Select.js +21 -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 +63 -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 +73 -16
  57. package/dist/esm/hooks/useFileUploader.js +16 -0
  58. package/dist/esm/hooks/useFormBuilder.js +65 -16
  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 +128 -38
  63. package/dist/esm/input/AvatarInput.js +43 -23
  64. package/dist/esm/input/Checkbox.js +19 -9
  65. package/dist/esm/input/ColorInput.js +116 -61
  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 +18 -11
  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 +26 -16
  74. package/dist/esm/input/Radio.js +15 -6
  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 +35 -25
  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 +3 -3
  82. package/dist/esm/internal/FieldWrapper.js +11 -4
  83. package/dist/esm/internal/InputWrapper.js +30 -23
  84. package/dist/esm/select/AutoComplete.js +23 -14
  85. package/dist/esm/select/DualList.js +35 -13
  86. package/dist/esm/select/List.js +50 -22
  87. package/dist/esm/select/Options.js +49 -42
  88. package/dist/esm/select/Select.js +22 -11
  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 +49 -6
  100. package/dist/types/hooks/useFileUploader.d.ts +16 -0
  101. package/dist/types/hooks/useFormBuilder.d.ts +38 -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 +21 -7
  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 +38 -10
  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 +56 -30
  136. package/src/builder/FormField.tsx +52 -32
  137. package/src/builder/SchemaEditor.tsx +79 -0
  138. package/src/builder/components/AddSchemaFields.tsx +75 -0
  139. package/src/builder/components/SchemaArrayForm.tsx +155 -0
  140. package/src/builder/components/SchemaField.tsx +435 -0
  141. package/src/form/Controller.tsx +48 -3
  142. package/src/form/Form.tsx +84 -13
  143. package/src/hooks/useFileUploader.ts +16 -0
  144. package/src/hooks/useFormBuilder.tsx +70 -19
  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 +195 -66
  149. package/src/input/AvatarInput.tsx +66 -18
  150. package/src/input/Checkbox.tsx +22 -9
  151. package/src/input/ColorInput.tsx +128 -72
  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 +18 -16
  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 +28 -18
  160. package/src/input/Radio.tsx +15 -6
  161. package/src/input/RadioGroup.tsx +12 -3
  162. package/src/input/{Range.tsx → RangeSlider.tsx} +17 -9
  163. package/src/input/Search.tsx +36 -25
  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 +3 -3
  168. package/src/internal/FieldWrapper.tsx +8 -4
  169. package/src/internal/InputWrapper.tsx +56 -30
  170. package/src/select/AutoComplete.tsx +16 -6
  171. package/src/select/DualList.tsx +40 -22
  172. package/src/select/List.tsx +52 -8
  173. package/src/select/Options.tsx +50 -46
  174. package/src/select/Select.tsx +29 -10
  175. package/src/select/useSelect.tsx +26 -9
  176. package/src/types/index.ts +42 -10
  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
@@ -0,0 +1,435 @@
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 { useSortable } from "@dnd-kit/sortable";
25
+ import { CSS } from "@dnd-kit/utilities";
26
+ import {
27
+ Button,
28
+ Collapsable,
29
+ Content,
30
+ Footer,
31
+ Modal,
32
+ type ModalProps,
33
+ ToggleButtonGroup,
34
+ useOverlayService,
35
+ } from "@react-fabric/core";
36
+ import { EMPTY_ARRAY } from "@react-fabric/utilities";
37
+ import classNames from "classnames";
38
+ import {
39
+ Fragment,
40
+ type PropsWithChildren,
41
+ useCallback,
42
+ useEffect,
43
+ useState,
44
+ } from "react";
45
+ import { Controller } from "../../form/Controller";
46
+ import { ArrayInput } from "../../input/ArrayInput";
47
+ import { Checkbox } from "../../input/Checkbox";
48
+ import { DateInput } from "../../input/DateInput";
49
+ import { Field } from "../../input/Field";
50
+ import { HiddenInput } from "../../input/Hidden";
51
+ import { Input } from "../../input/Input";
52
+ import { Number } from "../../input/Number";
53
+ import { Select } from "../../select/Select";
54
+ import {
55
+ DATA_TYPES,
56
+ type DateSchema,
57
+ type DefaultSchema,
58
+ type NumberSchema,
59
+ SCHEMA_CUSTOM_LIST,
60
+ type SchemaEditorProps,
61
+ type StringSchema,
62
+ type ValueType,
63
+ } from "../../types/schema";
64
+
65
+ export const SchemaField = ({
66
+ id,
67
+ field,
68
+ fieldId,
69
+ getModel,
70
+ onRemove,
71
+ defaultOpen = true,
72
+ optionLists = EMPTY_ARRAY,
73
+ dynamic,
74
+ onTypeChange,
75
+ acceptableTypes,
76
+ }: {
77
+ id: string;
78
+ field: string;
79
+ fieldId: string;
80
+ defaultOpen?: boolean;
81
+ onRemove: () => void;
82
+ getModel: () => KeyValue;
83
+ onTypeChange: (val: KeyValue) => void;
84
+ } & Omit<SchemaEditorProps, "schemaDef" | "onChange">) => {
85
+ const [type, setType] = useState<DATA_TYPES>();
86
+ const [multiple, setMultiple] = useState(false);
87
+ const [hasOptions, setHasOptions] = useState(false);
88
+ const [customOptions, setCustomOptions] = useState(false);
89
+
90
+ const SortableItem = useCallback(
91
+ ({
92
+ id,
93
+ children,
94
+ className,
95
+ }: PropsWithChildren<{ id: AnyObject; className: string }>) => {
96
+ if (dynamic) {
97
+ const {
98
+ attributes,
99
+ listeners,
100
+ setNodeRef,
101
+ transform,
102
+ transition,
103
+ isDragging,
104
+ } = useSortable({ id });
105
+ return (
106
+ <div
107
+ className={className}
108
+ ref={setNodeRef}
109
+ style={{
110
+ transform: CSS.Transform.toString(transform),
111
+ transition,
112
+ }}
113
+ >
114
+ <div
115
+ className={classNames(
116
+ "invisible group-hover/field:visible my-2 w-4",
117
+ isDragging ? "cursor-grabbing" : "cursor-grab",
118
+ )}
119
+ {...attributes}
120
+ {...listeners}
121
+ style={{
122
+ backgroundImage: `url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' height='4' width='4'%3E%3Ccircle cx='2' cy='2' r='1' fill='%238888' /%3E%3C/svg%3E ")`,
123
+ }}
124
+ />
125
+ {children}
126
+ </div>
127
+ );
128
+ }
129
+
130
+ return <div className={className}>{children}</div>;
131
+ },
132
+ [dynamic],
133
+ );
134
+ // @ts-expect-error ignore
135
+ SortableItem.displayName = "SortableItem";
136
+
137
+ const [OptionModal, openOptionModal] = useOverlayService(
138
+ (props: ModalProps) => (
139
+ <Modal {...props}>
140
+ <Content>
141
+ <ArrayInput name={`${field}.options`} onAdd={() => String("")}>
142
+ <Controller>
143
+ <Input />
144
+ </Controller>
145
+ </ArrayInput>
146
+ </Content>
147
+ <Footer
148
+ flex
149
+ justify="end"
150
+ className="sticky bottom-0 border-t bg-base px-6 py-2"
151
+ >
152
+ <Button variant="link" onClick={props.onClose}>
153
+ Close
154
+ </Button>
155
+ </Footer>
156
+ </Modal>
157
+ ),
158
+ );
159
+
160
+ useEffect(() => {
161
+ setTimeout(() => {
162
+ const model = getModel();
163
+ setType(model.datatype);
164
+ setMultiple(model.multiple);
165
+ setHasOptions(!!model.optionList);
166
+ setCustomOptions(model.optionList === SCHEMA_CUSTOM_LIST);
167
+ }, 10);
168
+ }, []);
169
+
170
+ const handleTypeChange = useCallback(
171
+ (type: DATA_TYPES) => {
172
+ if (type) {
173
+ const modelRef = getModel();
174
+ let reset = {
175
+ datatype: type,
176
+ id,
177
+ label: modelRef.label,
178
+ defaultValue: modelRef.defaultValue,
179
+ required: modelRef.required,
180
+ multiple: type === DATA_TYPES.AVATAR ? false : modelRef.multiple,
181
+ minItems: modelRef.minItems,
182
+ maxItems: modelRef.maxItems,
183
+ } as DefaultSchema & ValueType;
184
+ if (type === DATA_TYPES.STRING) {
185
+ reset = Object.assign(reset, {
186
+ optionList: modelRef.optionList,
187
+ options: modelRef.options,
188
+ regex: modelRef.regex,
189
+ } as StringSchema);
190
+ } else if (type === DATA_TYPES.DATE) {
191
+ reset = Object.assign(reset, {
192
+ min: modelRef.min,
193
+ max: modelRef.max,
194
+ } as DateSchema);
195
+ } else if (type === DATA_TYPES.NUMBER || type === DATA_TYPES.RANGE) {
196
+ reset = Object.assign(reset, {
197
+ min: modelRef.min,
198
+ max: modelRef.max,
199
+ step: modelRef.step,
200
+ } as NumberSchema);
201
+ }
202
+ if (type === DATA_TYPES.AVATAR) {
203
+ setMultiple(false);
204
+ }
205
+ onTypeChange(reset);
206
+ setType(type);
207
+ }
208
+ },
209
+ [id],
210
+ );
211
+
212
+ return (
213
+ <SortableItem id={fieldId} className="flex flex-nowrap gap-1 group/field">
214
+ <Collapsable key={fieldId} open={defaultOpen} className="flex-1">
215
+ <div className="flex flex-nowrap items-center">
216
+ <span className="flex-1 truncate">{id}</span>
217
+ {dynamic && (
218
+ <Button
219
+ size="sm"
220
+ color="danger"
221
+ variant="link"
222
+ aria-label="Delete"
223
+ stopPropagation
224
+ icon="icon-[mdi--trash-can-outline]"
225
+ onClick={onRemove}
226
+ className="invisible group-hover/field:visible"
227
+ />
228
+ )}
229
+ </div>
230
+ <div className="flex flex-col gap-1 pb-4">
231
+ <Controller name={`${field}.id`}>
232
+ <HiddenInput hiddenValue={id} />
233
+ </Controller>
234
+ <Controller name={`${field}.datatype`}>
235
+ <Select
236
+ inline
237
+ label="Type"
238
+ placeholder="Data type"
239
+ options={acceptableTypes}
240
+ onChange={(val) => handleTypeChange(val as unknown as DATA_TYPES)}
241
+ />
242
+ </Controller>
243
+ <Controller name={`${field}.label`}>
244
+ <Input
245
+ inline
246
+ label="Label"
247
+ allowClear
248
+ placeholder="Label..."
249
+ decorateEndShowWhenEmpty
250
+ decorateEnd={
251
+ <span className="text-sm text-tint-300 px-1">OPTIONAL</span>
252
+ }
253
+ />
254
+ </Controller>
255
+ {type === DATA_TYPES.STRING && (
256
+ <Fragment>
257
+ <Controller name={`${field}.regex`}>
258
+ <Input
259
+ inline
260
+ label="Regex validation"
261
+ allowClear
262
+ placeholder="Regex value..."
263
+ decorateEndShowWhenEmpty
264
+ decorateEnd={
265
+ <span className="text-sm text-tint-300 px-1">OPTIONAL</span>
266
+ }
267
+ />
268
+ </Controller>
269
+ <Field inline label="Option list">
270
+ <Controller name={`${field}.optionList`}>
271
+ <Select
272
+ allowClear
273
+ options={[SCHEMA_CUSTOM_LIST, ...optionLists]}
274
+ onChange={(v) => setCustomOptions(v === SCHEMA_CUSTOM_LIST)}
275
+ />
276
+ </Controller>
277
+ {customOptions && (
278
+ <Button
279
+ icon="icon-[mdi--list-box-outline]"
280
+ aria-label="Custom options"
281
+ onClick={() => {
282
+ void openOptionModal({ title: `${id} Options` });
283
+ }}
284
+ />
285
+ )}
286
+ </Field>
287
+ {OptionModal}
288
+ </Fragment>
289
+ )}
290
+ {type === DATA_TYPES.FILE && (
291
+ <Fragment>
292
+ <Controller name={`${field}.accept`}>
293
+ <Input
294
+ inline
295
+ label="Accept types"
296
+ allowClear
297
+ placeholder="Accept value..."
298
+ decorateEndShowWhenEmpty
299
+ decorateEnd={
300
+ <span className="text-sm text-tint-300 px-1">OPTIONAL</span>
301
+ }
302
+ />
303
+ </Controller>
304
+ </Fragment>
305
+ )}
306
+ {[DATA_TYPES.NUMBER, DATA_TYPES.RANGE].includes(
307
+ type as unknown as DATA_TYPES,
308
+ ) && (
309
+ <Fragment>
310
+ <Controller name={`${field}.min`}>
311
+ <Number
312
+ inline
313
+ label="Min value"
314
+ allowClear
315
+ placeholder="Minimum value..."
316
+ decorateEndShowWhenEmpty
317
+ decorateEnd={
318
+ <span className="text-sm text-tint-300 px-1">OPTIONAL</span>
319
+ }
320
+ />
321
+ </Controller>
322
+ <Controller name={`${field}.max`}>
323
+ <Number
324
+ inline
325
+ label="Max value"
326
+ allowClear
327
+ placeholder="Maximum value..."
328
+ decorateEndShowWhenEmpty
329
+ decorateEnd={
330
+ <span className="text-sm text-tint-300 px-1">OPTIONAL</span>
331
+ }
332
+ />
333
+ </Controller>
334
+ <Controller name={`${field}.step`}>
335
+ <Number
336
+ inline
337
+ label="Step value"
338
+ allowClear
339
+ placeholder="Step value..."
340
+ decorateEndShowWhenEmpty
341
+ decorateEnd={
342
+ <span className="text-sm text-tint-300 px-1">OPTIONAL</span>
343
+ }
344
+ />
345
+ </Controller>
346
+ </Fragment>
347
+ )}
348
+ {type === DATA_TYPES.DATE && (
349
+ <Fragment>
350
+ <Field inline label="Type" plain>
351
+ <Controller name={`${field}.type`}>
352
+ <ToggleButtonGroup value="" fullWidth className="flex-1">
353
+ <Button className="flex-1" value="date">
354
+ Date
355
+ </Button>
356
+ <Button className="flex-1" value="datetime">
357
+ Date Time
358
+ </Button>
359
+ </ToggleButtonGroup>
360
+ </Controller>
361
+ </Field>
362
+ <Controller name={`${field}.min`}>
363
+ <DateInput
364
+ inline
365
+ label="Min value"
366
+ allowClear
367
+ placeholder="Minimum value..."
368
+ decorateEndShowWhenEmpty
369
+ decorateEnd={
370
+ <span className="text-sm text-tint-300 px-1">OPTIONAL</span>
371
+ }
372
+ />
373
+ </Controller>
374
+ <Controller name={`${field}.max`}>
375
+ <DateInput
376
+ inline
377
+ label="Max value"
378
+ allowClear
379
+ placeholder="Maximum value..."
380
+ decorateEndShowWhenEmpty
381
+ decorateEnd={
382
+ <span className="text-sm text-tint-300 px-1">OPTIONAL</span>
383
+ }
384
+ />
385
+ </Controller>
386
+ </Fragment>
387
+ )}
388
+ <Field label=" " inline plain>
389
+ <Controller name={`${field}.required`}>
390
+ <Checkbox label="Required" />
391
+ </Controller>
392
+ {type !== DATA_TYPES.AVATAR && (
393
+ <Controller name={`${field}.multiple`}>
394
+ <Checkbox label="Multiple" onChange={setMultiple} />
395
+ </Controller>
396
+ )}
397
+ {hasOptions && (
398
+ <Controller name={`${field}.allowCustom`}>
399
+ <Checkbox label="Allow custom value" />
400
+ </Controller>
401
+ )}
402
+ </Field>
403
+ {multiple && (
404
+ <Fragment>
405
+ <Controller name={`${field}.minItems`}>
406
+ <Number
407
+ inline
408
+ label="Min items"
409
+ allowClear
410
+ placeholder="Minimum items..."
411
+ decorateEndShowWhenEmpty
412
+ decorateEnd={
413
+ <span className="text-sm text-tint-300 px-1">OPTIONAL</span>
414
+ }
415
+ />
416
+ </Controller>
417
+ <Controller name={`${field}.maxItems`}>
418
+ <Number
419
+ inline
420
+ label="Max items"
421
+ allowClear
422
+ placeholder="Maximum items..."
423
+ decorateEndShowWhenEmpty
424
+ decorateEnd={
425
+ <span className="text-sm text-tint-300 px-1">OPTIONAL</span>
426
+ }
427
+ />
428
+ </Controller>
429
+ </Fragment>
430
+ )}
431
+ </div>
432
+ </Collapsable>
433
+ </SortableItem>
434
+ );
435
+ };
@@ -35,7 +35,7 @@ export interface ControllerProps {
35
35
  */
36
36
  name?: string;
37
37
  children:
38
- | ReactElement
38
+ | ReactElement<KeyValue>
39
39
  | ((props: {
40
40
  name: string;
41
41
  error?: string;
@@ -46,10 +46,55 @@ export interface ControllerProps {
46
46
  disabled?: boolean;
47
47
  onBlur: () => void;
48
48
  onChange: (value: AnyObject) => void;
49
- inputRef?: React.Ref<HTMLInputElement>;
49
+ ref?: React.Ref<HTMLInputElement>;
50
50
  }) => ReactElement);
51
51
  }
52
52
 
53
+ /**
54
+ * This component is used to wrap form controls
55
+ * and provide them with the necessary props and state management.
56
+ *
57
+ * It uses the `Controller` component from `react-hook-form`
58
+ * to manage the form state and validation.
59
+ *
60
+ * It accepts a `name` prop to identify the form control,
61
+ * and a `children` prop which can either be a React element
62
+ * or a function that receives the necessary props to render the control.
63
+ *
64
+ * The component will pass down the following props to the child:
65
+ * - `name`: the name of the form control
66
+ * - `error`: any validation error message
67
+ * - `checked`: whether the control is checked (for checkboxes/radios)
68
+ * - `value`: the current value of the control
69
+ * - `invalid`: whether the control is invalid
70
+ * - `readOnly`: whether the control is read-only (disabled during submission)
71
+ * - `disabled`: whether the control is disabled
72
+ * - `onBlur`: function to call when the control loses focus
73
+ * - `onChange`: function to call when the control value changes
74
+ * - `ref`: a ref to the control element
75
+ * * The component also handles the `ref` prop to ensure that it is passed correctly
76
+ * to the child component, allowing for proper focus management and DOM access.
77
+ *
78
+ * @param {ControllerProps} props - The properties for the Controller component.
79
+ * @returns {JSX.Element} The rendered Controller component.
80
+ *
81
+ * @example
82
+ * ```jsx
83
+ * <Controller
84
+ * name="username"
85
+ * children={({ name, error, checked, value, invalid, readOnly, onChange }) => (
86
+ * <input
87
+ * type="text"
88
+ * name={name}
89
+ * onChange={onChange}
90
+ * readOnly={readOnly}
91
+ * aria-invalid={invalid}
92
+ * value={value}
93
+ * />
94
+ * )}
95
+ * />
96
+ * ```
97
+ */
53
98
  export const Controller = ({ name, children, ...rest }: ControllerProps) => {
54
99
  if (!name) throw Error("Form Controller missing `name` prop");
55
100
  return (
@@ -86,7 +131,7 @@ export const Controller = ({ name, children, ...rest }: ControllerProps) => {
86
131
  : children({
87
132
  ...field,
88
133
  ...rest,
89
- inputRef: (el: AnyObject) => {
134
+ ref: (el: AnyObject) => {
90
135
  el && ref(el);
91
136
  },
92
137
  readOnly: isSubmitting,
package/src/form/Form.tsx CHANGED
@@ -21,9 +21,8 @@
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 { yupResolver } from "@hookform/resolvers/yup";
25
24
  import { useDebounce } from "@react-fabric/core";
26
- import { type yup } from "@react-fabric/utilities";
25
+ import { EMPTY_OBJECT } from "@react-fabric/utilities";
27
26
  import {
28
27
  useCallback,
29
28
  useEffect,
@@ -35,11 +34,21 @@ import {
35
34
  } from "react";
36
35
  import {
37
36
  FormProvider,
38
- useForm,
37
+ useFormContext,
38
+ useForm as useFormHook,
39
+ useWatch,
39
40
  type DefaultValues,
40
41
  type FormState,
42
+ type Path,
43
+ type Resolver,
41
44
  } from "react-hook-form";
42
45
 
46
+ type NestedKeyOf<T extends object> = {
47
+ [K in keyof T & (string | number)]: T[K] extends object
48
+ ? `${K}` | `${K}.${NestedKeyOf<T[K]>}`
49
+ : K;
50
+ }[keyof T & (string | number)];
51
+
43
52
  export interface FormRef<K extends KeyValue> {
44
53
  state: FormState<K>;
45
54
  reset: () => void;
@@ -47,16 +56,17 @@ export interface FormRef<K extends KeyValue> {
47
56
  submit: () => void;
48
57
  validate: () => Promise<boolean>;
49
58
  getValues: () => K;
50
- setValue: (key: keyof K, value: AnyObject) => void;
59
+ setValue: (key: NestedKeyOf<K>, value: AnyObject) => void;
51
60
  setValues: (values: K) => void;
52
61
  }
53
62
 
54
63
  export interface FormProps<K extends KeyValue = KeyValue> {
55
64
  formRef?: Ref<FormRef<K>>;
56
65
  /**
57
- * form data schema
66
+ * react-hook-form resolver for validation
67
+ * @see {@link https://react-hook-form.com/ts#Resolver}
58
68
  */
59
- schema?: yup.ObjectSchema<K>;
69
+ resolver?: Resolver<K>;
60
70
  /**
61
71
  * default data values
62
72
  */
@@ -73,24 +83,52 @@ export interface FormProps<K extends KeyValue = KeyValue> {
73
83
 
74
84
  const DEFAULT_SUBMIT = () => undefined;
75
85
 
86
+ /**
87
+ * Form component for managing form state and validation using react-hook-form and yup.
88
+ * It provides a simple interface for form handling, including submission, validation, and value management.
89
+ * It supports nested form structures and allows for custom validation schemas using Yup.
90
+ *
91
+ * @param {FormProps<K>} props - The properties for the Form component.
92
+ * @returns {JSX.Element} The rendered Form component.
93
+ *
94
+ * @example
95
+ * ```jsx
96
+ * <Form
97
+ * formRef={formRef}
98
+ * resolver={hookformResolver(schema)}
99
+ * defaultValues={{ name: "", email: "" }}
100
+ * onSubmit={(values) => {
101
+ * console.log("Form submitted with values:", values);
102
+ * }}
103
+ * >
104
+ * <input name="name" ref={form.register} placeholder="Name" />
105
+ * <input name="email" ref={form.register} placeholder="Email" />
106
+ * <button type="submit">Submit</button>
107
+ * </Form>
108
+ * ```
109
+ *
110
+ * @see {@link https://adarshpastakia.github.io/react-fabric/?path=/docs/form-form--docs} for the properties that can be passed to this component.
111
+ * @see {@link https://react-hook-form.com/docs/useform} for more details on the properties.
112
+ * @see {@link https://github.com/jquense/yup} for more details on the Yup validation schema.
113
+ */
76
114
  export const Form = <K extends KeyValue>({
77
115
  formRef,
78
- schema,
116
+ resolver,
79
117
  children,
80
- defaultValues,
118
+ defaultValues = EMPTY_OBJECT as K,
81
119
  onSubmit = DEFAULT_SUBMIT,
82
120
  onChange,
83
121
  ...rest
84
122
  }: PropsWithChildren<FormProps<K>>) => {
85
123
  const ref = useRef<HTMLFormElement>(null);
86
- const form = useForm<K>({
124
+ const form = useFormHook<K>({
87
125
  shouldFocusError: true,
88
- resolver: schema && (yupResolver(schema) as AnyObject),
126
+ resolver,
89
127
  defaultValues: defaultValues as DefaultValues<K>,
90
128
  });
91
129
 
92
130
  const [, startTransition] = useTransition();
93
- const changeHandler = useDebounce(onChange, [onChange]);
131
+ const changeHandler = useDebounce(onChange, [onChange], 300);
94
132
  useEffect(() => {
95
133
  const subscription = form.watch((value) => {
96
134
  startTransition(() => changeHandler?.(value as K));
@@ -116,7 +154,7 @@ export const Form = <K extends KeyValue>({
116
154
  reset: () => handleReset(defaultValues),
117
155
  clear: () => handleReset(),
118
156
  submit: () => form.handleSubmit(onSubmit),
119
- validate: async () => await form.trigger(),
157
+ validate: async (fields?: Array<Path<K>>) => await form.trigger(fields),
120
158
  getValues: () => form.getValues(),
121
159
  setValues: (v) => setTimeout(() => form.reset(v), 50),
122
160
  setValue: (k, v) =>
@@ -134,6 +172,7 @@ export const Form = <K extends KeyValue>({
134
172
 
135
173
  const onInvalid = useCallback((_: AnyObject, e: AnyObject) => {
136
174
  const el = e.target;
175
+ console.log("Invalid:", _, e);
137
176
  setTimeout(
138
177
  () =>
139
178
  el
@@ -150,7 +189,10 @@ export const Form = <K extends KeyValue>({
150
189
  <form
151
190
  ref={ref}
152
191
  onReset={() => handleReset(defaultValues)}
153
- onSubmit={form.handleSubmit(onSubmit, onInvalid) as AnyObject}
192
+ onSubmit={(e) => {
193
+ e.stopPropagation();
194
+ void form.handleSubmit(onSubmit, onInvalid)(e);
195
+ }}
154
196
  data-loading={form.formState.isSubmitting}
155
197
  className="contents"
156
198
  autoComplete="off"
@@ -162,3 +204,32 @@ export const Form = <K extends KeyValue>({
162
204
  </FormProvider>
163
205
  );
164
206
  };
207
+
208
+ export const useForm = <K extends KeyValue = KeyValue>() => {
209
+ const {
210
+ formState,
211
+ getValues,
212
+ setValue,
213
+ watch,
214
+ reset,
215
+ resetField,
216
+ setError,
217
+ setFocus,
218
+ } = useFormContext<K>();
219
+ const values = useWatch<K>();
220
+
221
+ return {
222
+ values: {
223
+ ...values,
224
+ ...getValues(),
225
+ },
226
+ formState,
227
+ getValues,
228
+ setValue,
229
+ watch,
230
+ reset,
231
+ resetField,
232
+ setError,
233
+ setFocus,
234
+ };
235
+ };
@@ -57,6 +57,22 @@ type UploaderActions =
57
57
  | { type: "remove"; key: string }
58
58
  | { type: "abort"; key: string };
59
59
 
60
+ /**
61
+ * useFileUploader hook provides a way to handle file uploads
62
+ * with progress tracking, error handling, and file management.
63
+ * It uses a reducer to manage the state of uploaded files and their statuses.
64
+ *
65
+ * @param uploader - The function that handles the file upload.
66
+ * @param initialList - Initial list of files to be managed by the uploader.
67
+ * @param multiple - Whether to allow multiple file uploads or not.
68
+ * @returns An object containing methods to upload, remove, retry files,
69
+ * and the current state of the uploader including pending uploads and file list.
70
+ *
71
+ * @example
72
+ * ```jsx
73
+ * const { upload, remove, retry, state } = useFileUploader(uploader, initialList, multiple);
74
+ * ```
75
+ */
60
76
  export const useFileUploader = (
61
77
  uploader: UploadHandler,
62
78
  initialList?: string | FileInfo | FileInfo[],