@connect-soft/form-generator 1.1.0-alpha2 → 1.1.0-alpha4
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/README.md +486 -28
- package/dist/index.js +292 -87
- package/dist/index.js.map +1 -1
- package/dist/{index.m.js → index.mjs} +294 -89
- package/dist/index.mjs.map +1 -0
- package/dist/types/components/form/create-template-fields.d.ts +4 -0
- package/dist/types/components/form/create-template-fields.d.ts.map +1 -0
- package/dist/types/components/form/form-generator.d.ts +33 -12
- package/dist/types/components/form/form-generator.d.ts.map +1 -1
- package/dist/types/components/form/form-utils.d.ts +13 -0
- package/dist/types/components/form/form-utils.d.ts.map +1 -0
- package/dist/types/components/form/index.d.ts +1 -1
- package/dist/types/components/form/index.d.ts.map +1 -1
- package/dist/types/index.d.ts +2 -1
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/lib/index.d.ts +1 -0
- package/dist/types/lib/index.d.ts.map +1 -1
- package/dist/types/lib/template-types.d.ts +54 -0
- package/dist/types/lib/template-types.d.ts.map +1 -0
- package/package.json +11 -25
- package/MIGRATION.md +0 -503
- package/dist/index.m.js.map +0 -1
- package/dist/styles/globals.css +0 -2
package/dist/index.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var compilerRuntime = require('react/compiler-runtime');
|
|
4
3
|
var jsxRuntime = require('react/jsx-runtime');
|
|
5
4
|
var React = require('react');
|
|
6
5
|
var n$1 = require('zod/v4/core');
|
|
6
|
+
var compilerRuntime = require('react/compiler-runtime');
|
|
7
7
|
var zod = require('zod');
|
|
8
8
|
|
|
9
9
|
function _interopNamespaceDefault(e) {
|
|
@@ -3130,6 +3130,105 @@ function clearAllRegistries() {
|
|
|
3130
3130
|
clearLayoutRegistry();
|
|
3131
3131
|
}
|
|
3132
3132
|
|
|
3133
|
+
const RESERVED_PROPS = new Set(['all', 'remaining', 'names', 'has', 'render']);
|
|
3134
|
+
function createTemplateFields(fieldEntries) {
|
|
3135
|
+
const accessedFields = new Set();
|
|
3136
|
+
const handler = {
|
|
3137
|
+
get(_, prop) {
|
|
3138
|
+
if (typeof prop === 'symbol') {
|
|
3139
|
+
return undefined;
|
|
3140
|
+
}
|
|
3141
|
+
if (prop === 'all') {
|
|
3142
|
+
return Array.from(fieldEntries.values()).map(entry => entry.element);
|
|
3143
|
+
}
|
|
3144
|
+
if (prop === 'remaining') {
|
|
3145
|
+
return Array.from(fieldEntries.entries()).filter(([name]) => !accessedFields.has(name)).map(([, entry]) => entry.element);
|
|
3146
|
+
}
|
|
3147
|
+
if (prop === 'names') {
|
|
3148
|
+
return Array.from(fieldEntries.keys());
|
|
3149
|
+
}
|
|
3150
|
+
if (prop === 'has') {
|
|
3151
|
+
return name => fieldEntries.has(name);
|
|
3152
|
+
}
|
|
3153
|
+
if (prop === 'render') {
|
|
3154
|
+
return (...names) => {
|
|
3155
|
+
return names.filter(name => fieldEntries.has(name)).map(name => {
|
|
3156
|
+
accessedFields.add(name);
|
|
3157
|
+
return fieldEntries.get(name).element;
|
|
3158
|
+
});
|
|
3159
|
+
};
|
|
3160
|
+
}
|
|
3161
|
+
if (fieldEntries.has(prop)) {
|
|
3162
|
+
accessedFields.add(prop);
|
|
3163
|
+
return fieldEntries.get(prop).element;
|
|
3164
|
+
}
|
|
3165
|
+
return undefined;
|
|
3166
|
+
},
|
|
3167
|
+
has(_, prop) {
|
|
3168
|
+
if (typeof prop === 'symbol') {
|
|
3169
|
+
return false;
|
|
3170
|
+
}
|
|
3171
|
+
return RESERVED_PROPS.has(prop) || fieldEntries.has(prop);
|
|
3172
|
+
},
|
|
3173
|
+
ownKeys() {
|
|
3174
|
+
return [...RESERVED_PROPS, ...fieldEntries.keys()];
|
|
3175
|
+
},
|
|
3176
|
+
getOwnPropertyDescriptor(_, prop) {
|
|
3177
|
+
if (typeof prop === 'symbol') {
|
|
3178
|
+
return undefined;
|
|
3179
|
+
}
|
|
3180
|
+
if (RESERVED_PROPS.has(prop) || fieldEntries.has(prop)) {
|
|
3181
|
+
return {
|
|
3182
|
+
configurable: true,
|
|
3183
|
+
enumerable: true,
|
|
3184
|
+
value: this.get(_, prop, {})
|
|
3185
|
+
};
|
|
3186
|
+
}
|
|
3187
|
+
return undefined;
|
|
3188
|
+
}
|
|
3189
|
+
};
|
|
3190
|
+
return new Proxy({}, handler);
|
|
3191
|
+
}
|
|
3192
|
+
function createTemplateLayouts(layoutEntries) {
|
|
3193
|
+
const handler = {
|
|
3194
|
+
get(_, prop) {
|
|
3195
|
+
if (typeof prop === 'symbol') {
|
|
3196
|
+
return undefined;
|
|
3197
|
+
}
|
|
3198
|
+
if (prop === 'all') {
|
|
3199
|
+
return Array.from(layoutEntries.values()).map(entry => entry.element);
|
|
3200
|
+
}
|
|
3201
|
+
if (layoutEntries.has(prop)) {
|
|
3202
|
+
return layoutEntries.get(prop).element;
|
|
3203
|
+
}
|
|
3204
|
+
return undefined;
|
|
3205
|
+
},
|
|
3206
|
+
has(_, prop) {
|
|
3207
|
+
if (typeof prop === 'symbol') {
|
|
3208
|
+
return false;
|
|
3209
|
+
}
|
|
3210
|
+
return prop === 'all' || layoutEntries.has(prop);
|
|
3211
|
+
},
|
|
3212
|
+
ownKeys() {
|
|
3213
|
+
return ['all', ...layoutEntries.keys()];
|
|
3214
|
+
},
|
|
3215
|
+
getOwnPropertyDescriptor(_, prop) {
|
|
3216
|
+
if (typeof prop === 'symbol') {
|
|
3217
|
+
return undefined;
|
|
3218
|
+
}
|
|
3219
|
+
if (prop === 'all' || layoutEntries.has(prop)) {
|
|
3220
|
+
return {
|
|
3221
|
+
configurable: true,
|
|
3222
|
+
enumerable: true,
|
|
3223
|
+
value: this.get(_, prop, {})
|
|
3224
|
+
};
|
|
3225
|
+
}
|
|
3226
|
+
return undefined;
|
|
3227
|
+
}
|
|
3228
|
+
};
|
|
3229
|
+
return new Proxy({}, handler);
|
|
3230
|
+
}
|
|
3231
|
+
|
|
3133
3232
|
const FieldRenderer = /*#__PURE__*/React.memo(t0 => {
|
|
3134
3233
|
const $ = compilerRuntime.c(5);
|
|
3135
3234
|
const {
|
|
@@ -3375,8 +3474,9 @@ const defaultValidators = {
|
|
|
3375
3474
|
tel: stringValidator,
|
|
3376
3475
|
number: field => {
|
|
3377
3476
|
let schema = zod.z.coerce.number();
|
|
3378
|
-
|
|
3379
|
-
if (
|
|
3477
|
+
const numberField = field;
|
|
3478
|
+
if (numberField.min !== undefined) schema = schema.min(numberField.min);
|
|
3479
|
+
if (numberField.max !== undefined) schema = schema.max(numberField.max);
|
|
3380
3480
|
return schema;
|
|
3381
3481
|
},
|
|
3382
3482
|
checkbox: () => zod.z.coerce.boolean(),
|
|
@@ -3462,116 +3562,221 @@ function getItemKey(item, index) {
|
|
|
3462
3562
|
}
|
|
3463
3563
|
return item.name;
|
|
3464
3564
|
}
|
|
3565
|
+
function extractFields(items) {
|
|
3566
|
+
const fields = [];
|
|
3567
|
+
for (const item of items) {
|
|
3568
|
+
if (isLayoutBlock(item)) {
|
|
3569
|
+
let childItems = [];
|
|
3570
|
+
if (item.type === 'columns') {
|
|
3571
|
+
for (const column of item.columns) {
|
|
3572
|
+
childItems.push(...column.children);
|
|
3573
|
+
}
|
|
3574
|
+
} else if (item.type === 'section') {
|
|
3575
|
+
childItems = item.children;
|
|
3576
|
+
}
|
|
3577
|
+
fields.push(...extractFields(childItems));
|
|
3578
|
+
} else {
|
|
3579
|
+
fields.push(item);
|
|
3580
|
+
}
|
|
3581
|
+
}
|
|
3582
|
+
return fields;
|
|
3583
|
+
}
|
|
3584
|
+
function extractLayouts(items) {
|
|
3585
|
+
const layouts = [];
|
|
3586
|
+
items.forEach((item, index) => {
|
|
3587
|
+
if (isLayoutBlock(item)) {
|
|
3588
|
+
layouts.push({
|
|
3589
|
+
layout: item,
|
|
3590
|
+
index
|
|
3591
|
+
});
|
|
3592
|
+
}
|
|
3593
|
+
});
|
|
3594
|
+
return layouts;
|
|
3595
|
+
}
|
|
3596
|
+
|
|
3465
3597
|
function hasSchema(props) {
|
|
3466
3598
|
return props.schema !== undefined;
|
|
3467
3599
|
}
|
|
3468
|
-
function
|
|
3469
|
-
const $ = compilerRuntime.c(20);
|
|
3600
|
+
function FormGeneratorImpl(props) {
|
|
3470
3601
|
const {
|
|
3471
3602
|
fields,
|
|
3472
3603
|
defaultValues,
|
|
3473
3604
|
className,
|
|
3474
|
-
submitText
|
|
3605
|
+
submitText = 'Submit',
|
|
3475
3606
|
disabled,
|
|
3476
|
-
mode
|
|
3607
|
+
mode = 'onChange',
|
|
3608
|
+
title,
|
|
3609
|
+
description,
|
|
3610
|
+
showReset = false,
|
|
3611
|
+
resetText = 'Reset',
|
|
3612
|
+
children,
|
|
3613
|
+
ref
|
|
3477
3614
|
} = props;
|
|
3478
|
-
const submitText = t0 === undefined ? "Submit" : t0;
|
|
3479
|
-
const mode = t1 === undefined ? "onChange" : t1;
|
|
3480
3615
|
const userSchema = hasSchema(props) ? props.schema : undefined;
|
|
3481
3616
|
const onSubmit = props.onSubmit;
|
|
3482
|
-
|
|
3483
|
-
|
|
3617
|
+
const formRef = React.useRef(null);
|
|
3618
|
+
const schema = React.useMemo(() => {
|
|
3484
3619
|
if (userSchema) {
|
|
3485
|
-
|
|
3486
|
-
break bb0;
|
|
3487
|
-
}
|
|
3488
|
-
let t3;
|
|
3489
|
-
if ($[0] !== fields) {
|
|
3490
|
-
t3 = buildSchema(fields);
|
|
3491
|
-
$[0] = fields;
|
|
3492
|
-
$[1] = t3;
|
|
3493
|
-
} else {
|
|
3494
|
-
t3 = $[1];
|
|
3620
|
+
return userSchema;
|
|
3495
3621
|
}
|
|
3496
|
-
|
|
3497
|
-
}
|
|
3498
|
-
const
|
|
3499
|
-
let t3;
|
|
3500
|
-
if ($[2] !== defaultValues || $[3] !== fields) {
|
|
3622
|
+
return buildSchema(fields);
|
|
3623
|
+
}, [fields, userSchema]);
|
|
3624
|
+
const mergedDefaultValues = React.useMemo(() => {
|
|
3501
3625
|
const builtDefaults = buildDefaultValues(fields);
|
|
3502
|
-
|
|
3503
|
-
|
|
3504
|
-
|
|
3505
|
-
|
|
3506
|
-
|
|
3507
|
-
|
|
3508
|
-
}
|
|
3509
|
-
const
|
|
3510
|
-
|
|
3511
|
-
|
|
3512
|
-
|
|
3513
|
-
|
|
3514
|
-
|
|
3515
|
-
|
|
3516
|
-
|
|
3517
|
-
|
|
3518
|
-
|
|
3519
|
-
|
|
3520
|
-
|
|
3521
|
-
|
|
3522
|
-
|
|
3523
|
-
|
|
3524
|
-
|
|
3525
|
-
|
|
3526
|
-
|
|
3527
|
-
|
|
3528
|
-
|
|
3529
|
-
|
|
3530
|
-
|
|
3531
|
-
|
|
3532
|
-
|
|
3533
|
-
|
|
3534
|
-
|
|
3535
|
-
|
|
3536
|
-
|
|
3537
|
-
|
|
3538
|
-
|
|
3539
|
-
|
|
3540
|
-
|
|
3541
|
-
|
|
3542
|
-
|
|
3543
|
-
|
|
3626
|
+
return defaultValues ? deepMerge(builtDefaults, defaultValues) : builtDefaults;
|
|
3627
|
+
}, [fields, defaultValues]);
|
|
3628
|
+
const form = useForm({
|
|
3629
|
+
resolver: a(schema),
|
|
3630
|
+
defaultValues: mergedDefaultValues,
|
|
3631
|
+
mode
|
|
3632
|
+
});
|
|
3633
|
+
const handleSubmit = form.handleSubmit(async data => {
|
|
3634
|
+
await onSubmit(data);
|
|
3635
|
+
});
|
|
3636
|
+
const handleReset = React.useCallback(() => {
|
|
3637
|
+
form.reset(mergedDefaultValues);
|
|
3638
|
+
}, [form, mergedDefaultValues]);
|
|
3639
|
+
React.useImperativeHandle(ref, () => ({
|
|
3640
|
+
setValues: values => {
|
|
3641
|
+
Object.entries(values).forEach(([key, value]) => {
|
|
3642
|
+
form.setValue(key, value, {
|
|
3643
|
+
shouldValidate: true,
|
|
3644
|
+
shouldDirty: true
|
|
3645
|
+
});
|
|
3646
|
+
});
|
|
3647
|
+
},
|
|
3648
|
+
getValues: () => form.getValues(),
|
|
3649
|
+
reset: values_0 => {
|
|
3650
|
+
if (values_0) {
|
|
3651
|
+
form.reset(deepMerge(mergedDefaultValues, values_0));
|
|
3652
|
+
} else {
|
|
3653
|
+
form.reset(mergedDefaultValues);
|
|
3654
|
+
}
|
|
3655
|
+
},
|
|
3656
|
+
submit: async () => {
|
|
3657
|
+
await handleSubmit();
|
|
3658
|
+
},
|
|
3659
|
+
clearErrors: () => form.clearErrors(),
|
|
3660
|
+
setError: (name, error) => form.setError(name, error),
|
|
3661
|
+
isValid: () => form.formState.isValid,
|
|
3662
|
+
isDirty: () => form.formState.isDirty,
|
|
3663
|
+
form: form
|
|
3664
|
+
}), [form, handleSubmit, mergedDefaultValues]);
|
|
3665
|
+
const SubmitButton = getFormComponent('SubmitButton');
|
|
3666
|
+
const fieldEntries = React.useMemo(() => {
|
|
3667
|
+
const entries = new Map();
|
|
3668
|
+
const allFields = extractFields(fields);
|
|
3669
|
+
allFields.forEach((field, index) => {
|
|
3670
|
+
if (!field.hidden) {
|
|
3671
|
+
const element = jsxRuntime.jsx(FieldRenderer, {
|
|
3672
|
+
field: field
|
|
3673
|
+
}, field.name || `field-${index}`);
|
|
3674
|
+
entries.set(field.name, {
|
|
3675
|
+
field,
|
|
3676
|
+
element,
|
|
3677
|
+
accessed: false
|
|
3678
|
+
});
|
|
3679
|
+
}
|
|
3680
|
+
});
|
|
3681
|
+
return entries;
|
|
3682
|
+
}, [fields]);
|
|
3683
|
+
const layoutEntries = React.useMemo(() => {
|
|
3684
|
+
const entries_0 = new Map();
|
|
3685
|
+
const allLayouts = extractLayouts(fields);
|
|
3686
|
+
allLayouts.forEach(({
|
|
3687
|
+
layout,
|
|
3688
|
+
index: index_0
|
|
3689
|
+
}) => {
|
|
3690
|
+
if (!layout.hidden) {
|
|
3691
|
+
const key_0 = layout.name || `layout-${layout.type}-${index_0}`;
|
|
3692
|
+
const element_0 = jsxRuntime.jsx(FormItemRenderer, {
|
|
3693
|
+
item: layout
|
|
3694
|
+
}, key_0);
|
|
3695
|
+
if (layout.name) {
|
|
3696
|
+
entries_0.set(layout.name, {
|
|
3697
|
+
layout,
|
|
3698
|
+
element: element_0
|
|
3699
|
+
});
|
|
3700
|
+
}
|
|
3701
|
+
}
|
|
3702
|
+
});
|
|
3703
|
+
return entries_0;
|
|
3704
|
+
}, [fields]);
|
|
3705
|
+
const templateFields = React.useMemo(() => createTemplateFields(fieldEntries), [fieldEntries]);
|
|
3706
|
+
const templateLayouts = React.useMemo(() => createTemplateLayouts(layoutEntries), [layoutEntries]);
|
|
3707
|
+
const buttons = React.useMemo(() => {
|
|
3708
|
+
const result = {
|
|
3709
|
+
submit: jsxRuntime.jsx(SubmitButton, {
|
|
3710
|
+
disabled: disabled || form.formState.isSubmitting,
|
|
3711
|
+
isSubmitting: form.formState.isSubmitting,
|
|
3712
|
+
children: submitText
|
|
3713
|
+
}, "submit")
|
|
3714
|
+
};
|
|
3715
|
+
if (showReset) {
|
|
3716
|
+
result.reset = jsxRuntime.jsx("button", {
|
|
3717
|
+
type: "button",
|
|
3718
|
+
onClick: handleReset,
|
|
3719
|
+
disabled: disabled,
|
|
3720
|
+
children: resetText
|
|
3721
|
+
}, "reset");
|
|
3544
3722
|
}
|
|
3545
|
-
|
|
3546
|
-
|
|
3547
|
-
|
|
3723
|
+
return result;
|
|
3724
|
+
}, [SubmitButton, disabled, form.formState.isSubmitting, submitText, showReset, resetText, handleReset]);
|
|
3725
|
+
const renderField = React.useCallback((field_0, options) => {
|
|
3726
|
+
return jsxRuntime.jsx(FieldRenderer, {
|
|
3727
|
+
field: field_0,
|
|
3728
|
+
namePrefix: options === null || options === void 0 ? void 0 : options.namePrefix
|
|
3729
|
+
});
|
|
3730
|
+
}, []);
|
|
3731
|
+
const renderLayout = React.useCallback((layout_0, options_0) => {
|
|
3732
|
+
return jsxRuntime.jsx(FormItemRenderer, {
|
|
3733
|
+
item: layout_0,
|
|
3734
|
+
namePrefix: options_0 === null || options_0 === void 0 ? void 0 : options_0.namePrefix
|
|
3735
|
+
});
|
|
3736
|
+
}, []);
|
|
3737
|
+
if (!children) {
|
|
3738
|
+
return jsxRuntime.jsx(FormProvider, {
|
|
3548
3739
|
...form,
|
|
3549
3740
|
children: jsxRuntime.jsxs("form", {
|
|
3741
|
+
ref: formRef,
|
|
3550
3742
|
onSubmit: handleSubmit,
|
|
3551
|
-
className,
|
|
3552
|
-
children: [fields.map(
|
|
3743
|
+
className: className,
|
|
3744
|
+
children: [fields.map((item, index_1) => jsxRuntime.jsx(FormItemRenderer, {
|
|
3745
|
+
item: item
|
|
3746
|
+
}, getItemKey(item, index_1))), jsxRuntime.jsx(SubmitButton, {
|
|
3553
3747
|
disabled: disabled || form.formState.isSubmitting,
|
|
3554
3748
|
isSubmitting: form.formState.isSubmitting,
|
|
3555
3749
|
children: submitText
|
|
3556
3750
|
})]
|
|
3557
3751
|
})
|
|
3558
3752
|
});
|
|
3559
|
-
$[11] = className;
|
|
3560
|
-
$[12] = disabled;
|
|
3561
|
-
$[13] = fields;
|
|
3562
|
-
$[14] = form;
|
|
3563
|
-
$[15] = onSubmit;
|
|
3564
|
-
$[16] = submitText;
|
|
3565
|
-
$[17] = t6;
|
|
3566
|
-
} else {
|
|
3567
|
-
t6 = $[17];
|
|
3568
3753
|
}
|
|
3569
|
-
|
|
3754
|
+
const renderProps = {
|
|
3755
|
+
fields: templateFields,
|
|
3756
|
+
layouts: templateLayouts,
|
|
3757
|
+
buttons,
|
|
3758
|
+
title,
|
|
3759
|
+
description,
|
|
3760
|
+
form: form,
|
|
3761
|
+
isSubmitting: form.formState.isSubmitting,
|
|
3762
|
+
isValid: form.formState.isValid,
|
|
3763
|
+
isDirty: form.formState.isDirty,
|
|
3764
|
+
renderField,
|
|
3765
|
+
renderLayout
|
|
3766
|
+
};
|
|
3767
|
+
const renderFn = children;
|
|
3768
|
+
return jsxRuntime.jsx(FormProvider, {
|
|
3769
|
+
...form,
|
|
3770
|
+
children: jsxRuntime.jsx("form", {
|
|
3771
|
+
ref: formRef,
|
|
3772
|
+
onSubmit: handleSubmit,
|
|
3773
|
+
className: className,
|
|
3774
|
+
children: renderFn(renderProps)
|
|
3775
|
+
})
|
|
3776
|
+
});
|
|
3570
3777
|
}
|
|
3571
|
-
function
|
|
3572
|
-
return
|
|
3573
|
-
item
|
|
3574
|
-
}, getItemKey(item, index));
|
|
3778
|
+
function FormGenerator(props) {
|
|
3779
|
+
return FormGeneratorImpl(props);
|
|
3575
3780
|
}
|
|
3576
3781
|
|
|
3577
3782
|
Object.defineProperty(exports, "z", {
|