@react-fabric/form 1.0.0-beta.32 → 1.0.0-beta.34
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/builder/FileField.js +4 -4
- package/dist/cjs/builder/FormField.js +44 -34
- package/dist/cjs/builder/SchemaEditor.js +439 -0
- package/dist/cjs/form/Controller.js +1 -1
- package/dist/cjs/form/Form.js +10 -4
- package/dist/cjs/hooks/useFormBuilder.js +27 -15
- package/dist/cjs/index.js +8 -2
- package/dist/cjs/input/Checkbox.js +4 -3
- package/dist/cjs/input/ColorInput.js +1 -1
- package/dist/cjs/input/DateInput.js +183 -0
- package/dist/cjs/input/Field.js +1 -1
- package/dist/cjs/input/File.js +1 -1
- package/dist/cjs/input/Hidden.js +1 -1
- package/dist/cjs/input/Input.js +1 -1
- package/dist/cjs/input/Number.js +1 -1
- package/dist/cjs/input/Password.js +1 -1
- package/dist/cjs/input/{Range.js → RangeSlider.js} +3 -3
- package/dist/cjs/input/Search.js +1 -1
- package/dist/cjs/input/Slider.js +1 -1
- package/dist/cjs/input/Switch.js +4 -3
- package/dist/cjs/input/Textarea.js +1 -1
- package/dist/cjs/internal/Field.module.css +1 -1
- package/dist/cjs/internal/InputWrapper.js +4 -2
- package/dist/cjs/types/schema.js +40 -0
- package/dist/esm/builder/FileField.js +4 -4
- package/dist/esm/builder/FormField.js +44 -34
- package/dist/esm/builder/SchemaEditor.js +439 -0
- package/dist/esm/form/Controller.js +1 -1
- package/dist/esm/form/Form.js +11 -5
- package/dist/esm/hooks/useFormBuilder.js +25 -13
- package/dist/esm/index.js +4 -1
- package/dist/esm/input/Checkbox.js +4 -3
- package/dist/esm/input/ColorInput.js +1 -1
- package/dist/esm/input/DateInput.js +183 -0
- package/dist/esm/input/Field.js +1 -1
- package/dist/esm/input/File.js +1 -1
- package/dist/esm/input/Hidden.js +1 -1
- package/dist/esm/input/Input.js +1 -1
- package/dist/esm/input/Number.js +1 -1
- package/dist/esm/input/Password.js +1 -1
- package/dist/esm/input/{Range.js → RangeSlider.js} +3 -3
- package/dist/esm/input/Search.js +1 -1
- package/dist/esm/input/Slider.js +1 -1
- package/dist/esm/input/Switch.js +4 -3
- package/dist/esm/input/Textarea.js +1 -1
- package/dist/esm/internal/Field.module.css +1 -1
- package/dist/esm/internal/InputWrapper.js +4 -2
- package/dist/esm/types/schema.js +40 -0
- package/dist/types/builder/FileField.d.ts +2 -2
- package/dist/types/builder/FormField.d.ts +3 -1
- package/dist/types/builder/SchemaEditor.d.ts +16 -0
- package/dist/types/form/Controller.d.ts +1 -1
- package/dist/types/form/Form.d.ts +1 -0
- package/dist/types/hooks/useFormBuilder.d.ts +3 -1
- package/dist/types/index.d.ts +4 -2
- package/dist/types/input/Checkbox.d.ts +4 -3
- package/dist/types/input/ColorInput.d.ts +1 -1
- package/dist/types/input/DateInput.d.ts +19 -0
- package/dist/types/input/{Range.d.ts → RangeSlider.d.ts} +1 -1
- package/dist/types/input/Switch.d.ts +1 -1
- package/dist/types/internal/InputWrapper.d.ts +1 -1
- package/dist/types/types/index.d.ts +7 -3
- package/dist/types/types/schema.d.ts +52 -33
- package/package.json +5 -4
- package/src/builder/FileField.tsx +5 -4
- package/src/builder/FormField.tsx +45 -32
- package/src/builder/SchemaEditor.tsx +457 -0
- package/src/form/Controller.tsx +2 -2
- package/src/form/Form.tsx +6 -6
- package/src/hooks/useFormBuilder.tsx +29 -15
- package/src/index.ts +5 -2
- package/src/input/Checkbox.tsx +4 -3
- package/src/input/ColorInput.tsx +5 -2
- package/src/input/DateInput.tsx +248 -0
- package/src/input/Field.tsx +2 -1
- package/src/input/File.tsx +1 -1
- package/src/input/Hidden.tsx +4 -3
- package/src/input/Input.tsx +1 -1
- package/src/input/Number.tsx +1 -1
- package/src/input/Password.tsx +1 -1
- package/src/input/{Range.tsx → RangeSlider.tsx} +2 -2
- package/src/input/Search.tsx +1 -1
- package/src/input/Slider.tsx +1 -1
- package/src/input/Switch.tsx +2 -0
- package/src/input/Textarea.tsx +1 -1
- package/src/internal/Field.module.css +12 -1
- package/src/internal/InputWrapper.tsx +5 -0
- package/src/types/index.ts +9 -5
- package/src/types/schema.ts +65 -41
|
@@ -0,0 +1,457 @@
|
|
|
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 { type Elements } from "@react-fabric/core/dist/types/types";
|
|
35
|
+
import { EMPTY_ARRAY } from "@react-fabric/utilities";
|
|
36
|
+
import { Fragment, useCallback, useEffect, useRef, useState } from "react";
|
|
37
|
+
import { Controller } from "../form/Controller";
|
|
38
|
+
import { Form, type FormRef } from "../form/Form";
|
|
39
|
+
import { ArrayInput } from "../input/ArrayInput";
|
|
40
|
+
import { Checkbox } from "../input/Checkbox";
|
|
41
|
+
import { DateInput } from "../input/DateInput";
|
|
42
|
+
import { Field } from "../input/Field";
|
|
43
|
+
import { HiddenInput } from "../input/Hidden";
|
|
44
|
+
import { Input } from "../input/Input";
|
|
45
|
+
import { Number } from "../input/Number";
|
|
46
|
+
import { Select } from "../select/Select";
|
|
47
|
+
import {
|
|
48
|
+
DATA_TYPES,
|
|
49
|
+
type DateSchema,
|
|
50
|
+
type DefaultSchema,
|
|
51
|
+
type FormSchema,
|
|
52
|
+
type NumberSchema,
|
|
53
|
+
type SchemaDef,
|
|
54
|
+
type StringSchema,
|
|
55
|
+
type ValueType,
|
|
56
|
+
} from "../types/schema";
|
|
57
|
+
|
|
58
|
+
interface Props {
|
|
59
|
+
schemaDef: FormSchema;
|
|
60
|
+
allowRemove?: boolean;
|
|
61
|
+
acceptableTypes?: DATA_TYPES[];
|
|
62
|
+
|
|
63
|
+
optionLists?: string[];
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Footer actions
|
|
67
|
+
*/
|
|
68
|
+
children?: Elements<JSX.Element> | Array<Elements<JSX.Element>>;
|
|
69
|
+
|
|
70
|
+
onChange: (schema: FormSchema) => void;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
const CUSTOM_LIST = "--CUSTOM--";
|
|
74
|
+
|
|
75
|
+
const SchemaField = ({
|
|
76
|
+
field,
|
|
77
|
+
getModel,
|
|
78
|
+
onRemove,
|
|
79
|
+
optionLists = EMPTY_ARRAY,
|
|
80
|
+
allowRemove,
|
|
81
|
+
onTypeChange,
|
|
82
|
+
acceptableTypes,
|
|
83
|
+
}: {
|
|
84
|
+
field: string;
|
|
85
|
+
onRemove: () => void;
|
|
86
|
+
getModel: () => KeyValue;
|
|
87
|
+
onTypeChange: (val: KeyValue) => void;
|
|
88
|
+
} & Omit<Props, "schemaDef" | "onChange">) => {
|
|
89
|
+
const [type, setType] = useState<DATA_TYPES>();
|
|
90
|
+
const [multiple, setMultiple] = useState(false);
|
|
91
|
+
const [hasOptions, setHasOptions] = useState(false);
|
|
92
|
+
const [customOptions, setCustomOptions] = useState(false);
|
|
93
|
+
|
|
94
|
+
const [OptionModal, openOptionModal] = useOverlayService((props) => (
|
|
95
|
+
<Modal {...props}>
|
|
96
|
+
<Content>
|
|
97
|
+
<ArrayInput name={`${field}.options`} onAdd={() => String("")}>
|
|
98
|
+
<Controller>
|
|
99
|
+
<Input />
|
|
100
|
+
</Controller>
|
|
101
|
+
</ArrayInput>
|
|
102
|
+
</Content>
|
|
103
|
+
<Footer
|
|
104
|
+
flex
|
|
105
|
+
justify="end"
|
|
106
|
+
className="sticky bottom-0 border-t bg-base px-6 py-2"
|
|
107
|
+
>
|
|
108
|
+
<Button variant="link" onClick={props.onClose}>
|
|
109
|
+
Close
|
|
110
|
+
</Button>
|
|
111
|
+
</Footer>
|
|
112
|
+
</Modal>
|
|
113
|
+
));
|
|
114
|
+
|
|
115
|
+
useEffect(() => {
|
|
116
|
+
setTimeout(() => {
|
|
117
|
+
const model = getModel();
|
|
118
|
+
setType(model.datatype);
|
|
119
|
+
setMultiple(model.multiple);
|
|
120
|
+
setHasOptions(!!model.optionList);
|
|
121
|
+
setCustomOptions(model.optionList === CUSTOM_LIST);
|
|
122
|
+
}, 10);
|
|
123
|
+
}, []);
|
|
124
|
+
|
|
125
|
+
const handleTypeChange = useCallback((type: DATA_TYPES) => {
|
|
126
|
+
if (type) {
|
|
127
|
+
const modelRef = getModel();
|
|
128
|
+
let reset = {
|
|
129
|
+
datatype: type,
|
|
130
|
+
id: field,
|
|
131
|
+
label: modelRef.label,
|
|
132
|
+
defaultValue: modelRef.defaultValue,
|
|
133
|
+
required: modelRef.required,
|
|
134
|
+
multiple: type === DATA_TYPES.AVATAR ? false : modelRef.multiple,
|
|
135
|
+
minItems: modelRef.minItems,
|
|
136
|
+
maxItems: modelRef.maxItems,
|
|
137
|
+
} as DefaultSchema & ValueType;
|
|
138
|
+
if (type === DATA_TYPES.STRING) {
|
|
139
|
+
reset = Object.assign(reset, {
|
|
140
|
+
optionList: modelRef.optionList,
|
|
141
|
+
options: modelRef.options,
|
|
142
|
+
regex: modelRef.regex,
|
|
143
|
+
} as StringSchema);
|
|
144
|
+
} else if (type === DATA_TYPES.DATE) {
|
|
145
|
+
reset = Object.assign(reset, {
|
|
146
|
+
type: "date",
|
|
147
|
+
} as DateSchema);
|
|
148
|
+
} else if (type === DATA_TYPES.NUMBER || type === DATA_TYPES.RANGE) {
|
|
149
|
+
reset = Object.assign(reset, {
|
|
150
|
+
min: modelRef.min,
|
|
151
|
+
max: modelRef.max,
|
|
152
|
+
step: modelRef.step,
|
|
153
|
+
} as NumberSchema);
|
|
154
|
+
}
|
|
155
|
+
if (type === DATA_TYPES.AVATAR) {
|
|
156
|
+
setMultiple(false);
|
|
157
|
+
}
|
|
158
|
+
onTypeChange(reset);
|
|
159
|
+
setType(type);
|
|
160
|
+
}
|
|
161
|
+
}, []);
|
|
162
|
+
|
|
163
|
+
return (
|
|
164
|
+
<Collapsable key={field} open>
|
|
165
|
+
<div className="flex flex-nowrap group/field">
|
|
166
|
+
<span className="flex-1 truncate">{field}</span>
|
|
167
|
+
{allowRemove && (
|
|
168
|
+
<Button
|
|
169
|
+
size="sm"
|
|
170
|
+
color="danger"
|
|
171
|
+
variant="link"
|
|
172
|
+
aria-label="Delete"
|
|
173
|
+
stopPropagation
|
|
174
|
+
icon={CoreIcons.trash}
|
|
175
|
+
onClick={onRemove}
|
|
176
|
+
className="invisible group-hover/field:visible"
|
|
177
|
+
/>
|
|
178
|
+
)}
|
|
179
|
+
</div>
|
|
180
|
+
<div className="flex flex-col gap-1 pb-4">
|
|
181
|
+
<Controller name={`${field}.id`}>
|
|
182
|
+
<HiddenInput hiddenValue={field} />
|
|
183
|
+
</Controller>
|
|
184
|
+
<Controller name={`${field}.datatype`}>
|
|
185
|
+
<Select
|
|
186
|
+
inline
|
|
187
|
+
label="Type"
|
|
188
|
+
placeholder="Data type"
|
|
189
|
+
options={acceptableTypes}
|
|
190
|
+
onChange={(val) => handleTypeChange(val as unknown as DATA_TYPES)}
|
|
191
|
+
/>
|
|
192
|
+
</Controller>
|
|
193
|
+
<Controller name={`${field}.label`}>
|
|
194
|
+
<Input
|
|
195
|
+
inline
|
|
196
|
+
label="Label"
|
|
197
|
+
allowClear
|
|
198
|
+
placeholder="Label..."
|
|
199
|
+
decorateEndShowWhenEmpty
|
|
200
|
+
decorateEnd={
|
|
201
|
+
<span className="text-sm text-muted/50 px-1">OPTIONAL</span>
|
|
202
|
+
}
|
|
203
|
+
/>
|
|
204
|
+
</Controller>
|
|
205
|
+
{type === DATA_TYPES.STRING && (
|
|
206
|
+
<Fragment>
|
|
207
|
+
<Controller name={`${field}.regex`}>
|
|
208
|
+
<Input
|
|
209
|
+
inline
|
|
210
|
+
label="Regex validation"
|
|
211
|
+
allowClear
|
|
212
|
+
placeholder="Regex value..."
|
|
213
|
+
decorateEndShowWhenEmpty
|
|
214
|
+
decorateEnd={
|
|
215
|
+
<span className="text-sm text-muted/50 px-1">OPTIONAL</span>
|
|
216
|
+
}
|
|
217
|
+
/>
|
|
218
|
+
</Controller>
|
|
219
|
+
<Field inline label="Option list">
|
|
220
|
+
<Controller name={`${field}.optionList`}>
|
|
221
|
+
<Select
|
|
222
|
+
allowClear
|
|
223
|
+
options={[CUSTOM_LIST, ...optionLists]}
|
|
224
|
+
onChange={(v) => setCustomOptions(v === CUSTOM_LIST)}
|
|
225
|
+
/>
|
|
226
|
+
</Controller>
|
|
227
|
+
{customOptions && (
|
|
228
|
+
<Button
|
|
229
|
+
icon={CoreIcons.list}
|
|
230
|
+
aria-label="Custom options"
|
|
231
|
+
onClick={() => {
|
|
232
|
+
void openOptionModal();
|
|
233
|
+
}}
|
|
234
|
+
/>
|
|
235
|
+
)}
|
|
236
|
+
</Field>
|
|
237
|
+
{OptionModal}
|
|
238
|
+
</Fragment>
|
|
239
|
+
)}
|
|
240
|
+
{type === DATA_TYPES.FILE && (
|
|
241
|
+
<Fragment>
|
|
242
|
+
<Controller name={`${field}.accept`}>
|
|
243
|
+
<Input
|
|
244
|
+
inline
|
|
245
|
+
label="Accept types"
|
|
246
|
+
allowClear
|
|
247
|
+
placeholder="Accept value..."
|
|
248
|
+
decorateEndShowWhenEmpty
|
|
249
|
+
decorateEnd={
|
|
250
|
+
<span className="text-sm text-muted/50 px-1">OPTIONAL</span>
|
|
251
|
+
}
|
|
252
|
+
/>
|
|
253
|
+
</Controller>
|
|
254
|
+
</Fragment>
|
|
255
|
+
)}
|
|
256
|
+
{[DATA_TYPES.NUMBER, DATA_TYPES.RANGE].includes(
|
|
257
|
+
type as unknown as DATA_TYPES,
|
|
258
|
+
) && (
|
|
259
|
+
<Fragment>
|
|
260
|
+
<Controller name={`${field}.min`}>
|
|
261
|
+
<Number
|
|
262
|
+
inline
|
|
263
|
+
label="Min value"
|
|
264
|
+
allowClear
|
|
265
|
+
placeholder="Minimum value..."
|
|
266
|
+
decorateEndShowWhenEmpty
|
|
267
|
+
decorateEnd={
|
|
268
|
+
<span className="text-sm text-muted/50 px-1">OPTIONAL</span>
|
|
269
|
+
}
|
|
270
|
+
/>
|
|
271
|
+
</Controller>
|
|
272
|
+
<Controller name={`${field}.max`}>
|
|
273
|
+
<Number
|
|
274
|
+
inline
|
|
275
|
+
label="Max value"
|
|
276
|
+
allowClear
|
|
277
|
+
placeholder="Maximum value..."
|
|
278
|
+
decorateEndShowWhenEmpty
|
|
279
|
+
decorateEnd={
|
|
280
|
+
<span className="text-sm text-muted/50 px-1">OPTIONAL</span>
|
|
281
|
+
}
|
|
282
|
+
/>
|
|
283
|
+
</Controller>
|
|
284
|
+
<Controller name={`${field}.step`}>
|
|
285
|
+
<Number
|
|
286
|
+
inline
|
|
287
|
+
label="Step value"
|
|
288
|
+
allowClear
|
|
289
|
+
placeholder="Step value..."
|
|
290
|
+
decorateEndShowWhenEmpty
|
|
291
|
+
decorateEnd={
|
|
292
|
+
<span className="text-sm text-muted/50 px-1">OPTIONAL</span>
|
|
293
|
+
}
|
|
294
|
+
/>
|
|
295
|
+
</Controller>
|
|
296
|
+
</Fragment>
|
|
297
|
+
)}
|
|
298
|
+
{type === DATA_TYPES.DATE && (
|
|
299
|
+
<Fragment>
|
|
300
|
+
<Field inline label="Type" plain>
|
|
301
|
+
<Controller name={`${field}.type`}>
|
|
302
|
+
<ToggleButtonGroup value="" fullWidth className="flex-1">
|
|
303
|
+
<Button className="flex-1" value="date">
|
|
304
|
+
Date
|
|
305
|
+
</Button>
|
|
306
|
+
<Button className="flex-1" value="datetime">
|
|
307
|
+
Date Time
|
|
308
|
+
</Button>
|
|
309
|
+
</ToggleButtonGroup>
|
|
310
|
+
</Controller>
|
|
311
|
+
</Field>
|
|
312
|
+
<Controller name={`${field}.min`}>
|
|
313
|
+
<DateInput
|
|
314
|
+
inline
|
|
315
|
+
label="Min value"
|
|
316
|
+
allowClear
|
|
317
|
+
placeholder="Minimum value..."
|
|
318
|
+
decorateEndShowWhenEmpty
|
|
319
|
+
decorateEnd={
|
|
320
|
+
<span className="text-sm text-muted/50 px-1">OPTIONAL</span>
|
|
321
|
+
}
|
|
322
|
+
/>
|
|
323
|
+
</Controller>
|
|
324
|
+
<Controller name={`${field}.max`}>
|
|
325
|
+
<DateInput
|
|
326
|
+
inline
|
|
327
|
+
label="Max value"
|
|
328
|
+
allowClear
|
|
329
|
+
placeholder="Maximum value..."
|
|
330
|
+
decorateEndShowWhenEmpty
|
|
331
|
+
decorateEnd={
|
|
332
|
+
<span className="text-sm text-muted/50 px-1">OPTIONAL</span>
|
|
333
|
+
}
|
|
334
|
+
/>
|
|
335
|
+
</Controller>
|
|
336
|
+
</Fragment>
|
|
337
|
+
)}
|
|
338
|
+
<Field label=" " inline plain>
|
|
339
|
+
<Controller name={`${field}.required`}>
|
|
340
|
+
<Checkbox label="Required" />
|
|
341
|
+
</Controller>
|
|
342
|
+
{type !== DATA_TYPES.AVATAR && (
|
|
343
|
+
<Controller name={`${field}.multiple`}>
|
|
344
|
+
<Checkbox label="Multiple" onChange={setMultiple} />
|
|
345
|
+
</Controller>
|
|
346
|
+
)}
|
|
347
|
+
{hasOptions && (
|
|
348
|
+
<Controller name={`${field}.allowCustom`}>
|
|
349
|
+
<Checkbox label="Allow custom value" />
|
|
350
|
+
</Controller>
|
|
351
|
+
)}
|
|
352
|
+
</Field>
|
|
353
|
+
{multiple && (
|
|
354
|
+
<Fragment>
|
|
355
|
+
<Controller name={`${field}.minItems`}>
|
|
356
|
+
<Number
|
|
357
|
+
inline
|
|
358
|
+
label="Min items"
|
|
359
|
+
allowClear
|
|
360
|
+
placeholder="Minimum items..."
|
|
361
|
+
decorateEndShowWhenEmpty
|
|
362
|
+
decorateEnd={
|
|
363
|
+
<span className="text-sm text-muted/50 px-1">OPTIONAL</span>
|
|
364
|
+
}
|
|
365
|
+
/>
|
|
366
|
+
</Controller>
|
|
367
|
+
<Controller name={`${field}.maxItems`}>
|
|
368
|
+
<Number
|
|
369
|
+
inline
|
|
370
|
+
label="Max items"
|
|
371
|
+
allowClear
|
|
372
|
+
placeholder="Maximum items..."
|
|
373
|
+
decorateEndShowWhenEmpty
|
|
374
|
+
decorateEnd={
|
|
375
|
+
<span className="text-sm text-muted/50 px-1">OPTIONAL</span>
|
|
376
|
+
}
|
|
377
|
+
/>
|
|
378
|
+
</Controller>
|
|
379
|
+
</Fragment>
|
|
380
|
+
)}
|
|
381
|
+
</div>
|
|
382
|
+
</Collapsable>
|
|
383
|
+
);
|
|
384
|
+
};
|
|
385
|
+
|
|
386
|
+
export const SchemaEditor = ({
|
|
387
|
+
schemaDef,
|
|
388
|
+
allowRemove,
|
|
389
|
+
acceptableTypes,
|
|
390
|
+
optionLists = EMPTY_ARRAY,
|
|
391
|
+
children,
|
|
392
|
+
onChange,
|
|
393
|
+
}: Props) => {
|
|
394
|
+
const formRef = useRef<FormRef<KeyValue>>(null);
|
|
395
|
+
const [schemaFields, setSchemaFields] = useState<string[]>([]);
|
|
396
|
+
|
|
397
|
+
useEffect(() => {
|
|
398
|
+
const schemaMap = Object.fromEntries(
|
|
399
|
+
schemaDef.map((schema) => [schema.id, schema]),
|
|
400
|
+
);
|
|
401
|
+
|
|
402
|
+
const currentModel: KeyValue = formRef.current?.getValues() ?? {};
|
|
403
|
+
|
|
404
|
+
const schema = Object.fromEntries(
|
|
405
|
+
Object.keys(schemaMap).map((field) => [
|
|
406
|
+
field,
|
|
407
|
+
currentModel[field] ?? schemaMap[field],
|
|
408
|
+
]),
|
|
409
|
+
);
|
|
410
|
+
formRef.current?.setValues(schema);
|
|
411
|
+
setTimeout(() => {
|
|
412
|
+
setSchemaFields(Object.keys(schema));
|
|
413
|
+
}, 100);
|
|
414
|
+
}, [schemaDef]);
|
|
415
|
+
|
|
416
|
+
const handleChange = useCallback((model: KeyValue<SchemaDef>) => {
|
|
417
|
+
onChange?.(Object.values(model));
|
|
418
|
+
}, []);
|
|
419
|
+
|
|
420
|
+
const handleTypeChange = useCallback((field: string, val: KeyValue) => {
|
|
421
|
+
formRef.current?.setValue(field, val);
|
|
422
|
+
}, []);
|
|
423
|
+
|
|
424
|
+
const handleRemove = useCallback((field: string) => {
|
|
425
|
+
const newSchema: KeyValue = formRef.current?.getValues() ?? {};
|
|
426
|
+
// eslint-disable-next-line @typescript-eslint/no-dynamic-delete
|
|
427
|
+
delete newSchema[field];
|
|
428
|
+
formRef.current?.setValues(newSchema);
|
|
429
|
+
setTimeout(() => {
|
|
430
|
+
setSchemaFields(Object.keys(newSchema));
|
|
431
|
+
}, 100);
|
|
432
|
+
}, []);
|
|
433
|
+
|
|
434
|
+
return (
|
|
435
|
+
<Form formRef={formRef} onSubmit={handleChange}>
|
|
436
|
+
<Content className="divide-y">
|
|
437
|
+
{schemaFields.map((field) => (
|
|
438
|
+
<SchemaField
|
|
439
|
+
key={field}
|
|
440
|
+
field={field}
|
|
441
|
+
allowRemove={allowRemove}
|
|
442
|
+
optionLists={optionLists}
|
|
443
|
+
onRemove={() => handleRemove(field)}
|
|
444
|
+
getModel={() => formRef.current?.getValues()[field]}
|
|
445
|
+
onTypeChange={(val: KeyValue) => handleTypeChange(field, val)}
|
|
446
|
+
acceptableTypes={acceptableTypes ?? Object.values(DATA_TYPES)}
|
|
447
|
+
/>
|
|
448
|
+
))}
|
|
449
|
+
</Content>
|
|
450
|
+
{children && (
|
|
451
|
+
<Footer flex className="border-t bg-base px-6 py-2">
|
|
452
|
+
{children}
|
|
453
|
+
</Footer>
|
|
454
|
+
)}
|
|
455
|
+
</Form>
|
|
456
|
+
);
|
|
457
|
+
};
|
package/src/form/Controller.tsx
CHANGED
|
@@ -46,7 +46,7 @@ export interface ControllerProps {
|
|
|
46
46
|
disabled?: boolean;
|
|
47
47
|
onBlur: () => void;
|
|
48
48
|
onChange: (value: AnyObject) => void;
|
|
49
|
-
|
|
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
|
-
|
|
89
|
+
ref: (el: AnyObject) => {
|
|
90
90
|
el && ref(el);
|
|
91
91
|
},
|
|
92
92
|
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,
|
|
@@ -47,6 +47,7 @@ export interface FormRef<K extends KeyValue> {
|
|
|
47
47
|
submit: () => void;
|
|
48
48
|
validate: () => Promise<boolean>;
|
|
49
49
|
getValues: () => K;
|
|
50
|
+
removeField: (field: string) => void;
|
|
50
51
|
setValue: (key: keyof K, value: AnyObject) => void;
|
|
51
52
|
setValues: (values: K) => void;
|
|
52
53
|
}
|
|
@@ -77,7 +78,7 @@ export const Form = <K extends KeyValue>({
|
|
|
77
78
|
formRef,
|
|
78
79
|
schema,
|
|
79
80
|
children,
|
|
80
|
-
defaultValues,
|
|
81
|
+
defaultValues = EMPTY_OBJECT as K,
|
|
81
82
|
onSubmit = DEFAULT_SUBMIT,
|
|
82
83
|
onChange,
|
|
83
84
|
...rest
|
|
@@ -85,14 +86,11 @@ export const Form = <K extends KeyValue>({
|
|
|
85
86
|
const ref = useRef<HTMLFormElement>(null);
|
|
86
87
|
const form = useForm<K>({
|
|
87
88
|
shouldFocusError: true,
|
|
89
|
+
shouldUnregister: true,
|
|
88
90
|
resolver: schema && (yupResolver(schema) as AnyObject),
|
|
89
91
|
defaultValues: defaultValues as DefaultValues<K>,
|
|
90
92
|
});
|
|
91
93
|
|
|
92
|
-
useEffect(() => {
|
|
93
|
-
form.reset();
|
|
94
|
-
}, []);
|
|
95
|
-
|
|
96
94
|
const [, startTransition] = useTransition();
|
|
97
95
|
const changeHandler = useDebounce(onChange, [onChange]);
|
|
98
96
|
useEffect(() => {
|
|
@@ -123,6 +121,8 @@ export const Form = <K extends KeyValue>({
|
|
|
123
121
|
validate: async () => await form.trigger(),
|
|
124
122
|
getValues: () => form.getValues(),
|
|
125
123
|
setValues: (v) => setTimeout(() => form.reset(v), 50),
|
|
124
|
+
removeField: (field: AnyObject) =>
|
|
125
|
+
form.unregister(field, { keepDefaultValue: false, keepValue: false }),
|
|
126
126
|
setValue: (k, v) =>
|
|
127
127
|
setTimeout(
|
|
128
128
|
() =>
|
|
@@ -21,38 +21,47 @@
|
|
|
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 { EmptyContent } from "@react-fabric/core";
|
|
24
25
|
import { isEmpty, isNil, yup } from "@react-fabric/utilities";
|
|
25
26
|
import { Fragment, useMemo } from "react";
|
|
26
27
|
import { FormField } from "../builder/FormField";
|
|
27
|
-
import { type
|
|
28
|
+
import { type BaseSelectProps } from "../types";
|
|
29
|
+
import { DATA_TYPES, type FormSchema, type SchemaDef } from "../types/schema";
|
|
28
30
|
import { type UploadHandler } from "./useFileUploader";
|
|
29
31
|
|
|
30
|
-
const makeYupField = (field:
|
|
31
|
-
let yupField: AnyObject = yup.string();
|
|
32
|
-
if (field.datatype ===
|
|
32
|
+
const makeYupField = (field: SchemaDef) => {
|
|
33
|
+
let yupField: AnyObject = yup.string().trim();
|
|
34
|
+
if (field.datatype === DATA_TYPES.SCHEMA) {
|
|
33
35
|
const child: yup.ObjectShape = Object.fromEntries(
|
|
34
36
|
field.schema.map((field) => [field.id, makeYupField(field)]),
|
|
35
37
|
);
|
|
36
38
|
yupField = yup.object(child);
|
|
37
39
|
}
|
|
38
|
-
if (field.datatype ===
|
|
40
|
+
if (field.datatype === DATA_TYPES.BOOL) {
|
|
41
|
+
yupField = yup.boolean();
|
|
42
|
+
if (field.required)
|
|
43
|
+
yupField = yupField.test({
|
|
44
|
+
test: (value: boolean) => !!value,
|
|
45
|
+
});
|
|
46
|
+
}
|
|
39
47
|
if (
|
|
40
|
-
field.datatype ===
|
|
41
|
-
field.datatype ===
|
|
42
|
-
field.datatype === "range"
|
|
48
|
+
field.datatype === DATA_TYPES.NUMBER ||
|
|
49
|
+
field.datatype === DATA_TYPES.RANGE
|
|
43
50
|
) {
|
|
44
51
|
yupField = yup.number();
|
|
45
52
|
!isNil(field.min) && (yupField = yupField.min(field.min));
|
|
46
53
|
!isNil(field.max) && (yupField = yupField.max(field.max));
|
|
47
54
|
|
|
48
|
-
if (field.datatype ===
|
|
55
|
+
if (field.datatype === DATA_TYPES.RANGE) yupField = yup.array(yupField);
|
|
49
56
|
}
|
|
50
|
-
if (field.datatype ===
|
|
51
|
-
field.options &&
|
|
57
|
+
if (field.datatype === DATA_TYPES.STRING) {
|
|
58
|
+
field.options &&
|
|
59
|
+
!field.allowCustom &&
|
|
60
|
+
(yupField = yupField.oneOf(field.options));
|
|
52
61
|
field.regex && (yupField = yupField.matches(new RegExp(field.regex)));
|
|
53
62
|
}
|
|
54
63
|
|
|
55
|
-
if (field.datatype ===
|
|
64
|
+
if (field.datatype === DATA_TYPES.FILE) {
|
|
56
65
|
yupField = yup
|
|
57
66
|
.object({
|
|
58
67
|
filename: yup.string().required(),
|
|
@@ -83,13 +92,14 @@ const makeYupField = (field: FormSchema) => {
|
|
|
83
92
|
return yupField.label(field.label);
|
|
84
93
|
};
|
|
85
94
|
|
|
86
|
-
export const useFormBuilder = (
|
|
87
|
-
schema: FormSchema
|
|
95
|
+
export const useFormBuilder = <T extends AnyObject = string>(
|
|
96
|
+
schema: FormSchema,
|
|
88
97
|
options: {
|
|
89
98
|
prefix?: string;
|
|
90
99
|
inline?: boolean;
|
|
91
100
|
fileUrl?: (path: string) => string;
|
|
92
101
|
uploadHandler?: UploadHandler;
|
|
102
|
+
optionLists?: KeyValue<BaseSelectProps<T>>;
|
|
93
103
|
} = {},
|
|
94
104
|
) => {
|
|
95
105
|
const filteredSchema = useMemo(
|
|
@@ -106,11 +116,12 @@ export const useFormBuilder = (
|
|
|
106
116
|
<Fragment>
|
|
107
117
|
{filteredSchema.map((field, idx) => (
|
|
108
118
|
<div className="mb-4" key={field.id}>
|
|
109
|
-
{field.datatype ===
|
|
119
|
+
{field.datatype === DATA_TYPES.SCHEMA ? null : (
|
|
110
120
|
<FormField
|
|
111
121
|
{...field}
|
|
112
122
|
autoFocus={idx === 0}
|
|
113
123
|
fileUrl={options.fileUrl}
|
|
124
|
+
optionLists={options.optionLists}
|
|
114
125
|
uploadHandler={options.uploadHandler}
|
|
115
126
|
inline={options.inline}
|
|
116
127
|
prefix={options.prefix}
|
|
@@ -118,6 +129,9 @@ export const useFormBuilder = (
|
|
|
118
129
|
)}
|
|
119
130
|
</div>
|
|
120
131
|
))}
|
|
132
|
+
{filteredSchema.length === 0 && (
|
|
133
|
+
<EmptyContent message="No schema defined" />
|
|
134
|
+
)}
|
|
121
135
|
</Fragment>
|
|
122
136
|
);
|
|
123
137
|
|
package/src/index.ts
CHANGED
|
@@ -26,15 +26,18 @@ import "./i18n";
|
|
|
26
26
|
export { Controller } from "./form/Controller";
|
|
27
27
|
export { Form, type FormRef } from "./form/Form";
|
|
28
28
|
|
|
29
|
+
export { SchemaEditor } from "./builder/SchemaEditor";
|
|
30
|
+
|
|
29
31
|
export { useFileUploader } from "./hooks/useFileUploader";
|
|
30
32
|
export { useFormBuilder } from "./hooks/useFormBuilder";
|
|
31
33
|
|
|
32
|
-
export { type FormSchema } from "./types/schema";
|
|
34
|
+
export { DATA_TYPES, type FormSchema, type SchemaDef } from "./types/schema";
|
|
33
35
|
|
|
34
36
|
export { ArrayInput } from "./input/ArrayInput";
|
|
35
37
|
export { Checkbox } from "./input/Checkbox";
|
|
36
38
|
export { ColorInput } from "./input/ColorInput";
|
|
37
39
|
export { ColorPicker } from "./input/ColorPicker";
|
|
40
|
+
export { DateInput } from "./input/DateInput";
|
|
38
41
|
export { Field } from "./input/Field";
|
|
39
42
|
export { FileInput } from "./input/File";
|
|
40
43
|
export { HiddenInput } from "./input/Hidden";
|
|
@@ -43,7 +46,7 @@ export { Number } from "./input/Number";
|
|
|
43
46
|
export { Password } from "./input/Password";
|
|
44
47
|
export { Radio } from "./input/Radio";
|
|
45
48
|
export { RadioGroup } from "./input/RadioGroup";
|
|
46
|
-
export {
|
|
49
|
+
export { RangeSlider } from "./input/RangeSlider";
|
|
47
50
|
export { Search } from "./input/Search";
|
|
48
51
|
export { Slider } from "./input/Slider";
|
|
49
52
|
export { Switch } from "./input/Switch";
|
package/src/input/Checkbox.tsx
CHANGED
|
@@ -34,7 +34,7 @@ import {
|
|
|
34
34
|
} from "react";
|
|
35
35
|
import { ErrorIcon } from "../internal/ErrorIcon";
|
|
36
36
|
|
|
37
|
-
|
|
37
|
+
interface CheckboxProps extends RefProp<HTMLInputElement> {
|
|
38
38
|
/**
|
|
39
39
|
* checkbox label
|
|
40
40
|
*/
|
|
@@ -50,8 +50,7 @@ export interface CheckboxProps extends RefProp<HTMLInputElement> {
|
|
|
50
50
|
/**
|
|
51
51
|
* on checked change
|
|
52
52
|
*/
|
|
53
|
-
onChange?: (checked: boolean
|
|
54
|
-
|
|
53
|
+
onChange?: (checked: boolean) => void;
|
|
55
54
|
/**
|
|
56
55
|
* disabled input
|
|
57
56
|
*/
|
|
@@ -94,6 +93,8 @@ export const Checkbox = ({
|
|
|
94
93
|
width,
|
|
95
94
|
icon,
|
|
96
95
|
iconChecked,
|
|
96
|
+
// @ts-expect-error ignore
|
|
97
|
+
required,
|
|
97
98
|
onChange,
|
|
98
99
|
...rest
|
|
99
100
|
}: CheckboxProps) => {
|