@react-fabric/form 1.0.0-beta.32 → 1.0.0-beta.33

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 (61) hide show
  1. package/dist/cjs/builder/FileField.js +4 -4
  2. package/dist/cjs/builder/FormField.js +41 -31
  3. package/dist/cjs/builder/SchemaEditor.js +428 -0
  4. package/dist/cjs/form/Controller.js +1 -1
  5. package/dist/cjs/form/Form.js +3 -4
  6. package/dist/cjs/hooks/useFormBuilder.js +27 -15
  7. package/dist/cjs/index.js +8 -2
  8. package/dist/cjs/input/Checkbox.js +4 -3
  9. package/dist/cjs/input/DateInput.js +183 -0
  10. package/dist/cjs/input/Field.js +1 -1
  11. package/dist/cjs/input/{Range.js → RangeSlider.js} +2 -2
  12. package/dist/cjs/input/Switch.js +4 -3
  13. package/dist/cjs/internal/Field.module.css +1 -1
  14. package/dist/cjs/internal/InputWrapper.js +4 -2
  15. package/dist/cjs/types/schema.js +40 -0
  16. package/dist/esm/builder/FileField.js +4 -4
  17. package/dist/esm/builder/FormField.js +41 -31
  18. package/dist/esm/builder/SchemaEditor.js +428 -0
  19. package/dist/esm/form/Controller.js +1 -1
  20. package/dist/esm/form/Form.js +4 -5
  21. package/dist/esm/hooks/useFormBuilder.js +25 -13
  22. package/dist/esm/index.js +4 -1
  23. package/dist/esm/input/Checkbox.js +4 -3
  24. package/dist/esm/input/DateInput.js +183 -0
  25. package/dist/esm/input/Field.js +1 -1
  26. package/dist/esm/input/{Range.js → RangeSlider.js} +2 -2
  27. package/dist/esm/input/Switch.js +4 -3
  28. package/dist/esm/internal/Field.module.css +1 -1
  29. package/dist/esm/internal/InputWrapper.js +4 -2
  30. package/dist/esm/types/schema.js +40 -0
  31. package/dist/types/builder/FileField.d.ts +2 -2
  32. package/dist/types/builder/FormField.d.ts +3 -1
  33. package/dist/types/builder/SchemaEditor.d.ts +10 -0
  34. package/dist/types/form/Controller.d.ts +1 -1
  35. package/dist/types/form/Form.d.ts +5 -1
  36. package/dist/types/hooks/useFormBuilder.d.ts +3 -1
  37. package/dist/types/index.d.ts +4 -2
  38. package/dist/types/input/Checkbox.d.ts +4 -3
  39. package/dist/types/input/DateInput.d.ts +19 -0
  40. package/dist/types/input/{Range.d.ts → RangeSlider.d.ts} +1 -1
  41. package/dist/types/input/Switch.d.ts +1 -1
  42. package/dist/types/internal/InputWrapper.d.ts +1 -1
  43. package/dist/types/types/index.d.ts +6 -2
  44. package/dist/types/types/schema.d.ts +52 -33
  45. package/package.json +5 -4
  46. package/src/builder/FileField.tsx +5 -4
  47. package/src/builder/FormField.tsx +42 -29
  48. package/src/builder/SchemaEditor.tsx +441 -0
  49. package/src/form/Controller.tsx +2 -2
  50. package/src/form/Form.tsx +6 -4
  51. package/src/hooks/useFormBuilder.tsx +29 -15
  52. package/src/index.ts +5 -2
  53. package/src/input/Checkbox.tsx +4 -3
  54. package/src/input/DateInput.tsx +248 -0
  55. package/src/input/Field.tsx +2 -1
  56. package/src/input/{Range.tsx → RangeSlider.tsx} +1 -1
  57. package/src/input/Switch.tsx +2 -0
  58. package/src/internal/Field.module.css +12 -1
  59. package/src/internal/InputWrapper.tsx +5 -0
  60. package/src/types/index.ts +8 -4
  61. package/src/types/schema.ts +65 -41
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@react-fabric/form",
3
- "version": "1.0.0-beta.32",
3
+ "version": "1.0.0-beta.33",
4
4
  "description": "RUF: Form package",
5
5
  "author": "Adarsh Pastakia <adarshpastakia@outlook.com>",
6
6
  "license": "MIT",
@@ -39,8 +39,9 @@
39
39
  "@dnd-kit/utilities": "^3.2.2",
40
40
  "@floating-ui/react": "^0.26.24",
41
41
  "@hookform/resolvers": "^3.9.0",
42
- "@react-fabric/core": "^1.0.0-beta.32",
43
- "@react-fabric/utilities": "^1.0.0-beta.32",
42
+ "@react-fabric/core": "^1.0.0-beta.33",
43
+ "@react-fabric/date": "^1.0.0-beta.33",
44
+ "@react-fabric/utilities": "^1.0.0-beta.33",
44
45
  "classnames": "^2.5.1",
45
46
  "color": "^4.2.3",
46
47
  "react-color": "^2.19.3",
@@ -60,5 +61,5 @@
60
61
  "node": ">=18.12.0",
61
62
  "npm": ">=8.19.0"
62
63
  },
63
- "gitHead": "e8ba7088116f5ae253ed989854d6fdaef2ad7507"
64
+ "gitHead": "3b7ffc99aac7b8fdc72c7122c39db55d1104297f"
64
65
  }
@@ -24,20 +24,21 @@
24
24
  import { Button, Icon, ProgressBar } from "@react-fabric/core";
25
25
  import { FileUtil, Format } from "@react-fabric/utilities";
26
26
  import { Fragment } from "react";
27
- import { type UploadHandler, useFileUploader } from "../hooks/useFileUploader";
27
+ import { useFileUploader, type UploadHandler } from "../hooks/useFileUploader";
28
28
  import { Field } from "../input/Field";
29
29
  import { HiddenInput } from "../input/Hidden";
30
- import { type FileSchema } from "../types/schema";
30
+ import { type FileSchema, type ValueType } from "../types/schema";
31
31
 
32
32
  export const FileField = ({
33
33
  uploadHandler,
34
34
  inline,
35
35
  label,
36
36
  accept,
37
- multiple = false,
38
37
  value,
38
+ fileUrl,
39
+ multiple,
39
40
  ...rest
40
- }: Partial<FileSchema> & {
41
+ }: Partial<FileSchema & ValueType> & {
41
42
  value?: AnyObject;
42
43
  inline?: boolean;
43
44
  fileUrl?: (path: string) => string;
@@ -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,34 @@ 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.COLOR) return ColorInput;
83
+ if (hasOptions) return Select;
72
84
 
73
85
  return Input;
74
- }, [rest.datatype]);
86
+ }, [rest.datatype, hasOptions]);
75
87
 
76
88
  const props = useMemo(() => {
77
- if (rest.datatype === "number")
89
+ if (rest.datatype === DATA_TYPES.NUMBER)
78
90
  return {
79
91
  min: rest.min,
80
92
  max: rest.max,
81
93
  step: rest.step ?? 1,
82
94
  };
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")
95
+ if (rest.datatype === DATA_TYPES.RANGE)
90
96
  return {
91
97
  min: rest.min ?? 1,
92
98
  max: rest.max ?? 99,
@@ -94,20 +100,27 @@ export const FormField = ({
94
100
  showValue: true,
95
101
  showLabels: true,
96
102
  };
97
- if (rest.datatype === "text") return { rows: 3 };
98
- if (rest.datatype === "color") return { showPicker: true };
103
+ if (rest.datatype === DATA_TYPES.DATE)
104
+ return {
105
+ min: rest.min,
106
+ max: rest.max,
107
+ type: rest.type,
108
+ };
109
+ if (rest.datatype === DATA_TYPES.TEXT) return { rows: 3 };
110
+ if (rest.datatype === DATA_TYPES.COLOR) return { showPicker: true };
99
111
 
100
- if (rest.datatype === "string" && rest.options)
112
+ if (rest.datatype === DATA_TYPES.STRING && hasOptions)
101
113
  return {
102
- options: rest.options,
103
114
  multiple: rest.multiple,
115
+ allowCreate: rest.allowCustom,
104
116
  searchable: true,
117
+ ...(optionLists?.[rest.optionList ?? ""] ?? { options: rest.options }),
105
118
  };
106
119
 
107
120
  return {};
108
- }, [rest]);
121
+ }, [rest, hasOptions, optionLists]);
109
122
 
110
- if (rest.datatype === "avatar")
123
+ if (rest.datatype === DATA_TYPES.AVATAR)
111
124
  return (
112
125
  uploadHandler && (
113
126
  <Controller name={fieldName}>
@@ -120,7 +133,7 @@ export const FormField = ({
120
133
  )
121
134
  );
122
135
 
123
- if (rest.datatype === "file")
136
+ if (rest.datatype === DATA_TYPES.FILE)
124
137
  return (
125
138
  uploadHandler && (
126
139
  <Controller name={fieldName}>
@@ -135,12 +148,12 @@ export const FormField = ({
135
148
  )
136
149
  );
137
150
 
138
- return rest.multiple && !("options" in rest) ? (
151
+ return rest.multiple && !hasOptions ? (
139
152
  <ArrayInput
140
153
  name={fieldName}
141
154
  label={t(label, label)}
142
155
  required={required}
143
- onAdd={() => ""}
156
+ onAdd={() => String("")}
144
157
  >
145
158
  <Controller>
146
159
  <E {...props} />
@@ -0,0 +1,441 @@
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
+ Collapsable,
27
+ Content,
28
+ CoreIcons,
29
+ Footer,
30
+ Modal,
31
+ ToggleButtonGroup,
32
+ useOverlayService,
33
+ } from "@react-fabric/core";
34
+ import { EMPTY_ARRAY } from "@react-fabric/utilities";
35
+ import { Fragment, useCallback, useEffect, useRef, useState } from "react";
36
+ import { Controller } from "../form/Controller";
37
+ import { Form, type FormRef } from "../form/Form";
38
+ import { ArrayInput } from "../input/ArrayInput";
39
+ import { Checkbox } from "../input/Checkbox";
40
+ import { DateInput } from "../input/DateInput";
41
+ import { Field } from "../input/Field";
42
+ import { Input } from "../input/Input";
43
+ import { Number } from "../input/Number";
44
+ import { Select } from "../select/Select";
45
+ import {
46
+ DATA_TYPES,
47
+ type DateSchema,
48
+ type DefaultSchema,
49
+ type FormSchema,
50
+ type NumberSchema,
51
+ type SchemaDef,
52
+ type StringSchema,
53
+ type ValueType,
54
+ } from "../types/schema";
55
+
56
+ interface Props {
57
+ schemaDef: FormSchema;
58
+ allowRemove?: boolean;
59
+ acceptableTypes?: DATA_TYPES[];
60
+
61
+ optionLists?: string[];
62
+
63
+ onChange: (schema: FormSchema) => void;
64
+ }
65
+
66
+ const CUSTOM_LIST = "--CUSTOM--";
67
+
68
+ const SchemaField = ({
69
+ field,
70
+ getModel,
71
+ optionLists = EMPTY_ARRAY,
72
+ allowRemove,
73
+ onTypeChange,
74
+ acceptableTypes,
75
+ }: {
76
+ field: string;
77
+ getModel: () => KeyValue;
78
+ onTypeChange: (val: KeyValue) => void;
79
+ } & Omit<Props, "schemaDef" | "onChange">) => {
80
+ const [type, setType] = useState<DATA_TYPES>();
81
+ const [multiple, setMultiple] = useState(false);
82
+ const [hasOptions, setHasOptions] = useState(false);
83
+ const [customOptions, setCustomOptions] = useState(false);
84
+
85
+ const [OptionModal, openOptionModal] = useOverlayService((props) => (
86
+ <Modal {...props}>
87
+ <Content>
88
+ <ArrayInput name={`${field}.options`} onAdd={() => String("")}>
89
+ <Controller>
90
+ <Input />
91
+ </Controller>
92
+ </ArrayInput>
93
+ </Content>
94
+ <Footer
95
+ flex
96
+ justify="end"
97
+ className="sticky bottom-0 border-t bg-base px-6 py-2"
98
+ >
99
+ <Button variant="link" onClick={props.onClose}>
100
+ Close
101
+ </Button>
102
+ </Footer>
103
+ </Modal>
104
+ ));
105
+
106
+ useEffect(() => {
107
+ setTimeout(() => {
108
+ const model = getModel();
109
+ setType(model.datatype);
110
+ setMultiple(model.multiple);
111
+ setHasOptions(!!model.optionList);
112
+ setCustomOptions(model.optionList === CUSTOM_LIST);
113
+ }, 10);
114
+ }, []);
115
+
116
+ useEffect(() => {
117
+ if (type) {
118
+ const modelRef = getModel();
119
+ let reset = {
120
+ datatype: type,
121
+ id: field,
122
+ label: modelRef.label,
123
+ defaultValue: modelRef.defaultValue,
124
+ required: modelRef.required,
125
+ multiple: type === DATA_TYPES.AVATAR ? false : modelRef.multiple,
126
+ minItems: modelRef.minItems,
127
+ maxItems: modelRef.maxItems,
128
+ } as DefaultSchema & ValueType;
129
+ if (type === DATA_TYPES.STRING) {
130
+ reset = Object.assign(reset, {
131
+ optionList: modelRef.optionList,
132
+ options: modelRef.options,
133
+ regex: modelRef.regex,
134
+ } as StringSchema);
135
+ } else if (type === DATA_TYPES.DATE) {
136
+ reset = Object.assign(reset, {
137
+ type: "date",
138
+ } as DateSchema);
139
+ } else if (type === DATA_TYPES.NUMBER || type === DATA_TYPES.RANGE) {
140
+ reset = Object.assign(reset, {
141
+ min: modelRef.min,
142
+ max: modelRef.max,
143
+ step: modelRef.step,
144
+ } as NumberSchema);
145
+ }
146
+ if (type === DATA_TYPES.AVATAR) {
147
+ setMultiple(false);
148
+ }
149
+ onTypeChange(reset);
150
+ }
151
+ }, [type]);
152
+
153
+ return (
154
+ <Collapsable key={field} open>
155
+ <div className="flex flex-nowrap group/field">
156
+ <span className="flex-1 truncate">{field}</span>
157
+ {allowRemove && (
158
+ <Button
159
+ size="sm"
160
+ color="danger"
161
+ variant="link"
162
+ aria-label="Delete"
163
+ stopPropagation
164
+ icon={CoreIcons.trash}
165
+ className="invisible group-hover/field:visible"
166
+ />
167
+ )}
168
+ </div>
169
+ <div className="flex flex-col gap-1 pb-4">
170
+ <Controller name={`${field}.datatype`}>
171
+ <Select
172
+ inline
173
+ label="Type"
174
+ placeholder="Data type"
175
+ options={acceptableTypes}
176
+ onChange={(val) => setType(val as unknown as DATA_TYPES)}
177
+ />
178
+ </Controller>
179
+ <Controller name={`${field}.label`}>
180
+ <Input
181
+ inline
182
+ label="Label"
183
+ allowClear
184
+ placeholder="Label..."
185
+ decorateEndShowWhenEmpty
186
+ decorateEnd={
187
+ <span className="text-sm text-muted/50 px-1">OPTIONAL</span>
188
+ }
189
+ />
190
+ </Controller>
191
+ {type === DATA_TYPES.STRING && (
192
+ <Fragment>
193
+ <Controller name={`${field}.regex`}>
194
+ <Input
195
+ inline
196
+ label="Regex validation"
197
+ allowClear
198
+ placeholder="Regex value..."
199
+ decorateEndShowWhenEmpty
200
+ decorateEnd={
201
+ <span className="text-sm text-muted/50 px-1">OPTIONAL</span>
202
+ }
203
+ />
204
+ </Controller>
205
+ <Field inline label="Option list">
206
+ <Controller name={`${field}.optionList`}>
207
+ <Select
208
+ allowClear
209
+ options={[CUSTOM_LIST, ...optionLists]}
210
+ onChange={(v) => setCustomOptions(v === CUSTOM_LIST)}
211
+ />
212
+ </Controller>
213
+ {customOptions && (
214
+ <Button
215
+ icon={CoreIcons.list}
216
+ aria-label="Custom options"
217
+ onClick={() => {
218
+ void openOptionModal();
219
+ }}
220
+ />
221
+ )}
222
+ </Field>
223
+ {OptionModal}
224
+ </Fragment>
225
+ )}
226
+ {type === DATA_TYPES.FILE && (
227
+ <Fragment>
228
+ <Controller name={`${field}.accept`}>
229
+ <Input
230
+ inline
231
+ label="Accept types"
232
+ allowClear
233
+ placeholder="Accept value..."
234
+ decorateEndShowWhenEmpty
235
+ decorateEnd={
236
+ <span className="text-sm text-muted/50 px-1">OPTIONAL</span>
237
+ }
238
+ />
239
+ </Controller>
240
+ </Fragment>
241
+ )}
242
+ {[DATA_TYPES.NUMBER, DATA_TYPES.RANGE].includes(
243
+ type as unknown as DATA_TYPES,
244
+ ) && (
245
+ <Fragment>
246
+ <Controller name={`${field}.min`}>
247
+ <Number
248
+ inline
249
+ label="Min value"
250
+ allowClear
251
+ placeholder="Minimum value..."
252
+ decorateEndShowWhenEmpty
253
+ decorateEnd={
254
+ <span className="text-sm text-muted/50 px-1">OPTIONAL</span>
255
+ }
256
+ />
257
+ </Controller>
258
+ <Controller name={`${field}.max`}>
259
+ <Number
260
+ inline
261
+ label="Max value"
262
+ allowClear
263
+ placeholder="Maximum value..."
264
+ decorateEndShowWhenEmpty
265
+ decorateEnd={
266
+ <span className="text-sm text-muted/50 px-1">OPTIONAL</span>
267
+ }
268
+ />
269
+ </Controller>
270
+ <Controller name={`${field}.step`}>
271
+ <Number
272
+ inline
273
+ label="Step value"
274
+ allowClear
275
+ placeholder="Step value..."
276
+ decorateEndShowWhenEmpty
277
+ decorateEnd={
278
+ <span className="text-sm text-muted/50 px-1">OPTIONAL</span>
279
+ }
280
+ />
281
+ </Controller>
282
+ </Fragment>
283
+ )}
284
+ {type === DATA_TYPES.DATE && (
285
+ <Fragment>
286
+ <Field inline label="Type" plain>
287
+ <Controller name={`${field}.type`}>
288
+ <ToggleButtonGroup value="" fullWidth className="flex-1">
289
+ <Button className="flex-1" value="date">
290
+ Date
291
+ </Button>
292
+ <Button className="flex-1" value="datetime">
293
+ Date Time
294
+ </Button>
295
+ </ToggleButtonGroup>
296
+ </Controller>
297
+ </Field>
298
+ <Controller name={`${field}.min`}>
299
+ <DateInput
300
+ inline
301
+ label="Min value"
302
+ allowClear
303
+ placeholder="Minimum value..."
304
+ decorateEndShowWhenEmpty
305
+ decorateEnd={
306
+ <span className="text-sm text-muted/50 px-1">OPTIONAL</span>
307
+ }
308
+ />
309
+ </Controller>
310
+ <Controller name={`${field}.max`}>
311
+ <DateInput
312
+ inline
313
+ label="Max value"
314
+ allowClear
315
+ placeholder="Maximum value..."
316
+ decorateEndShowWhenEmpty
317
+ decorateEnd={
318
+ <span className="text-sm text-muted/50 px-1">OPTIONAL</span>
319
+ }
320
+ />
321
+ </Controller>
322
+ </Fragment>
323
+ )}
324
+ <Field label=" " inline plain>
325
+ <Controller name={`${field}.required`}>
326
+ <Checkbox label="Required" />
327
+ </Controller>
328
+ {type !== DATA_TYPES.AVATAR && (
329
+ <Controller name={`${field}.multiple`}>
330
+ <Checkbox label="Multiple" onChange={setMultiple} />
331
+ </Controller>
332
+ )}
333
+ {hasOptions && (
334
+ <Controller name={`${field}.allowCustom`}>
335
+ <Checkbox label="Allow custom value" />
336
+ </Controller>
337
+ )}
338
+ </Field>
339
+ {multiple && (
340
+ <Fragment>
341
+ <Controller name={`${field}.minItems`}>
342
+ <Number
343
+ inline
344
+ label="Min items"
345
+ allowClear
346
+ placeholder="Minimum items..."
347
+ decorateEndShowWhenEmpty
348
+ decorateEnd={
349
+ <span className="text-sm text-muted/50 px-1">OPTIONAL</span>
350
+ }
351
+ />
352
+ </Controller>
353
+ <Controller name={`${field}.maxItems`}>
354
+ <Number
355
+ inline
356
+ label="Max items"
357
+ allowClear
358
+ placeholder="Maximum items..."
359
+ decorateEndShowWhenEmpty
360
+ decorateEnd={
361
+ <span className="text-sm text-muted/50 px-1">OPTIONAL</span>
362
+ }
363
+ />
364
+ </Controller>
365
+ </Fragment>
366
+ )}
367
+ </div>
368
+ </Collapsable>
369
+ );
370
+ };
371
+
372
+ export const SchemaEditor = ({
373
+ schemaDef,
374
+ allowRemove,
375
+ acceptableTypes,
376
+ optionLists = EMPTY_ARRAY,
377
+ onChange,
378
+ }: Props) => {
379
+ const formRef = useRef<FormRef<KeyValue>>(null);
380
+ const schemaRef = useRef<KeyValue>({});
381
+ const [schema, setSchema] = useState<KeyValue<SchemaDef>>({});
382
+ const [schemaFields, setSchemaFields] = useState<string[]>([]);
383
+
384
+ useEffect(() => {
385
+ const schemaMap = Object.fromEntries(
386
+ schemaDef.map((schema) => [schema.id, schema]),
387
+ );
388
+
389
+ const schema = Object.fromEntries(
390
+ Object.keys(schemaMap).map((field) => [
391
+ field,
392
+ schemaRef.current[field] ?? schemaMap[field],
393
+ ]),
394
+ );
395
+ setSchema(schema);
396
+ setSchemaFields(Object.keys(schema));
397
+ }, [schemaDef]);
398
+
399
+ const handleChange = useCallback((model: KeyValue<SchemaDef>) => {
400
+ onChange?.(Object.values(model));
401
+ }, []);
402
+
403
+ const handleTypeChange = useCallback((field: string, val: KeyValue) => {
404
+ formRef.current?.setValue(field, val);
405
+ }, []);
406
+
407
+ return (
408
+ <Form
409
+ formRef={formRef}
410
+ values={schema}
411
+ defaultValues={schema}
412
+ onSubmit={handleChange}
413
+ >
414
+ <Content className="divide-y">
415
+ {schemaFields.map((field) => (
416
+ <SchemaField
417
+ key={field}
418
+ field={field}
419
+ allowRemove={allowRemove}
420
+ optionLists={optionLists}
421
+ getModel={() => formRef.current?.getValues()[field]}
422
+ onTypeChange={(val: KeyValue) => handleTypeChange(field, val)}
423
+ acceptableTypes={acceptableTypes ?? Object.values(DATA_TYPES)}
424
+ />
425
+ ))}
426
+ </Content>
427
+ <Footer
428
+ flex
429
+ justify="end"
430
+ className="sticky bottom-0 border-t bg-base px-6 py-2"
431
+ >
432
+ <Button type="reset" variant="link">
433
+ Reset
434
+ </Button>
435
+ <Button type="submit" variant="solid">
436
+ Submit
437
+ </Button>
438
+ </Footer>
439
+ </Form>
440
+ );
441
+ };
@@ -46,7 +46,7 @@ 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
 
@@ -86,7 +86,7 @@ export const Controller = ({ name, children, ...rest }: ControllerProps) => {
86
86
  : children({
87
87
  ...field,
88
88
  ...rest,
89
- inputRef: (el: AnyObject) => {
89
+ ref: (el: AnyObject) => {
90
90
  el && ref(el);
91
91
  },
92
92
  readOnly: isSubmitting,
package/src/form/Form.tsx CHANGED
@@ -57,6 +57,10 @@ export interface FormProps<K extends KeyValue = KeyValue> {
57
57
  * form data schema
58
58
  */
59
59
  schema?: yup.ObjectSchema<K>;
60
+ /**
61
+ * current form model
62
+ */
63
+ values?: K;
60
64
  /**
61
65
  * default data values
62
66
  */
@@ -77,6 +81,7 @@ export const Form = <K extends KeyValue>({
77
81
  formRef,
78
82
  schema,
79
83
  children,
84
+ values,
80
85
  defaultValues,
81
86
  onSubmit = DEFAULT_SUBMIT,
82
87
  onChange,
@@ -84,15 +89,12 @@ export const Form = <K extends KeyValue>({
84
89
  }: PropsWithChildren<FormProps<K>>) => {
85
90
  const ref = useRef<HTMLFormElement>(null);
86
91
  const form = useForm<K>({
92
+ values,
87
93
  shouldFocusError: true,
88
94
  resolver: schema && (yupResolver(schema) as AnyObject),
89
95
  defaultValues: defaultValues as DefaultValues<K>,
90
96
  });
91
97
 
92
- useEffect(() => {
93
- form.reset();
94
- }, []);
95
-
96
98
  const [, startTransition] = useTransition();
97
99
  const changeHandler = useDebounce(onChange, [onChange]);
98
100
  useEffect(() => {