@connect-soft/form-generator 1.1.0-alpha9 → 1.1.2
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 +271 -2
- package/dist/index.js +438 -38
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +427 -39
- package/dist/index.mjs.map +1 -1
- package/dist/types/components/form/create-template-fields.d.ts +5 -1
- package/dist/types/components/form/create-template-fields.d.ts.map +1 -1
- package/dist/types/components/form/form-generator-typed.d.ts +3 -5
- package/dist/types/components/form/form-generator-typed.d.ts.map +1 -1
- package/dist/types/components/form/form-generator.d.ts +19 -3
- package/dist/types/components/form/form-generator.d.ts.map +1 -1
- package/dist/types/components/form/form-utils.d.ts +38 -0
- package/dist/types/components/form/form-utils.d.ts.map +1 -1
- package/dist/types/components/form/index.d.ts +3 -1
- package/dist/types/components/form/index.d.ts.map +1 -1
- package/dist/types/components/form/template-field-context.d.ts +14 -0
- package/dist/types/components/form/template-field-context.d.ts.map +1 -0
- package/dist/types/index.d.ts +4 -1
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/lib/field-types.d.ts +8 -3
- package/dist/types/lib/field-types.d.ts.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -3304,7 +3304,7 @@ const ArrayFieldRenderer = /*#__PURE__*/React.memo(t0 => {
|
|
|
3304
3304
|
remove: () => remove(index_0),
|
|
3305
3305
|
move: toIndex => move(index_0, toIndex),
|
|
3306
3306
|
fields: renderItem(index_0),
|
|
3307
|
-
fieldNames: arrayField.fields.map(_temp),
|
|
3307
|
+
fieldNames: arrayField.fields.map(_temp$1),
|
|
3308
3308
|
namePrefix: `${field.name}.${index_0}`
|
|
3309
3309
|
});
|
|
3310
3310
|
};
|
|
@@ -3343,7 +3343,7 @@ const ArrayFieldRenderer = /*#__PURE__*/React.memo(t0 => {
|
|
|
3343
3343
|
return t1;
|
|
3344
3344
|
});
|
|
3345
3345
|
ArrayFieldRenderer.displayName = 'ArrayFieldRenderer';
|
|
3346
|
-
function _temp(f) {
|
|
3346
|
+
function _temp$1(f) {
|
|
3347
3347
|
return f.name;
|
|
3348
3348
|
}
|
|
3349
3349
|
|
|
@@ -3404,7 +3404,10 @@ function createTemplateFields(fieldEntries) {
|
|
|
3404
3404
|
return undefined;
|
|
3405
3405
|
}
|
|
3406
3406
|
};
|
|
3407
|
-
return
|
|
3407
|
+
return {
|
|
3408
|
+
templateFields: new Proxy({}, handler),
|
|
3409
|
+
accessedFields
|
|
3410
|
+
};
|
|
3408
3411
|
}
|
|
3409
3412
|
|
|
3410
3413
|
const FieldsContext = /*#__PURE__*/React.createContext(null);
|
|
@@ -3568,6 +3571,232 @@ function useFieldsContext() {
|
|
|
3568
3571
|
return React.useContext(FieldsContext);
|
|
3569
3572
|
}
|
|
3570
3573
|
|
|
3574
|
+
function isSchemaRequired(schema) {
|
|
3575
|
+
return !schema.isOptional() && !schema.isNullable();
|
|
3576
|
+
}
|
|
3577
|
+
function unwrapSchema(schema) {
|
|
3578
|
+
let current = schema;
|
|
3579
|
+
while (current instanceof zod.z.ZodOptional || current instanceof zod.z.ZodNullable) {
|
|
3580
|
+
current = current.unwrap();
|
|
3581
|
+
}
|
|
3582
|
+
if (current instanceof zod.z.ZodDefault) {
|
|
3583
|
+
current = current._def.innerType;
|
|
3584
|
+
}
|
|
3585
|
+
return current;
|
|
3586
|
+
}
|
|
3587
|
+
function getSchemaTypeName(schema) {
|
|
3588
|
+
const unwrapped = unwrapSchema(schema);
|
|
3589
|
+
if (unwrapped instanceof zod.z.ZodString) return 'string';
|
|
3590
|
+
if (unwrapped instanceof zod.z.ZodNumber) return 'number';
|
|
3591
|
+
if (unwrapped instanceof zod.z.ZodBoolean) return 'boolean';
|
|
3592
|
+
if (unwrapped instanceof zod.z.ZodDate) return 'date';
|
|
3593
|
+
if (unwrapped instanceof zod.z.ZodArray) return 'array';
|
|
3594
|
+
if (unwrapped instanceof zod.z.ZodObject) return 'object';
|
|
3595
|
+
if (unwrapped instanceof zod.z.ZodEnum) return 'enum';
|
|
3596
|
+
return 'unknown';
|
|
3597
|
+
}
|
|
3598
|
+
function getCheckDef(check) {
|
|
3599
|
+
var _a;
|
|
3600
|
+
if ((_a = check._zod) === null || _a === void 0 ? void 0 : _a.def) {
|
|
3601
|
+
const def = check._zod.def;
|
|
3602
|
+
return {
|
|
3603
|
+
type: def.check,
|
|
3604
|
+
value: def.value,
|
|
3605
|
+
minimum: def.minimum,
|
|
3606
|
+
maximum: def.maximum,
|
|
3607
|
+
length: def.length,
|
|
3608
|
+
regex: def.pattern || def.regex,
|
|
3609
|
+
format: def.format,
|
|
3610
|
+
inclusive: def.inclusive
|
|
3611
|
+
};
|
|
3612
|
+
}
|
|
3613
|
+
if (check.kind) {
|
|
3614
|
+
return {
|
|
3615
|
+
type: check.kind,
|
|
3616
|
+
value: check.value,
|
|
3617
|
+
regex: check.regex
|
|
3618
|
+
};
|
|
3619
|
+
}
|
|
3620
|
+
return null;
|
|
3621
|
+
}
|
|
3622
|
+
function getNumberConstraints(schema) {
|
|
3623
|
+
const unwrapped = unwrapSchema(schema);
|
|
3624
|
+
if (!(unwrapped instanceof zod.z.ZodNumber)) {
|
|
3625
|
+
return {};
|
|
3626
|
+
}
|
|
3627
|
+
const constraints = {};
|
|
3628
|
+
const checks = unwrapped._def.checks;
|
|
3629
|
+
if (checks) {
|
|
3630
|
+
for (const check of checks) {
|
|
3631
|
+
const def = getCheckDef(check);
|
|
3632
|
+
if (!def) continue;
|
|
3633
|
+
if (def.type === 'greater_than' || def.type === 'min') {
|
|
3634
|
+
constraints.min = def.value;
|
|
3635
|
+
} else if (def.type === 'less_than' || def.type === 'max') {
|
|
3636
|
+
constraints.max = def.value;
|
|
3637
|
+
} else if (def.type === 'multiple_of' || def.type === 'multipleOf') {
|
|
3638
|
+
constraints.step = def.value;
|
|
3639
|
+
} else if (def.type === 'number_format' && (def.format === 'safeint' || def.format === 'int')) {
|
|
3640
|
+
constraints.step = 1;
|
|
3641
|
+
} else if (def.type === 'int' || def.type === 'integer') {
|
|
3642
|
+
constraints.step = 1;
|
|
3643
|
+
}
|
|
3644
|
+
}
|
|
3645
|
+
}
|
|
3646
|
+
return constraints;
|
|
3647
|
+
}
|
|
3648
|
+
function getStringConstraints(schema) {
|
|
3649
|
+
const unwrapped = unwrapSchema(schema);
|
|
3650
|
+
if (!(unwrapped instanceof zod.z.ZodString)) {
|
|
3651
|
+
return {};
|
|
3652
|
+
}
|
|
3653
|
+
const constraints = {};
|
|
3654
|
+
const checks = unwrapped._def.checks;
|
|
3655
|
+
if (checks) {
|
|
3656
|
+
for (const check of checks) {
|
|
3657
|
+
const def = getCheckDef(check);
|
|
3658
|
+
if (!def) continue;
|
|
3659
|
+
if (def.type === 'min_length' && def.minimum !== undefined) {
|
|
3660
|
+
constraints.minLength = def.minimum;
|
|
3661
|
+
} else if (def.type === 'min' && def.value !== undefined) {
|
|
3662
|
+
constraints.minLength = def.value;
|
|
3663
|
+
} else if (def.type === 'max_length' && def.maximum !== undefined) {
|
|
3664
|
+
constraints.maxLength = def.maximum;
|
|
3665
|
+
} else if (def.type === 'max' && def.value !== undefined) {
|
|
3666
|
+
constraints.maxLength = def.value;
|
|
3667
|
+
} else if (def.type === 'length_equals' && def.length !== undefined) {
|
|
3668
|
+
constraints.minLength = def.length;
|
|
3669
|
+
constraints.maxLength = def.length;
|
|
3670
|
+
} else if (def.type === 'length' && def.value !== undefined) {
|
|
3671
|
+
constraints.minLength = def.value;
|
|
3672
|
+
constraints.maxLength = def.value;
|
|
3673
|
+
} else if (def.type === 'string_format' && def.regex) {
|
|
3674
|
+
constraints.pattern = def.regex.source;
|
|
3675
|
+
} else if (def.type === 'regex' && def.regex) {
|
|
3676
|
+
constraints.pattern = def.regex.source;
|
|
3677
|
+
}
|
|
3678
|
+
}
|
|
3679
|
+
}
|
|
3680
|
+
return constraints;
|
|
3681
|
+
}
|
|
3682
|
+
function getDateConstraints(schema) {
|
|
3683
|
+
const unwrapped = unwrapSchema(schema);
|
|
3684
|
+
if (!(unwrapped instanceof zod.z.ZodDate)) {
|
|
3685
|
+
return {};
|
|
3686
|
+
}
|
|
3687
|
+
const constraints = {};
|
|
3688
|
+
const checks = unwrapped._def.checks;
|
|
3689
|
+
if (checks) {
|
|
3690
|
+
for (const check of checks) {
|
|
3691
|
+
const def = getCheckDef(check);
|
|
3692
|
+
if (!def) continue;
|
|
3693
|
+
if (def.type === 'greater_than' || def.type === 'min') {
|
|
3694
|
+
const value = def.value;
|
|
3695
|
+
constraints.min = value instanceof Date ? value : new Date(value);
|
|
3696
|
+
} else if (def.type === 'less_than' || def.type === 'max') {
|
|
3697
|
+
const value = def.value;
|
|
3698
|
+
constraints.max = value instanceof Date ? value : new Date(value);
|
|
3699
|
+
}
|
|
3700
|
+
}
|
|
3701
|
+
}
|
|
3702
|
+
return constraints;
|
|
3703
|
+
}
|
|
3704
|
+
function getSchemaRequirements(schema) {
|
|
3705
|
+
const result = {};
|
|
3706
|
+
const shape = schema.shape;
|
|
3707
|
+
for (const key in shape) {
|
|
3708
|
+
result[key] = isSchemaRequired(shape[key]);
|
|
3709
|
+
}
|
|
3710
|
+
return result;
|
|
3711
|
+
}
|
|
3712
|
+
function analyzeSchema(schema) {
|
|
3713
|
+
const shape = schema.shape;
|
|
3714
|
+
return Object.entries(shape).map(([name, fieldSchema]) => {
|
|
3715
|
+
const type = getSchemaTypeName(fieldSchema);
|
|
3716
|
+
const info = {
|
|
3717
|
+
name,
|
|
3718
|
+
required: isSchemaRequired(fieldSchema),
|
|
3719
|
+
type,
|
|
3720
|
+
schema: fieldSchema
|
|
3721
|
+
};
|
|
3722
|
+
if (type === 'number') {
|
|
3723
|
+
const constraints = getNumberConstraints(fieldSchema);
|
|
3724
|
+
if (constraints.min !== undefined) info.min = constraints.min;
|
|
3725
|
+
if (constraints.max !== undefined) info.max = constraints.max;
|
|
3726
|
+
if (constraints.step !== undefined) info.step = constraints.step;
|
|
3727
|
+
} else if (type === 'string') {
|
|
3728
|
+
const constraints = getStringConstraints(fieldSchema);
|
|
3729
|
+
if (constraints.minLength !== undefined) info.minLength = constraints.minLength;
|
|
3730
|
+
if (constraints.maxLength !== undefined) info.maxLength = constraints.maxLength;
|
|
3731
|
+
if (constraints.pattern !== undefined) info.pattern = constraints.pattern;
|
|
3732
|
+
} else if (type === 'date') {
|
|
3733
|
+
const constraints = getDateConstraints(fieldSchema);
|
|
3734
|
+
if (constraints.min !== undefined) info.minDate = constraints.min;
|
|
3735
|
+
if (constraints.max !== undefined) info.maxDate = constraints.max;
|
|
3736
|
+
}
|
|
3737
|
+
return info;
|
|
3738
|
+
});
|
|
3739
|
+
}
|
|
3740
|
+
function mergeSchemaRequirements(schema, fields) {
|
|
3741
|
+
const requirements = getSchemaRequirements(schema);
|
|
3742
|
+
return fields.map(field => {
|
|
3743
|
+
const schemaRequired = requirements[field.name];
|
|
3744
|
+
if (schemaRequired !== undefined) {
|
|
3745
|
+
return {
|
|
3746
|
+
...field,
|
|
3747
|
+
required: schemaRequired
|
|
3748
|
+
};
|
|
3749
|
+
}
|
|
3750
|
+
return field;
|
|
3751
|
+
});
|
|
3752
|
+
}
|
|
3753
|
+
function mergeSchemaConstraints(schema, fields) {
|
|
3754
|
+
const fieldInfoMap = new Map();
|
|
3755
|
+
analyzeSchema(schema).forEach(info => {
|
|
3756
|
+
fieldInfoMap.set(info.name, info);
|
|
3757
|
+
});
|
|
3758
|
+
return fields.map(field => {
|
|
3759
|
+
const schemaInfo = fieldInfoMap.get(field.name);
|
|
3760
|
+
if (!schemaInfo) {
|
|
3761
|
+
return field;
|
|
3762
|
+
}
|
|
3763
|
+
const merged = {
|
|
3764
|
+
...field,
|
|
3765
|
+
required: schemaInfo.required
|
|
3766
|
+
};
|
|
3767
|
+
if (schemaInfo.type === 'number') {
|
|
3768
|
+
if (schemaInfo.min !== undefined) {
|
|
3769
|
+
merged.min = schemaInfo.min;
|
|
3770
|
+
}
|
|
3771
|
+
if (schemaInfo.max !== undefined) {
|
|
3772
|
+
merged.max = schemaInfo.max;
|
|
3773
|
+
}
|
|
3774
|
+
if (schemaInfo.step !== undefined) {
|
|
3775
|
+
merged.step = schemaInfo.step;
|
|
3776
|
+
}
|
|
3777
|
+
}
|
|
3778
|
+
if (schemaInfo.type === 'string') {
|
|
3779
|
+
if (schemaInfo.minLength !== undefined) {
|
|
3780
|
+
merged.minLength = schemaInfo.minLength;
|
|
3781
|
+
}
|
|
3782
|
+
if (schemaInfo.maxLength !== undefined) {
|
|
3783
|
+
merged.maxLength = schemaInfo.maxLength;
|
|
3784
|
+
}
|
|
3785
|
+
if (schemaInfo.pattern !== undefined) {
|
|
3786
|
+
merged.pattern = schemaInfo.pattern;
|
|
3787
|
+
}
|
|
3788
|
+
}
|
|
3789
|
+
if (schemaInfo.type === 'date') {
|
|
3790
|
+
if (schemaInfo.minDate !== undefined) {
|
|
3791
|
+
merged.min = schemaInfo.minDate.toISOString().split('T')[0];
|
|
3792
|
+
}
|
|
3793
|
+
if (schemaInfo.maxDate !== undefined) {
|
|
3794
|
+
merged.max = schemaInfo.maxDate.toISOString().split('T')[0];
|
|
3795
|
+
}
|
|
3796
|
+
}
|
|
3797
|
+
return merged;
|
|
3798
|
+
});
|
|
3799
|
+
}
|
|
3571
3800
|
function deepMerge(target, source) {
|
|
3572
3801
|
const result = {
|
|
3573
3802
|
...target
|
|
@@ -3630,6 +3859,33 @@ function buildArrayItemSchema(arrayField) {
|
|
|
3630
3859
|
}
|
|
3631
3860
|
return zod.z.object(itemShape);
|
|
3632
3861
|
}
|
|
3862
|
+
function setNestedShape(shape, path, validator) {
|
|
3863
|
+
const parts = path.split('.');
|
|
3864
|
+
if (parts.length === 1) {
|
|
3865
|
+
shape[path] = validator;
|
|
3866
|
+
return;
|
|
3867
|
+
}
|
|
3868
|
+
const [head, ...rest] = parts;
|
|
3869
|
+
const nestedPath = rest.join('.');
|
|
3870
|
+
const existingNested = shape[head];
|
|
3871
|
+
const nestedShape = existingNested ? {
|
|
3872
|
+
...existingNested.shape
|
|
3873
|
+
} : {};
|
|
3874
|
+
setNestedShape(nestedShape, nestedPath, validator);
|
|
3875
|
+
shape[head] = zod.z.object(nestedShape);
|
|
3876
|
+
}
|
|
3877
|
+
function setNestedValue(obj, path, value) {
|
|
3878
|
+
const parts = path.split('.');
|
|
3879
|
+
if (parts.length === 1) {
|
|
3880
|
+
obj[path] = value;
|
|
3881
|
+
return;
|
|
3882
|
+
}
|
|
3883
|
+
const [head, ...rest] = parts;
|
|
3884
|
+
if (!(head in obj) || typeof obj[head] !== 'object' || obj[head] === null) {
|
|
3885
|
+
obj[head] = {};
|
|
3886
|
+
}
|
|
3887
|
+
setNestedValue(obj[head], rest.join('.'), value);
|
|
3888
|
+
}
|
|
3633
3889
|
function buildSchema(fields) {
|
|
3634
3890
|
const shape = {};
|
|
3635
3891
|
for (const field of fields) {
|
|
@@ -3641,10 +3897,10 @@ function buildSchema(fields) {
|
|
|
3641
3897
|
if (field.maxItems !== undefined) {
|
|
3642
3898
|
arraySchema = arraySchema.max(field.maxItems);
|
|
3643
3899
|
}
|
|
3644
|
-
shape
|
|
3900
|
+
setNestedShape(shape, field.name, arraySchema);
|
|
3645
3901
|
} else {
|
|
3646
3902
|
const validator = getValidatorForField(field);
|
|
3647
|
-
shape
|
|
3903
|
+
setNestedShape(shape, field.name, field.required ? validator : validator.optional());
|
|
3648
3904
|
}
|
|
3649
3905
|
}
|
|
3650
3906
|
return zod.z.object(shape);
|
|
@@ -3672,12 +3928,84 @@ function buildDefaultValues(fields) {
|
|
|
3672
3928
|
values[field.name] = [];
|
|
3673
3929
|
}
|
|
3674
3930
|
} else {
|
|
3675
|
-
values
|
|
3931
|
+
setNestedValue(values, field.name, (_a = field.defaultValue) !== null && _a !== void 0 ? _a : '');
|
|
3676
3932
|
}
|
|
3677
3933
|
}
|
|
3678
3934
|
return values;
|
|
3679
3935
|
}
|
|
3680
3936
|
|
|
3937
|
+
const TemplateFieldContext = /*#__PURE__*/React.createContext(null);
|
|
3938
|
+
function TemplateFieldProvider({
|
|
3939
|
+
children,
|
|
3940
|
+
fieldEntries,
|
|
3941
|
+
accessedFields
|
|
3942
|
+
}) {
|
|
3943
|
+
return jsxRuntime.jsx(TemplateFieldContext.Provider, {
|
|
3944
|
+
value: {
|
|
3945
|
+
fieldEntries,
|
|
3946
|
+
accessedFields
|
|
3947
|
+
},
|
|
3948
|
+
children: children
|
|
3949
|
+
});
|
|
3950
|
+
}
|
|
3951
|
+
function useTemplateField(name) {
|
|
3952
|
+
const $ = compilerRuntime.c(3);
|
|
3953
|
+
const context = React.useContext(TemplateFieldContext);
|
|
3954
|
+
if (!context) {
|
|
3955
|
+
throw new Error("useTemplateField must be used within FormGenerator with a custom layout (children render function).");
|
|
3956
|
+
}
|
|
3957
|
+
const {
|
|
3958
|
+
fieldEntries,
|
|
3959
|
+
accessedFields
|
|
3960
|
+
} = context;
|
|
3961
|
+
let t0;
|
|
3962
|
+
if ($[0] !== fieldEntries || $[1] !== name) {
|
|
3963
|
+
t0 = fieldEntries.get(name);
|
|
3964
|
+
$[0] = fieldEntries;
|
|
3965
|
+
$[1] = name;
|
|
3966
|
+
$[2] = t0;
|
|
3967
|
+
} else {
|
|
3968
|
+
t0 = $[2];
|
|
3969
|
+
}
|
|
3970
|
+
const entry = t0;
|
|
3971
|
+
if (entry) {
|
|
3972
|
+
accessedFields.add(name);
|
|
3973
|
+
return entry.element;
|
|
3974
|
+
}
|
|
3975
|
+
}
|
|
3976
|
+
function RemainingFields() {
|
|
3977
|
+
const $ = compilerRuntime.c(3);
|
|
3978
|
+
const context = React.useContext(TemplateFieldContext);
|
|
3979
|
+
if (!context) {
|
|
3980
|
+
throw new Error("RemainingFields must be used within FormGenerator with a custom layout (children render function).");
|
|
3981
|
+
}
|
|
3982
|
+
const {
|
|
3983
|
+
fieldEntries,
|
|
3984
|
+
accessedFields
|
|
3985
|
+
} = context;
|
|
3986
|
+
let t0;
|
|
3987
|
+
if ($[0] !== accessedFields || $[1] !== fieldEntries) {
|
|
3988
|
+
const remaining = Array.from(fieldEntries.entries()).filter(t1 => {
|
|
3989
|
+
const [name] = t1;
|
|
3990
|
+
return !accessedFields.has(name);
|
|
3991
|
+
}).map(_temp);
|
|
3992
|
+
t0 = jsxRuntime.jsx(jsxRuntime.Fragment, {
|
|
3993
|
+
children: remaining
|
|
3994
|
+
});
|
|
3995
|
+
$[0] = accessedFields;
|
|
3996
|
+
$[1] = fieldEntries;
|
|
3997
|
+
$[2] = t0;
|
|
3998
|
+
} else {
|
|
3999
|
+
t0 = $[2];
|
|
4000
|
+
}
|
|
4001
|
+
return t0;
|
|
4002
|
+
}
|
|
4003
|
+
function _temp(t0) {
|
|
4004
|
+
const [, entry] = t0;
|
|
4005
|
+
return entry.element;
|
|
4006
|
+
}
|
|
4007
|
+
RemainingFields.displayName = 'RemainingFields';
|
|
4008
|
+
|
|
3681
4009
|
function FormGenerator(props) {
|
|
3682
4010
|
const {
|
|
3683
4011
|
fields,
|
|
@@ -3717,17 +4045,31 @@ function FormGenerator(props) {
|
|
|
3717
4045
|
mode
|
|
3718
4046
|
});
|
|
3719
4047
|
const handleSubmit = form.handleSubmit(async data => {
|
|
3720
|
-
|
|
4048
|
+
const context = {
|
|
4049
|
+
isDirty: form.formState.isDirty,
|
|
4050
|
+
dirtyFields: form.formState.dirtyFields,
|
|
4051
|
+
isValid: form.formState.isValid,
|
|
4052
|
+
errors: form.formState.errors,
|
|
4053
|
+
touchedFields: form.formState.touchedFields,
|
|
4054
|
+
isSubmitting: form.formState.isSubmitting,
|
|
4055
|
+
submitCount: form.formState.submitCount,
|
|
4056
|
+
defaultValues: mergedDefaultValues,
|
|
4057
|
+
form: form
|
|
4058
|
+
};
|
|
4059
|
+
await onSubmit(data, context);
|
|
3721
4060
|
});
|
|
3722
4061
|
const handleReset = React.useCallback(() => {
|
|
3723
4062
|
form.reset(mergedDefaultValues);
|
|
3724
4063
|
}, [form, mergedDefaultValues]);
|
|
3725
4064
|
React.useImperativeHandle(ref, () => ({
|
|
3726
|
-
setValues: values => {
|
|
4065
|
+
setValues: (values, options_0) => {
|
|
4066
|
+
var _a, _b;
|
|
4067
|
+
const shouldDirty = (_a = options_0 === null || options_0 === void 0 ? void 0 : options_0.shouldDirty) !== null && _a !== void 0 ? _a : true;
|
|
4068
|
+
const shouldValidate = (_b = options_0 === null || options_0 === void 0 ? void 0 : options_0.shouldValidate) !== null && _b !== void 0 ? _b : true;
|
|
3727
4069
|
Object.entries(values).forEach(([key, value]) => {
|
|
3728
4070
|
form.setValue(key, value, {
|
|
3729
|
-
shouldValidate
|
|
3730
|
-
shouldDirty
|
|
4071
|
+
shouldValidate,
|
|
4072
|
+
shouldDirty
|
|
3731
4073
|
});
|
|
3732
4074
|
});
|
|
3733
4075
|
},
|
|
@@ -3739,6 +4081,12 @@ function FormGenerator(props) {
|
|
|
3739
4081
|
form.reset(mergedDefaultValues);
|
|
3740
4082
|
}
|
|
3741
4083
|
},
|
|
4084
|
+
setDefaultValues: values_1 => {
|
|
4085
|
+
const newDefaults = deepMerge(mergedDefaultValues, values_1);
|
|
4086
|
+
form.reset(newDefaults, {
|
|
4087
|
+
keepDirtyValues: false
|
|
4088
|
+
});
|
|
4089
|
+
},
|
|
3742
4090
|
submit: async () => {
|
|
3743
4091
|
await handleSubmit();
|
|
3744
4092
|
},
|
|
@@ -3790,7 +4138,10 @@ function FormGenerator(props) {
|
|
|
3790
4138
|
});
|
|
3791
4139
|
return entries;
|
|
3792
4140
|
}, [regularFields, FieldWrapper]);
|
|
3793
|
-
const
|
|
4141
|
+
const {
|
|
4142
|
+
templateFields,
|
|
4143
|
+
accessedFields
|
|
4144
|
+
} = React.useMemo(() => createTemplateFields(fieldEntries), [fieldEntries]);
|
|
3794
4145
|
const buttons = React.useMemo(() => {
|
|
3795
4146
|
const result = {
|
|
3796
4147
|
submit: jsxRuntime.jsx(SubmitButton, {
|
|
@@ -3809,10 +4160,10 @@ function FormGenerator(props) {
|
|
|
3809
4160
|
}
|
|
3810
4161
|
return result;
|
|
3811
4162
|
}, [SubmitButton, disabled, form.formState.isSubmitting, submitText, showReset, resetText, handleReset]);
|
|
3812
|
-
const renderField = React.useCallback((field_1,
|
|
4163
|
+
const renderField = React.useCallback((field_1, options_1) => {
|
|
3813
4164
|
return jsxRuntime.jsx(FieldRenderer, {
|
|
3814
4165
|
field: field_1,
|
|
3815
|
-
namePrefix:
|
|
4166
|
+
namePrefix: options_1 === null || options_1 === void 0 ? void 0 : options_1.namePrefix
|
|
3816
4167
|
});
|
|
3817
4168
|
}, []);
|
|
3818
4169
|
if (!children) {
|
|
@@ -3870,11 +4221,15 @@ function FormGenerator(props) {
|
|
|
3870
4221
|
...form,
|
|
3871
4222
|
children: jsxRuntime.jsx(FieldsProvider, {
|
|
3872
4223
|
fields: fields,
|
|
3873
|
-
children: jsxRuntime.jsx(
|
|
3874
|
-
|
|
3875
|
-
|
|
3876
|
-
|
|
3877
|
-
|
|
4224
|
+
children: jsxRuntime.jsx(TemplateFieldProvider, {
|
|
4225
|
+
fieldEntries: fieldEntries,
|
|
4226
|
+
accessedFields: accessedFields,
|
|
4227
|
+
children: jsxRuntime.jsx("form", {
|
|
4228
|
+
ref: formRef,
|
|
4229
|
+
onSubmit: handleSubmit,
|
|
4230
|
+
className: className,
|
|
4231
|
+
children: renderFn(renderProps)
|
|
4232
|
+
})
|
|
3878
4233
|
})
|
|
3879
4234
|
})
|
|
3880
4235
|
});
|
|
@@ -3905,6 +4260,12 @@ function StrictFormGenerator(props) {
|
|
|
3905
4260
|
validateTypes
|
|
3906
4261
|
} = props;
|
|
3907
4262
|
const formRef = React.useRef(null);
|
|
4263
|
+
const fieldsWithRequirements = React.useMemo(() => {
|
|
4264
|
+
if (schema && typeof schema === 'object' && 'shape' in schema) {
|
|
4265
|
+
return mergeSchemaConstraints(schema, fields);
|
|
4266
|
+
}
|
|
4267
|
+
return fields;
|
|
4268
|
+
}, [schema, fields]);
|
|
3908
4269
|
React.useMemo(() => {
|
|
3909
4270
|
if (validateTypes) {
|
|
3910
4271
|
const registeredTypes = getRegisteredFieldTypes();
|
|
@@ -3912,30 +4273,44 @@ function StrictFormGenerator(props) {
|
|
|
3912
4273
|
throwOnError: false,
|
|
3913
4274
|
warn: true
|
|
3914
4275
|
};
|
|
3915
|
-
validateFieldTypes(
|
|
4276
|
+
validateFieldTypes(fieldsWithRequirements, registeredTypes, options);
|
|
3916
4277
|
}
|
|
3917
|
-
}, [
|
|
4278
|
+
}, [fieldsWithRequirements, validateTypes]);
|
|
3918
4279
|
const mergedDefaultValues = React.useMemo(() => {
|
|
3919
|
-
const builtDefaults = buildDefaultValues(
|
|
4280
|
+
const builtDefaults = buildDefaultValues(fieldsWithRequirements);
|
|
3920
4281
|
return defaultValues ? deepMerge(builtDefaults, defaultValues) : builtDefaults;
|
|
3921
|
-
}, [
|
|
4282
|
+
}, [fieldsWithRequirements, defaultValues]);
|
|
3922
4283
|
const form = useForm({
|
|
3923
4284
|
resolver: a(schema),
|
|
3924
4285
|
defaultValues: mergedDefaultValues,
|
|
3925
4286
|
mode
|
|
3926
4287
|
});
|
|
3927
4288
|
const handleSubmit = form.handleSubmit(async data => {
|
|
3928
|
-
|
|
4289
|
+
const context = {
|
|
4290
|
+
isDirty: form.formState.isDirty,
|
|
4291
|
+
dirtyFields: form.formState.dirtyFields,
|
|
4292
|
+
isValid: form.formState.isValid,
|
|
4293
|
+
errors: form.formState.errors,
|
|
4294
|
+
touchedFields: form.formState.touchedFields,
|
|
4295
|
+
isSubmitting: form.formState.isSubmitting,
|
|
4296
|
+
submitCount: form.formState.submitCount,
|
|
4297
|
+
defaultValues: mergedDefaultValues,
|
|
4298
|
+
form: form
|
|
4299
|
+
};
|
|
4300
|
+
await onSubmit(data, context);
|
|
3929
4301
|
});
|
|
3930
4302
|
const handleReset = React.useCallback(() => {
|
|
3931
4303
|
form.reset(mergedDefaultValues);
|
|
3932
4304
|
}, [form, mergedDefaultValues]);
|
|
3933
4305
|
React.useImperativeHandle(ref, () => ({
|
|
3934
|
-
setValues: values => {
|
|
4306
|
+
setValues: (values, options_0) => {
|
|
4307
|
+
var _a, _b;
|
|
4308
|
+
const shouldDirty = (_a = options_0 === null || options_0 === void 0 ? void 0 : options_0.shouldDirty) !== null && _a !== void 0 ? _a : true;
|
|
4309
|
+
const shouldValidate = (_b = options_0 === null || options_0 === void 0 ? void 0 : options_0.shouldValidate) !== null && _b !== void 0 ? _b : true;
|
|
3935
4310
|
Object.entries(values).forEach(([key, value]) => {
|
|
3936
4311
|
form.setValue(key, value, {
|
|
3937
|
-
shouldValidate
|
|
3938
|
-
shouldDirty
|
|
4312
|
+
shouldValidate,
|
|
4313
|
+
shouldDirty
|
|
3939
4314
|
});
|
|
3940
4315
|
});
|
|
3941
4316
|
},
|
|
@@ -3947,6 +4322,12 @@ function StrictFormGenerator(props) {
|
|
|
3947
4322
|
form.reset(mergedDefaultValues);
|
|
3948
4323
|
}
|
|
3949
4324
|
},
|
|
4325
|
+
setDefaultValues: values_1 => {
|
|
4326
|
+
const newDefaults = deepMerge(mergedDefaultValues, values_1);
|
|
4327
|
+
form.reset(newDefaults, {
|
|
4328
|
+
keepDirtyValues: false
|
|
4329
|
+
});
|
|
4330
|
+
},
|
|
3950
4331
|
submit: async () => {
|
|
3951
4332
|
await handleSubmit();
|
|
3952
4333
|
},
|
|
@@ -3965,7 +4346,7 @@ function StrictFormGenerator(props) {
|
|
|
3965
4346
|
} = React.useMemo(() => {
|
|
3966
4347
|
const regular = [];
|
|
3967
4348
|
const arrays = [];
|
|
3968
|
-
|
|
4349
|
+
fieldsWithRequirements.forEach(field => {
|
|
3969
4350
|
if (isArrayField(field)) {
|
|
3970
4351
|
arrays.push(field);
|
|
3971
4352
|
} else {
|
|
@@ -3976,7 +4357,7 @@ function StrictFormGenerator(props) {
|
|
|
3976
4357
|
regularFields: regular,
|
|
3977
4358
|
arrayFields: arrays
|
|
3978
4359
|
};
|
|
3979
|
-
}, [
|
|
4360
|
+
}, [fieldsWithRequirements]);
|
|
3980
4361
|
const fieldEntries = React.useMemo(() => {
|
|
3981
4362
|
const entries = new Map();
|
|
3982
4363
|
regularFields.forEach((field_0, index) => {
|
|
@@ -3998,7 +4379,10 @@ function StrictFormGenerator(props) {
|
|
|
3998
4379
|
});
|
|
3999
4380
|
return entries;
|
|
4000
4381
|
}, [regularFields, FieldWrapper]);
|
|
4001
|
-
const
|
|
4382
|
+
const {
|
|
4383
|
+
templateFields,
|
|
4384
|
+
accessedFields
|
|
4385
|
+
} = React.useMemo(() => createTemplateFields(fieldEntries), [fieldEntries]);
|
|
4002
4386
|
const buttons = React.useMemo(() => {
|
|
4003
4387
|
const result = {
|
|
4004
4388
|
submit: jsxRuntime.jsx(SubmitButton, {
|
|
@@ -4017,17 +4401,17 @@ function StrictFormGenerator(props) {
|
|
|
4017
4401
|
}
|
|
4018
4402
|
return result;
|
|
4019
4403
|
}, [SubmitButton, disabled, form.formState.isSubmitting, submitText, showReset, resetText, handleReset]);
|
|
4020
|
-
const renderField = React.useCallback((field_1,
|
|
4404
|
+
const renderField = React.useCallback((field_1, options_1) => {
|
|
4021
4405
|
return jsxRuntime.jsx(FieldRenderer, {
|
|
4022
4406
|
field: field_1,
|
|
4023
|
-
namePrefix:
|
|
4407
|
+
namePrefix: options_1 === null || options_1 === void 0 ? void 0 : options_1.namePrefix
|
|
4024
4408
|
});
|
|
4025
4409
|
}, []);
|
|
4026
4410
|
if (!children) {
|
|
4027
4411
|
return jsxRuntime.jsx(FormProvider, {
|
|
4028
4412
|
...form,
|
|
4029
4413
|
children: jsxRuntime.jsx(FieldsProvider, {
|
|
4030
|
-
fields:
|
|
4414
|
+
fields: fieldsWithRequirements,
|
|
4031
4415
|
children: jsxRuntime.jsxs("form", {
|
|
4032
4416
|
ref: formRef,
|
|
4033
4417
|
onSubmit: handleSubmit,
|
|
@@ -4077,12 +4461,16 @@ function StrictFormGenerator(props) {
|
|
|
4077
4461
|
return jsxRuntime.jsx(FormProvider, {
|
|
4078
4462
|
...form,
|
|
4079
4463
|
children: jsxRuntime.jsx(FieldsProvider, {
|
|
4080
|
-
fields:
|
|
4081
|
-
children: jsxRuntime.jsx(
|
|
4082
|
-
|
|
4083
|
-
|
|
4084
|
-
|
|
4085
|
-
|
|
4464
|
+
fields: fieldsWithRequirements,
|
|
4465
|
+
children: jsxRuntime.jsx(TemplateFieldProvider, {
|
|
4466
|
+
fieldEntries: fieldEntries,
|
|
4467
|
+
accessedFields: accessedFields,
|
|
4468
|
+
children: jsxRuntime.jsx("form", {
|
|
4469
|
+
ref: formRef,
|
|
4470
|
+
onSubmit: handleSubmit,
|
|
4471
|
+
className: className,
|
|
4472
|
+
children: renderFn(renderProps)
|
|
4473
|
+
})
|
|
4086
4474
|
})
|
|
4087
4475
|
})
|
|
4088
4476
|
});
|
|
@@ -4101,20 +4489,30 @@ exports.FieldRenderer = FieldRenderer;
|
|
|
4101
4489
|
exports.FieldsProvider = FieldsProvider;
|
|
4102
4490
|
exports.FormGenerator = FormGenerator;
|
|
4103
4491
|
exports.FormProvider = FormProvider;
|
|
4492
|
+
exports.RemainingFields = RemainingFields;
|
|
4104
4493
|
exports.StrictFormGenerator = StrictFormGenerator;
|
|
4494
|
+
exports.analyzeSchema = analyzeSchema;
|
|
4105
4495
|
exports.clearAllRegistries = clearAllRegistries;
|
|
4106
4496
|
exports.clearFieldRegistry = clearFieldRegistry;
|
|
4107
4497
|
exports.clearFormComponentRegistry = clearFormComponentRegistry;
|
|
4108
4498
|
exports.createArrayField = createArrayField;
|
|
4109
4499
|
exports.createField = createField;
|
|
4110
4500
|
exports.createFieldFactory = createFieldFactory;
|
|
4501
|
+
exports.getDateConstraints = getDateConstraints;
|
|
4111
4502
|
exports.getFieldComponent = getFieldComponent;
|
|
4112
4503
|
exports.getFormComponent = getFormComponent;
|
|
4113
4504
|
exports.getFormComponents = getFormComponents;
|
|
4505
|
+
exports.getNumberConstraints = getNumberConstraints;
|
|
4114
4506
|
exports.getRegisteredFieldTypes = getRegisteredFieldTypes;
|
|
4507
|
+
exports.getSchemaRequirements = getSchemaRequirements;
|
|
4508
|
+
exports.getSchemaTypeName = getSchemaTypeName;
|
|
4509
|
+
exports.getStringConstraints = getStringConstraints;
|
|
4115
4510
|
exports.hasFieldType = hasFieldType;
|
|
4116
4511
|
exports.hasFormComponent = hasFormComponent;
|
|
4117
4512
|
exports.isArrayField = isArrayField;
|
|
4513
|
+
exports.isSchemaRequired = isSchemaRequired;
|
|
4514
|
+
exports.mergeSchemaConstraints = mergeSchemaConstraints;
|
|
4515
|
+
exports.mergeSchemaRequirements = mergeSchemaRequirements;
|
|
4118
4516
|
exports.registerField = registerField;
|
|
4119
4517
|
exports.registerFields = registerFields;
|
|
4120
4518
|
exports.registerFormComponent = registerFormComponent;
|
|
@@ -4124,12 +4522,14 @@ exports.strictFields = strictFields;
|
|
|
4124
4522
|
exports.typedField = typedField;
|
|
4125
4523
|
exports.typedFields = typedFields;
|
|
4126
4524
|
exports.unregisterField = unregisterField;
|
|
4525
|
+
exports.unwrapSchema = unwrapSchema;
|
|
4127
4526
|
exports.useArrayField = useArrayField;
|
|
4128
4527
|
exports.useFieldArray = useFieldArray;
|
|
4129
4528
|
exports.useFieldProps = useFieldProps;
|
|
4130
4529
|
exports.useFieldsContext = useFieldsContext;
|
|
4131
4530
|
exports.useForm = useForm;
|
|
4132
4531
|
exports.useFormContext = useFormContext;
|
|
4532
|
+
exports.useTemplateField = useTemplateField;
|
|
4133
4533
|
exports.useWatch = useWatch;
|
|
4134
4534
|
exports.validateFieldType = validateFieldType;
|
|
4135
4535
|
exports.validateFieldTypes = validateFieldTypes;
|