@react-fabric/form 1.0.0-beta.33 → 1.0.0-beta.35
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/FormField.js +3 -3
- package/dist/cjs/builder/SchemaEditor.js +43 -404
- package/dist/cjs/builder/components/AddSchemaFields.js +62 -0
- package/dist/cjs/builder/components/SchemaArrayForm.js +144 -0
- package/dist/cjs/builder/components/SchemaField.js +411 -0
- package/dist/cjs/form/Form.js +11 -8
- package/dist/cjs/index.js +1 -0
- package/dist/cjs/input/ColorInput.js +1 -1
- package/dist/cjs/input/DateInput.js +2 -2
- 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/RangeSlider.js +1 -1
- package/dist/cjs/input/Search.js +1 -1
- package/dist/cjs/input/Slider.js +1 -1
- package/dist/cjs/input/Textarea.js +1 -1
- package/dist/cjs/types/schema.js +2 -0
- package/dist/esm/builder/FormField.js +3 -3
- package/dist/esm/builder/SchemaEditor.js +45 -406
- package/dist/esm/builder/components/AddSchemaFields.js +62 -0
- package/dist/esm/builder/components/SchemaArrayForm.js +144 -0
- package/dist/esm/builder/components/SchemaField.js +411 -0
- package/dist/esm/form/Form.js +11 -8
- package/dist/esm/index.js +1 -1
- package/dist/esm/input/ColorInput.js +1 -1
- package/dist/esm/input/DateInput.js +2 -2
- 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/RangeSlider.js +1 -1
- package/dist/esm/input/Search.js +1 -1
- package/dist/esm/input/Slider.js +1 -1
- package/dist/esm/input/Textarea.js +1 -1
- package/dist/esm/types/schema.js +2 -1
- package/dist/types/builder/SchemaEditor.d.ts +2 -10
- package/dist/types/builder/components/AddSchemaFields.d.ts +2 -0
- package/dist/types/builder/components/SchemaArrayForm.d.ts +2 -0
- package/dist/types/builder/components/SchemaField.d.ts +10 -0
- package/dist/types/form/Form.d.ts +1 -5
- package/dist/types/index.d.ts +1 -1
- package/dist/types/input/ColorInput.d.ts +1 -1
- package/dist/types/types/index.d.ts +1 -1
- package/dist/types/types/schema.d.ts +26 -0
- package/package.json +5 -5
- package/src/builder/FormField.tsx +3 -3
- package/src/builder/SchemaEditor.tsx +24 -398
- package/src/builder/components/AddSchemaFields.tsx +74 -0
- package/src/builder/components/SchemaArrayForm.tsx +158 -0
- package/src/builder/components/SchemaField.tsx +432 -0
- package/src/form/Form.tsx +6 -9
- package/src/index.ts +6 -1
- package/src/input/ArrayInput.tsx +1 -0
- package/src/input/ColorInput.tsx +5 -2
- package/src/input/DateInput.tsx +2 -2
- 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/RangeSlider.tsx +1 -1
- package/src/input/Search.tsx +1 -1
- package/src/input/Slider.tsx +1 -1
- package/src/input/Textarea.tsx +1 -1
- package/src/types/index.ts +1 -1
- package/src/types/schema.ts +30 -0
|
@@ -0,0 +1,432 @@
|
|
|
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((type: DATA_TYPES) => {
|
|
172
|
+
if (type) {
|
|
173
|
+
const modelRef = getModel();
|
|
174
|
+
let reset = {
|
|
175
|
+
datatype: type,
|
|
176
|
+
id: field,
|
|
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
|
+
type: "date",
|
|
193
|
+
} as DateSchema);
|
|
194
|
+
} else if (type === DATA_TYPES.NUMBER || type === DATA_TYPES.RANGE) {
|
|
195
|
+
reset = Object.assign(reset, {
|
|
196
|
+
min: modelRef.min,
|
|
197
|
+
max: modelRef.max,
|
|
198
|
+
step: modelRef.step,
|
|
199
|
+
} as NumberSchema);
|
|
200
|
+
}
|
|
201
|
+
if (type === DATA_TYPES.AVATAR) {
|
|
202
|
+
setMultiple(false);
|
|
203
|
+
}
|
|
204
|
+
onTypeChange(reset);
|
|
205
|
+
setType(type);
|
|
206
|
+
}
|
|
207
|
+
}, []);
|
|
208
|
+
|
|
209
|
+
return (
|
|
210
|
+
<SortableItem id={fieldId} className="flex flex-nowrap gap-1 group/field">
|
|
211
|
+
<Collapsable key={fieldId} open={defaultOpen} className="flex-1">
|
|
212
|
+
<div className="flex flex-nowrap items-center">
|
|
213
|
+
<span className="flex-1 truncate">{id}</span>
|
|
214
|
+
{dynamic && (
|
|
215
|
+
<Button
|
|
216
|
+
size="sm"
|
|
217
|
+
color="danger"
|
|
218
|
+
variant="link"
|
|
219
|
+
aria-label="Delete"
|
|
220
|
+
stopPropagation
|
|
221
|
+
icon={CoreIcons.trash}
|
|
222
|
+
onClick={onRemove}
|
|
223
|
+
className="invisible group-hover/field:visible"
|
|
224
|
+
/>
|
|
225
|
+
)}
|
|
226
|
+
</div>
|
|
227
|
+
<div className="flex flex-col gap-1 pb-4">
|
|
228
|
+
<Controller name={`${field}.id`}>
|
|
229
|
+
<HiddenInput hiddenValue={id} />
|
|
230
|
+
</Controller>
|
|
231
|
+
<Controller name={`${field}.datatype`}>
|
|
232
|
+
<Select
|
|
233
|
+
inline
|
|
234
|
+
label="Type"
|
|
235
|
+
placeholder="Data type"
|
|
236
|
+
options={acceptableTypes}
|
|
237
|
+
onChange={(val) => handleTypeChange(val as unknown as DATA_TYPES)}
|
|
238
|
+
/>
|
|
239
|
+
</Controller>
|
|
240
|
+
<Controller name={`${field}.label`}>
|
|
241
|
+
<Input
|
|
242
|
+
inline
|
|
243
|
+
label="Label"
|
|
244
|
+
allowClear
|
|
245
|
+
placeholder="Label..."
|
|
246
|
+
decorateEndShowWhenEmpty
|
|
247
|
+
decorateEnd={
|
|
248
|
+
<span className="text-sm text-muted/50 px-1">OPTIONAL</span>
|
|
249
|
+
}
|
|
250
|
+
/>
|
|
251
|
+
</Controller>
|
|
252
|
+
{type === DATA_TYPES.STRING && (
|
|
253
|
+
<Fragment>
|
|
254
|
+
<Controller name={`${field}.regex`}>
|
|
255
|
+
<Input
|
|
256
|
+
inline
|
|
257
|
+
label="Regex validation"
|
|
258
|
+
allowClear
|
|
259
|
+
placeholder="Regex value..."
|
|
260
|
+
decorateEndShowWhenEmpty
|
|
261
|
+
decorateEnd={
|
|
262
|
+
<span className="text-sm text-muted/50 px-1">OPTIONAL</span>
|
|
263
|
+
}
|
|
264
|
+
/>
|
|
265
|
+
</Controller>
|
|
266
|
+
<Field inline label="Option list">
|
|
267
|
+
<Controller name={`${field}.optionList`}>
|
|
268
|
+
<Select
|
|
269
|
+
allowClear
|
|
270
|
+
options={[SCHEMA_CUSTOM_LIST, ...optionLists]}
|
|
271
|
+
onChange={(v) => setCustomOptions(v === SCHEMA_CUSTOM_LIST)}
|
|
272
|
+
/>
|
|
273
|
+
</Controller>
|
|
274
|
+
{customOptions && (
|
|
275
|
+
<Button
|
|
276
|
+
icon={CoreIcons.list}
|
|
277
|
+
aria-label="Custom options"
|
|
278
|
+
onClick={() => {
|
|
279
|
+
void openOptionModal();
|
|
280
|
+
}}
|
|
281
|
+
/>
|
|
282
|
+
)}
|
|
283
|
+
</Field>
|
|
284
|
+
{OptionModal}
|
|
285
|
+
</Fragment>
|
|
286
|
+
)}
|
|
287
|
+
{type === DATA_TYPES.FILE && (
|
|
288
|
+
<Fragment>
|
|
289
|
+
<Controller name={`${field}.accept`}>
|
|
290
|
+
<Input
|
|
291
|
+
inline
|
|
292
|
+
label="Accept types"
|
|
293
|
+
allowClear
|
|
294
|
+
placeholder="Accept value..."
|
|
295
|
+
decorateEndShowWhenEmpty
|
|
296
|
+
decorateEnd={
|
|
297
|
+
<span className="text-sm text-muted/50 px-1">OPTIONAL</span>
|
|
298
|
+
}
|
|
299
|
+
/>
|
|
300
|
+
</Controller>
|
|
301
|
+
</Fragment>
|
|
302
|
+
)}
|
|
303
|
+
{[DATA_TYPES.NUMBER, DATA_TYPES.RANGE].includes(
|
|
304
|
+
type as unknown as DATA_TYPES,
|
|
305
|
+
) && (
|
|
306
|
+
<Fragment>
|
|
307
|
+
<Controller name={`${field}.min`}>
|
|
308
|
+
<Number
|
|
309
|
+
inline
|
|
310
|
+
label="Min value"
|
|
311
|
+
allowClear
|
|
312
|
+
placeholder="Minimum value..."
|
|
313
|
+
decorateEndShowWhenEmpty
|
|
314
|
+
decorateEnd={
|
|
315
|
+
<span className="text-sm text-muted/50 px-1">OPTIONAL</span>
|
|
316
|
+
}
|
|
317
|
+
/>
|
|
318
|
+
</Controller>
|
|
319
|
+
<Controller name={`${field}.max`}>
|
|
320
|
+
<Number
|
|
321
|
+
inline
|
|
322
|
+
label="Max value"
|
|
323
|
+
allowClear
|
|
324
|
+
placeholder="Maximum value..."
|
|
325
|
+
decorateEndShowWhenEmpty
|
|
326
|
+
decorateEnd={
|
|
327
|
+
<span className="text-sm text-muted/50 px-1">OPTIONAL</span>
|
|
328
|
+
}
|
|
329
|
+
/>
|
|
330
|
+
</Controller>
|
|
331
|
+
<Controller name={`${field}.step`}>
|
|
332
|
+
<Number
|
|
333
|
+
inline
|
|
334
|
+
label="Step value"
|
|
335
|
+
allowClear
|
|
336
|
+
placeholder="Step value..."
|
|
337
|
+
decorateEndShowWhenEmpty
|
|
338
|
+
decorateEnd={
|
|
339
|
+
<span className="text-sm text-muted/50 px-1">OPTIONAL</span>
|
|
340
|
+
}
|
|
341
|
+
/>
|
|
342
|
+
</Controller>
|
|
343
|
+
</Fragment>
|
|
344
|
+
)}
|
|
345
|
+
{type === DATA_TYPES.DATE && (
|
|
346
|
+
<Fragment>
|
|
347
|
+
<Field inline label="Type" plain>
|
|
348
|
+
<Controller name={`${field}.type`}>
|
|
349
|
+
<ToggleButtonGroup value="" fullWidth className="flex-1">
|
|
350
|
+
<Button className="flex-1" value="date">
|
|
351
|
+
Date
|
|
352
|
+
</Button>
|
|
353
|
+
<Button className="flex-1" value="datetime">
|
|
354
|
+
Date Time
|
|
355
|
+
</Button>
|
|
356
|
+
</ToggleButtonGroup>
|
|
357
|
+
</Controller>
|
|
358
|
+
</Field>
|
|
359
|
+
<Controller name={`${field}.min`}>
|
|
360
|
+
<DateInput
|
|
361
|
+
inline
|
|
362
|
+
label="Min value"
|
|
363
|
+
allowClear
|
|
364
|
+
placeholder="Minimum value..."
|
|
365
|
+
decorateEndShowWhenEmpty
|
|
366
|
+
decorateEnd={
|
|
367
|
+
<span className="text-sm text-muted/50 px-1">OPTIONAL</span>
|
|
368
|
+
}
|
|
369
|
+
/>
|
|
370
|
+
</Controller>
|
|
371
|
+
<Controller name={`${field}.max`}>
|
|
372
|
+
<DateInput
|
|
373
|
+
inline
|
|
374
|
+
label="Max value"
|
|
375
|
+
allowClear
|
|
376
|
+
placeholder="Maximum value..."
|
|
377
|
+
decorateEndShowWhenEmpty
|
|
378
|
+
decorateEnd={
|
|
379
|
+
<span className="text-sm text-muted/50 px-1">OPTIONAL</span>
|
|
380
|
+
}
|
|
381
|
+
/>
|
|
382
|
+
</Controller>
|
|
383
|
+
</Fragment>
|
|
384
|
+
)}
|
|
385
|
+
<Field label=" " inline plain>
|
|
386
|
+
<Controller name={`${field}.required`}>
|
|
387
|
+
<Checkbox label="Required" />
|
|
388
|
+
</Controller>
|
|
389
|
+
{type !== DATA_TYPES.AVATAR && (
|
|
390
|
+
<Controller name={`${field}.multiple`}>
|
|
391
|
+
<Checkbox label="Multiple" onChange={setMultiple} />
|
|
392
|
+
</Controller>
|
|
393
|
+
)}
|
|
394
|
+
{hasOptions && (
|
|
395
|
+
<Controller name={`${field}.allowCustom`}>
|
|
396
|
+
<Checkbox label="Allow custom value" />
|
|
397
|
+
</Controller>
|
|
398
|
+
)}
|
|
399
|
+
</Field>
|
|
400
|
+
{multiple && (
|
|
401
|
+
<Fragment>
|
|
402
|
+
<Controller name={`${field}.minItems`}>
|
|
403
|
+
<Number
|
|
404
|
+
inline
|
|
405
|
+
label="Min items"
|
|
406
|
+
allowClear
|
|
407
|
+
placeholder="Minimum items..."
|
|
408
|
+
decorateEndShowWhenEmpty
|
|
409
|
+
decorateEnd={
|
|
410
|
+
<span className="text-sm text-muted/50 px-1">OPTIONAL</span>
|
|
411
|
+
}
|
|
412
|
+
/>
|
|
413
|
+
</Controller>
|
|
414
|
+
<Controller name={`${field}.maxItems`}>
|
|
415
|
+
<Number
|
|
416
|
+
inline
|
|
417
|
+
label="Max items"
|
|
418
|
+
allowClear
|
|
419
|
+
placeholder="Maximum items..."
|
|
420
|
+
decorateEndShowWhenEmpty
|
|
421
|
+
decorateEnd={
|
|
422
|
+
<span className="text-sm text-muted/50 px-1">OPTIONAL</span>
|
|
423
|
+
}
|
|
424
|
+
/>
|
|
425
|
+
</Controller>
|
|
426
|
+
</Fragment>
|
|
427
|
+
)}
|
|
428
|
+
</div>
|
|
429
|
+
</Collapsable>
|
|
430
|
+
</SortableItem>
|
|
431
|
+
);
|
|
432
|
+
};
|
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,
|
|
@@ -57,10 +57,6 @@ 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;
|
|
64
60
|
/**
|
|
65
61
|
* default data values
|
|
66
62
|
*/
|
|
@@ -81,15 +77,13 @@ export const Form = <K extends KeyValue>({
|
|
|
81
77
|
formRef,
|
|
82
78
|
schema,
|
|
83
79
|
children,
|
|
84
|
-
|
|
85
|
-
defaultValues,
|
|
80
|
+
defaultValues = EMPTY_OBJECT as K,
|
|
86
81
|
onSubmit = DEFAULT_SUBMIT,
|
|
87
82
|
onChange,
|
|
88
83
|
...rest
|
|
89
84
|
}: PropsWithChildren<FormProps<K>>) => {
|
|
90
85
|
const ref = useRef<HTMLFormElement>(null);
|
|
91
86
|
const form = useForm<K>({
|
|
92
|
-
values,
|
|
93
87
|
shouldFocusError: true,
|
|
94
88
|
resolver: schema && (yupResolver(schema) as AnyObject),
|
|
95
89
|
defaultValues: defaultValues as DefaultValues<K>,
|
|
@@ -156,7 +150,10 @@ export const Form = <K extends KeyValue>({
|
|
|
156
150
|
<form
|
|
157
151
|
ref={ref}
|
|
158
152
|
onReset={() => handleReset(defaultValues)}
|
|
159
|
-
onSubmit={
|
|
153
|
+
onSubmit={(e) => {
|
|
154
|
+
e.stopPropagation();
|
|
155
|
+
void form.handleSubmit(onSubmit, onInvalid)(e);
|
|
156
|
+
}}
|
|
160
157
|
data-loading={form.formState.isSubmitting}
|
|
161
158
|
className="contents"
|
|
162
159
|
autoComplete="off"
|
package/src/index.ts
CHANGED
|
@@ -31,7 +31,12 @@ export { SchemaEditor } from "./builder/SchemaEditor";
|
|
|
31
31
|
export { useFileUploader } from "./hooks/useFileUploader";
|
|
32
32
|
export { useFormBuilder } from "./hooks/useFormBuilder";
|
|
33
33
|
|
|
34
|
-
export {
|
|
34
|
+
export {
|
|
35
|
+
DATA_TYPES,
|
|
36
|
+
SCHEMA_CUSTOM_LIST,
|
|
37
|
+
type FormSchema,
|
|
38
|
+
type SchemaDef,
|
|
39
|
+
} from "./types/schema";
|
|
35
40
|
|
|
36
41
|
export { ArrayInput } from "./input/ArrayInput";
|
|
37
42
|
export { Checkbox } from "./input/Checkbox";
|
package/src/input/ArrayInput.tsx
CHANGED
package/src/input/ColorInput.tsx
CHANGED
|
@@ -48,7 +48,10 @@ import {
|
|
|
48
48
|
import { type InputProps } from "../types";
|
|
49
49
|
import { ColorPicker, type ColorProps } from "./ColorPicker";
|
|
50
50
|
|
|
51
|
-
export interface ColorInputProps
|
|
51
|
+
export interface ColorInputProps
|
|
52
|
+
extends InputProps,
|
|
53
|
+
Omit<ColorProps, "onChange">,
|
|
54
|
+
RefProp {
|
|
52
55
|
/**
|
|
53
56
|
* show eydrop picker
|
|
54
57
|
*/
|
|
@@ -82,7 +85,7 @@ export const ColorInput = ({
|
|
|
82
85
|
const handleChange = useCallback(
|
|
83
86
|
(e?: string) => {
|
|
84
87
|
setActualValue(e ?? defaultColor);
|
|
85
|
-
onChange != null && startTransition(() => onChange(e));
|
|
88
|
+
onChange != null && startTransition(() => onChange(e ?? null));
|
|
86
89
|
},
|
|
87
90
|
[onChange],
|
|
88
91
|
);
|
package/src/input/DateInput.tsx
CHANGED
|
@@ -120,7 +120,7 @@ export const DateInput = ({
|
|
|
120
120
|
} else {
|
|
121
121
|
setActualValue("");
|
|
122
122
|
setInputValue("");
|
|
123
|
-
onChange != null && startTransition(() => onChange(
|
|
123
|
+
onChange != null && startTransition(() => onChange(null));
|
|
124
124
|
}
|
|
125
125
|
},
|
|
126
126
|
[onChange, inputFormat],
|
|
@@ -137,7 +137,7 @@ export const DateInput = ({
|
|
|
137
137
|
} else {
|
|
138
138
|
setActualValue("");
|
|
139
139
|
setInputValue("");
|
|
140
|
-
onChange != null && startTransition(() => onChange(
|
|
140
|
+
onChange != null && startTransition(() => onChange(null));
|
|
141
141
|
}
|
|
142
142
|
},
|
|
143
143
|
[onChange, inputFormat],
|
package/src/input/File.tsx
CHANGED
|
@@ -134,7 +134,7 @@ export const FileInput = ({
|
|
|
134
134
|
}
|
|
135
135
|
} else {
|
|
136
136
|
setActualValue(e?.target.files?.item(0));
|
|
137
|
-
onChange?.(e?.target.files?.item(0) ??
|
|
137
|
+
onChange?.(e?.target.files?.item(0) ?? null);
|
|
138
138
|
if (!e && inputRef.current) inputRef.current.value = "";
|
|
139
139
|
}
|
|
140
140
|
},
|
package/src/input/Hidden.tsx
CHANGED
|
@@ -54,9 +54,10 @@ export const HiddenInput = ({
|
|
|
54
54
|
name,
|
|
55
55
|
}: Props) => {
|
|
56
56
|
useEffect(() => {
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
57
|
+
hiddenValue &&
|
|
58
|
+
setTimeout(() => {
|
|
59
|
+
onChange?.(hiddenValue);
|
|
60
|
+
}, 50);
|
|
60
61
|
}, [hiddenValue]);
|
|
61
62
|
return (
|
|
62
63
|
<Fragment>
|
package/src/input/Input.tsx
CHANGED
package/src/input/Number.tsx
CHANGED
package/src/input/Password.tsx
CHANGED
package/src/input/Search.tsx
CHANGED
|
@@ -96,7 +96,7 @@ export const Search = ({
|
|
|
96
96
|
const value = e?.target.value;
|
|
97
97
|
refValue.current = value ?? "";
|
|
98
98
|
setActualValue(value ?? "");
|
|
99
|
-
onChange?.(value ??
|
|
99
|
+
onChange?.(value ?? null);
|
|
100
100
|
|
|
101
101
|
// value was cleared so force fire onSearch
|
|
102
102
|
if (searchOnChange) {
|
package/src/input/Slider.tsx
CHANGED
|
@@ -146,7 +146,7 @@ export const Slider = ({
|
|
|
146
146
|
(value: number) => {
|
|
147
147
|
const val = useInfinity && value === max ? Infinity : value;
|
|
148
148
|
setActualValue(val ?? 0);
|
|
149
|
-
onChange?.(val ??
|
|
149
|
+
onChange?.(val ?? null);
|
|
150
150
|
},
|
|
151
151
|
[onChange, max, useInfinity],
|
|
152
152
|
);
|
package/src/input/Textarea.tsx
CHANGED