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