@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.mjs
CHANGED
|
@@ -3284,7 +3284,7 @@ const ArrayFieldRenderer = /*#__PURE__*/memo(t0 => {
|
|
|
3284
3284
|
remove: () => remove(index_0),
|
|
3285
3285
|
move: toIndex => move(index_0, toIndex),
|
|
3286
3286
|
fields: renderItem(index_0),
|
|
3287
|
-
fieldNames: arrayField.fields.map(_temp),
|
|
3287
|
+
fieldNames: arrayField.fields.map(_temp$1),
|
|
3288
3288
|
namePrefix: `${field.name}.${index_0}`
|
|
3289
3289
|
});
|
|
3290
3290
|
};
|
|
@@ -3323,7 +3323,7 @@ const ArrayFieldRenderer = /*#__PURE__*/memo(t0 => {
|
|
|
3323
3323
|
return t1;
|
|
3324
3324
|
});
|
|
3325
3325
|
ArrayFieldRenderer.displayName = 'ArrayFieldRenderer';
|
|
3326
|
-
function _temp(f) {
|
|
3326
|
+
function _temp$1(f) {
|
|
3327
3327
|
return f.name;
|
|
3328
3328
|
}
|
|
3329
3329
|
|
|
@@ -3384,7 +3384,10 @@ function createTemplateFields(fieldEntries) {
|
|
|
3384
3384
|
return undefined;
|
|
3385
3385
|
}
|
|
3386
3386
|
};
|
|
3387
|
-
return
|
|
3387
|
+
return {
|
|
3388
|
+
templateFields: new Proxy({}, handler),
|
|
3389
|
+
accessedFields
|
|
3390
|
+
};
|
|
3388
3391
|
}
|
|
3389
3392
|
|
|
3390
3393
|
const FieldsContext = /*#__PURE__*/createContext(null);
|
|
@@ -3548,6 +3551,232 @@ function useFieldsContext() {
|
|
|
3548
3551
|
return useContext(FieldsContext);
|
|
3549
3552
|
}
|
|
3550
3553
|
|
|
3554
|
+
function isSchemaRequired(schema) {
|
|
3555
|
+
return !schema.isOptional() && !schema.isNullable();
|
|
3556
|
+
}
|
|
3557
|
+
function unwrapSchema(schema) {
|
|
3558
|
+
let current = schema;
|
|
3559
|
+
while (current instanceof z.ZodOptional || current instanceof z.ZodNullable) {
|
|
3560
|
+
current = current.unwrap();
|
|
3561
|
+
}
|
|
3562
|
+
if (current instanceof z.ZodDefault) {
|
|
3563
|
+
current = current._def.innerType;
|
|
3564
|
+
}
|
|
3565
|
+
return current;
|
|
3566
|
+
}
|
|
3567
|
+
function getSchemaTypeName(schema) {
|
|
3568
|
+
const unwrapped = unwrapSchema(schema);
|
|
3569
|
+
if (unwrapped instanceof z.ZodString) return 'string';
|
|
3570
|
+
if (unwrapped instanceof z.ZodNumber) return 'number';
|
|
3571
|
+
if (unwrapped instanceof z.ZodBoolean) return 'boolean';
|
|
3572
|
+
if (unwrapped instanceof z.ZodDate) return 'date';
|
|
3573
|
+
if (unwrapped instanceof z.ZodArray) return 'array';
|
|
3574
|
+
if (unwrapped instanceof z.ZodObject) return 'object';
|
|
3575
|
+
if (unwrapped instanceof z.ZodEnum) return 'enum';
|
|
3576
|
+
return 'unknown';
|
|
3577
|
+
}
|
|
3578
|
+
function getCheckDef(check) {
|
|
3579
|
+
var _a;
|
|
3580
|
+
if ((_a = check._zod) === null || _a === void 0 ? void 0 : _a.def) {
|
|
3581
|
+
const def = check._zod.def;
|
|
3582
|
+
return {
|
|
3583
|
+
type: def.check,
|
|
3584
|
+
value: def.value,
|
|
3585
|
+
minimum: def.minimum,
|
|
3586
|
+
maximum: def.maximum,
|
|
3587
|
+
length: def.length,
|
|
3588
|
+
regex: def.pattern || def.regex,
|
|
3589
|
+
format: def.format,
|
|
3590
|
+
inclusive: def.inclusive
|
|
3591
|
+
};
|
|
3592
|
+
}
|
|
3593
|
+
if (check.kind) {
|
|
3594
|
+
return {
|
|
3595
|
+
type: check.kind,
|
|
3596
|
+
value: check.value,
|
|
3597
|
+
regex: check.regex
|
|
3598
|
+
};
|
|
3599
|
+
}
|
|
3600
|
+
return null;
|
|
3601
|
+
}
|
|
3602
|
+
function getNumberConstraints(schema) {
|
|
3603
|
+
const unwrapped = unwrapSchema(schema);
|
|
3604
|
+
if (!(unwrapped instanceof z.ZodNumber)) {
|
|
3605
|
+
return {};
|
|
3606
|
+
}
|
|
3607
|
+
const constraints = {};
|
|
3608
|
+
const checks = unwrapped._def.checks;
|
|
3609
|
+
if (checks) {
|
|
3610
|
+
for (const check of checks) {
|
|
3611
|
+
const def = getCheckDef(check);
|
|
3612
|
+
if (!def) continue;
|
|
3613
|
+
if (def.type === 'greater_than' || def.type === 'min') {
|
|
3614
|
+
constraints.min = def.value;
|
|
3615
|
+
} else if (def.type === 'less_than' || def.type === 'max') {
|
|
3616
|
+
constraints.max = def.value;
|
|
3617
|
+
} else if (def.type === 'multiple_of' || def.type === 'multipleOf') {
|
|
3618
|
+
constraints.step = def.value;
|
|
3619
|
+
} else if (def.type === 'number_format' && (def.format === 'safeint' || def.format === 'int')) {
|
|
3620
|
+
constraints.step = 1;
|
|
3621
|
+
} else if (def.type === 'int' || def.type === 'integer') {
|
|
3622
|
+
constraints.step = 1;
|
|
3623
|
+
}
|
|
3624
|
+
}
|
|
3625
|
+
}
|
|
3626
|
+
return constraints;
|
|
3627
|
+
}
|
|
3628
|
+
function getStringConstraints(schema) {
|
|
3629
|
+
const unwrapped = unwrapSchema(schema);
|
|
3630
|
+
if (!(unwrapped instanceof z.ZodString)) {
|
|
3631
|
+
return {};
|
|
3632
|
+
}
|
|
3633
|
+
const constraints = {};
|
|
3634
|
+
const checks = unwrapped._def.checks;
|
|
3635
|
+
if (checks) {
|
|
3636
|
+
for (const check of checks) {
|
|
3637
|
+
const def = getCheckDef(check);
|
|
3638
|
+
if (!def) continue;
|
|
3639
|
+
if (def.type === 'min_length' && def.minimum !== undefined) {
|
|
3640
|
+
constraints.minLength = def.minimum;
|
|
3641
|
+
} else if (def.type === 'min' && def.value !== undefined) {
|
|
3642
|
+
constraints.minLength = def.value;
|
|
3643
|
+
} else if (def.type === 'max_length' && def.maximum !== undefined) {
|
|
3644
|
+
constraints.maxLength = def.maximum;
|
|
3645
|
+
} else if (def.type === 'max' && def.value !== undefined) {
|
|
3646
|
+
constraints.maxLength = def.value;
|
|
3647
|
+
} else if (def.type === 'length_equals' && def.length !== undefined) {
|
|
3648
|
+
constraints.minLength = def.length;
|
|
3649
|
+
constraints.maxLength = def.length;
|
|
3650
|
+
} else if (def.type === 'length' && def.value !== undefined) {
|
|
3651
|
+
constraints.minLength = def.value;
|
|
3652
|
+
constraints.maxLength = def.value;
|
|
3653
|
+
} else if (def.type === 'string_format' && def.regex) {
|
|
3654
|
+
constraints.pattern = def.regex.source;
|
|
3655
|
+
} else if (def.type === 'regex' && def.regex) {
|
|
3656
|
+
constraints.pattern = def.regex.source;
|
|
3657
|
+
}
|
|
3658
|
+
}
|
|
3659
|
+
}
|
|
3660
|
+
return constraints;
|
|
3661
|
+
}
|
|
3662
|
+
function getDateConstraints(schema) {
|
|
3663
|
+
const unwrapped = unwrapSchema(schema);
|
|
3664
|
+
if (!(unwrapped instanceof z.ZodDate)) {
|
|
3665
|
+
return {};
|
|
3666
|
+
}
|
|
3667
|
+
const constraints = {};
|
|
3668
|
+
const checks = unwrapped._def.checks;
|
|
3669
|
+
if (checks) {
|
|
3670
|
+
for (const check of checks) {
|
|
3671
|
+
const def = getCheckDef(check);
|
|
3672
|
+
if (!def) continue;
|
|
3673
|
+
if (def.type === 'greater_than' || def.type === 'min') {
|
|
3674
|
+
const value = def.value;
|
|
3675
|
+
constraints.min = value instanceof Date ? value : new Date(value);
|
|
3676
|
+
} else if (def.type === 'less_than' || def.type === 'max') {
|
|
3677
|
+
const value = def.value;
|
|
3678
|
+
constraints.max = value instanceof Date ? value : new Date(value);
|
|
3679
|
+
}
|
|
3680
|
+
}
|
|
3681
|
+
}
|
|
3682
|
+
return constraints;
|
|
3683
|
+
}
|
|
3684
|
+
function getSchemaRequirements(schema) {
|
|
3685
|
+
const result = {};
|
|
3686
|
+
const shape = schema.shape;
|
|
3687
|
+
for (const key in shape) {
|
|
3688
|
+
result[key] = isSchemaRequired(shape[key]);
|
|
3689
|
+
}
|
|
3690
|
+
return result;
|
|
3691
|
+
}
|
|
3692
|
+
function analyzeSchema(schema) {
|
|
3693
|
+
const shape = schema.shape;
|
|
3694
|
+
return Object.entries(shape).map(([name, fieldSchema]) => {
|
|
3695
|
+
const type = getSchemaTypeName(fieldSchema);
|
|
3696
|
+
const info = {
|
|
3697
|
+
name,
|
|
3698
|
+
required: isSchemaRequired(fieldSchema),
|
|
3699
|
+
type,
|
|
3700
|
+
schema: fieldSchema
|
|
3701
|
+
};
|
|
3702
|
+
if (type === 'number') {
|
|
3703
|
+
const constraints = getNumberConstraints(fieldSchema);
|
|
3704
|
+
if (constraints.min !== undefined) info.min = constraints.min;
|
|
3705
|
+
if (constraints.max !== undefined) info.max = constraints.max;
|
|
3706
|
+
if (constraints.step !== undefined) info.step = constraints.step;
|
|
3707
|
+
} else if (type === 'string') {
|
|
3708
|
+
const constraints = getStringConstraints(fieldSchema);
|
|
3709
|
+
if (constraints.minLength !== undefined) info.minLength = constraints.minLength;
|
|
3710
|
+
if (constraints.maxLength !== undefined) info.maxLength = constraints.maxLength;
|
|
3711
|
+
if (constraints.pattern !== undefined) info.pattern = constraints.pattern;
|
|
3712
|
+
} else if (type === 'date') {
|
|
3713
|
+
const constraints = getDateConstraints(fieldSchema);
|
|
3714
|
+
if (constraints.min !== undefined) info.minDate = constraints.min;
|
|
3715
|
+
if (constraints.max !== undefined) info.maxDate = constraints.max;
|
|
3716
|
+
}
|
|
3717
|
+
return info;
|
|
3718
|
+
});
|
|
3719
|
+
}
|
|
3720
|
+
function mergeSchemaRequirements(schema, fields) {
|
|
3721
|
+
const requirements = getSchemaRequirements(schema);
|
|
3722
|
+
return fields.map(field => {
|
|
3723
|
+
const schemaRequired = requirements[field.name];
|
|
3724
|
+
if (schemaRequired !== undefined) {
|
|
3725
|
+
return {
|
|
3726
|
+
...field,
|
|
3727
|
+
required: schemaRequired
|
|
3728
|
+
};
|
|
3729
|
+
}
|
|
3730
|
+
return field;
|
|
3731
|
+
});
|
|
3732
|
+
}
|
|
3733
|
+
function mergeSchemaConstraints(schema, fields) {
|
|
3734
|
+
const fieldInfoMap = new Map();
|
|
3735
|
+
analyzeSchema(schema).forEach(info => {
|
|
3736
|
+
fieldInfoMap.set(info.name, info);
|
|
3737
|
+
});
|
|
3738
|
+
return fields.map(field => {
|
|
3739
|
+
const schemaInfo = fieldInfoMap.get(field.name);
|
|
3740
|
+
if (!schemaInfo) {
|
|
3741
|
+
return field;
|
|
3742
|
+
}
|
|
3743
|
+
const merged = {
|
|
3744
|
+
...field,
|
|
3745
|
+
required: schemaInfo.required
|
|
3746
|
+
};
|
|
3747
|
+
if (schemaInfo.type === 'number') {
|
|
3748
|
+
if (schemaInfo.min !== undefined) {
|
|
3749
|
+
merged.min = schemaInfo.min;
|
|
3750
|
+
}
|
|
3751
|
+
if (schemaInfo.max !== undefined) {
|
|
3752
|
+
merged.max = schemaInfo.max;
|
|
3753
|
+
}
|
|
3754
|
+
if (schemaInfo.step !== undefined) {
|
|
3755
|
+
merged.step = schemaInfo.step;
|
|
3756
|
+
}
|
|
3757
|
+
}
|
|
3758
|
+
if (schemaInfo.type === 'string') {
|
|
3759
|
+
if (schemaInfo.minLength !== undefined) {
|
|
3760
|
+
merged.minLength = schemaInfo.minLength;
|
|
3761
|
+
}
|
|
3762
|
+
if (schemaInfo.maxLength !== undefined) {
|
|
3763
|
+
merged.maxLength = schemaInfo.maxLength;
|
|
3764
|
+
}
|
|
3765
|
+
if (schemaInfo.pattern !== undefined) {
|
|
3766
|
+
merged.pattern = schemaInfo.pattern;
|
|
3767
|
+
}
|
|
3768
|
+
}
|
|
3769
|
+
if (schemaInfo.type === 'date') {
|
|
3770
|
+
if (schemaInfo.minDate !== undefined) {
|
|
3771
|
+
merged.min = schemaInfo.minDate.toISOString().split('T')[0];
|
|
3772
|
+
}
|
|
3773
|
+
if (schemaInfo.maxDate !== undefined) {
|
|
3774
|
+
merged.max = schemaInfo.maxDate.toISOString().split('T')[0];
|
|
3775
|
+
}
|
|
3776
|
+
}
|
|
3777
|
+
return merged;
|
|
3778
|
+
});
|
|
3779
|
+
}
|
|
3551
3780
|
function deepMerge(target, source) {
|
|
3552
3781
|
const result = {
|
|
3553
3782
|
...target
|
|
@@ -3610,6 +3839,33 @@ function buildArrayItemSchema(arrayField) {
|
|
|
3610
3839
|
}
|
|
3611
3840
|
return z.object(itemShape);
|
|
3612
3841
|
}
|
|
3842
|
+
function setNestedShape(shape, path, validator) {
|
|
3843
|
+
const parts = path.split('.');
|
|
3844
|
+
if (parts.length === 1) {
|
|
3845
|
+
shape[path] = validator;
|
|
3846
|
+
return;
|
|
3847
|
+
}
|
|
3848
|
+
const [head, ...rest] = parts;
|
|
3849
|
+
const nestedPath = rest.join('.');
|
|
3850
|
+
const existingNested = shape[head];
|
|
3851
|
+
const nestedShape = existingNested ? {
|
|
3852
|
+
...existingNested.shape
|
|
3853
|
+
} : {};
|
|
3854
|
+
setNestedShape(nestedShape, nestedPath, validator);
|
|
3855
|
+
shape[head] = z.object(nestedShape);
|
|
3856
|
+
}
|
|
3857
|
+
function setNestedValue(obj, path, value) {
|
|
3858
|
+
const parts = path.split('.');
|
|
3859
|
+
if (parts.length === 1) {
|
|
3860
|
+
obj[path] = value;
|
|
3861
|
+
return;
|
|
3862
|
+
}
|
|
3863
|
+
const [head, ...rest] = parts;
|
|
3864
|
+
if (!(head in obj) || typeof obj[head] !== 'object' || obj[head] === null) {
|
|
3865
|
+
obj[head] = {};
|
|
3866
|
+
}
|
|
3867
|
+
setNestedValue(obj[head], rest.join('.'), value);
|
|
3868
|
+
}
|
|
3613
3869
|
function buildSchema(fields) {
|
|
3614
3870
|
const shape = {};
|
|
3615
3871
|
for (const field of fields) {
|
|
@@ -3621,10 +3877,10 @@ function buildSchema(fields) {
|
|
|
3621
3877
|
if (field.maxItems !== undefined) {
|
|
3622
3878
|
arraySchema = arraySchema.max(field.maxItems);
|
|
3623
3879
|
}
|
|
3624
|
-
shape
|
|
3880
|
+
setNestedShape(shape, field.name, arraySchema);
|
|
3625
3881
|
} else {
|
|
3626
3882
|
const validator = getValidatorForField(field);
|
|
3627
|
-
shape
|
|
3883
|
+
setNestedShape(shape, field.name, field.required ? validator : validator.optional());
|
|
3628
3884
|
}
|
|
3629
3885
|
}
|
|
3630
3886
|
return z.object(shape);
|
|
@@ -3652,12 +3908,84 @@ function buildDefaultValues(fields) {
|
|
|
3652
3908
|
values[field.name] = [];
|
|
3653
3909
|
}
|
|
3654
3910
|
} else {
|
|
3655
|
-
values
|
|
3911
|
+
setNestedValue(values, field.name, (_a = field.defaultValue) !== null && _a !== void 0 ? _a : '');
|
|
3656
3912
|
}
|
|
3657
3913
|
}
|
|
3658
3914
|
return values;
|
|
3659
3915
|
}
|
|
3660
3916
|
|
|
3917
|
+
const TemplateFieldContext = /*#__PURE__*/createContext(null);
|
|
3918
|
+
function TemplateFieldProvider({
|
|
3919
|
+
children,
|
|
3920
|
+
fieldEntries,
|
|
3921
|
+
accessedFields
|
|
3922
|
+
}) {
|
|
3923
|
+
return jsx(TemplateFieldContext.Provider, {
|
|
3924
|
+
value: {
|
|
3925
|
+
fieldEntries,
|
|
3926
|
+
accessedFields
|
|
3927
|
+
},
|
|
3928
|
+
children: children
|
|
3929
|
+
});
|
|
3930
|
+
}
|
|
3931
|
+
function useTemplateField(name) {
|
|
3932
|
+
const $ = c(3);
|
|
3933
|
+
const context = useContext(TemplateFieldContext);
|
|
3934
|
+
if (!context) {
|
|
3935
|
+
throw new Error("useTemplateField must be used within FormGenerator with a custom layout (children render function).");
|
|
3936
|
+
}
|
|
3937
|
+
const {
|
|
3938
|
+
fieldEntries,
|
|
3939
|
+
accessedFields
|
|
3940
|
+
} = context;
|
|
3941
|
+
let t0;
|
|
3942
|
+
if ($[0] !== fieldEntries || $[1] !== name) {
|
|
3943
|
+
t0 = fieldEntries.get(name);
|
|
3944
|
+
$[0] = fieldEntries;
|
|
3945
|
+
$[1] = name;
|
|
3946
|
+
$[2] = t0;
|
|
3947
|
+
} else {
|
|
3948
|
+
t0 = $[2];
|
|
3949
|
+
}
|
|
3950
|
+
const entry = t0;
|
|
3951
|
+
if (entry) {
|
|
3952
|
+
accessedFields.add(name);
|
|
3953
|
+
return entry.element;
|
|
3954
|
+
}
|
|
3955
|
+
}
|
|
3956
|
+
function RemainingFields() {
|
|
3957
|
+
const $ = c(3);
|
|
3958
|
+
const context = useContext(TemplateFieldContext);
|
|
3959
|
+
if (!context) {
|
|
3960
|
+
throw new Error("RemainingFields must be used within FormGenerator with a custom layout (children render function).");
|
|
3961
|
+
}
|
|
3962
|
+
const {
|
|
3963
|
+
fieldEntries,
|
|
3964
|
+
accessedFields
|
|
3965
|
+
} = context;
|
|
3966
|
+
let t0;
|
|
3967
|
+
if ($[0] !== accessedFields || $[1] !== fieldEntries) {
|
|
3968
|
+
const remaining = Array.from(fieldEntries.entries()).filter(t1 => {
|
|
3969
|
+
const [name] = t1;
|
|
3970
|
+
return !accessedFields.has(name);
|
|
3971
|
+
}).map(_temp);
|
|
3972
|
+
t0 = jsx(Fragment$1, {
|
|
3973
|
+
children: remaining
|
|
3974
|
+
});
|
|
3975
|
+
$[0] = accessedFields;
|
|
3976
|
+
$[1] = fieldEntries;
|
|
3977
|
+
$[2] = t0;
|
|
3978
|
+
} else {
|
|
3979
|
+
t0 = $[2];
|
|
3980
|
+
}
|
|
3981
|
+
return t0;
|
|
3982
|
+
}
|
|
3983
|
+
function _temp(t0) {
|
|
3984
|
+
const [, entry] = t0;
|
|
3985
|
+
return entry.element;
|
|
3986
|
+
}
|
|
3987
|
+
RemainingFields.displayName = 'RemainingFields';
|
|
3988
|
+
|
|
3661
3989
|
function FormGenerator(props) {
|
|
3662
3990
|
const {
|
|
3663
3991
|
fields,
|
|
@@ -3697,17 +4025,31 @@ function FormGenerator(props) {
|
|
|
3697
4025
|
mode
|
|
3698
4026
|
});
|
|
3699
4027
|
const handleSubmit = form.handleSubmit(async data => {
|
|
3700
|
-
|
|
4028
|
+
const context = {
|
|
4029
|
+
isDirty: form.formState.isDirty,
|
|
4030
|
+
dirtyFields: form.formState.dirtyFields,
|
|
4031
|
+
isValid: form.formState.isValid,
|
|
4032
|
+
errors: form.formState.errors,
|
|
4033
|
+
touchedFields: form.formState.touchedFields,
|
|
4034
|
+
isSubmitting: form.formState.isSubmitting,
|
|
4035
|
+
submitCount: form.formState.submitCount,
|
|
4036
|
+
defaultValues: mergedDefaultValues,
|
|
4037
|
+
form: form
|
|
4038
|
+
};
|
|
4039
|
+
await onSubmit(data, context);
|
|
3701
4040
|
});
|
|
3702
4041
|
const handleReset = useCallback(() => {
|
|
3703
4042
|
form.reset(mergedDefaultValues);
|
|
3704
4043
|
}, [form, mergedDefaultValues]);
|
|
3705
4044
|
useImperativeHandle(ref, () => ({
|
|
3706
|
-
setValues: values => {
|
|
4045
|
+
setValues: (values, options_0) => {
|
|
4046
|
+
var _a, _b;
|
|
4047
|
+
const shouldDirty = (_a = options_0 === null || options_0 === void 0 ? void 0 : options_0.shouldDirty) !== null && _a !== void 0 ? _a : true;
|
|
4048
|
+
const shouldValidate = (_b = options_0 === null || options_0 === void 0 ? void 0 : options_0.shouldValidate) !== null && _b !== void 0 ? _b : true;
|
|
3707
4049
|
Object.entries(values).forEach(([key, value]) => {
|
|
3708
4050
|
form.setValue(key, value, {
|
|
3709
|
-
shouldValidate
|
|
3710
|
-
shouldDirty
|
|
4051
|
+
shouldValidate,
|
|
4052
|
+
shouldDirty
|
|
3711
4053
|
});
|
|
3712
4054
|
});
|
|
3713
4055
|
},
|
|
@@ -3719,6 +4061,12 @@ function FormGenerator(props) {
|
|
|
3719
4061
|
form.reset(mergedDefaultValues);
|
|
3720
4062
|
}
|
|
3721
4063
|
},
|
|
4064
|
+
setDefaultValues: values_1 => {
|
|
4065
|
+
const newDefaults = deepMerge(mergedDefaultValues, values_1);
|
|
4066
|
+
form.reset(newDefaults, {
|
|
4067
|
+
keepDirtyValues: false
|
|
4068
|
+
});
|
|
4069
|
+
},
|
|
3722
4070
|
submit: async () => {
|
|
3723
4071
|
await handleSubmit();
|
|
3724
4072
|
},
|
|
@@ -3770,7 +4118,10 @@ function FormGenerator(props) {
|
|
|
3770
4118
|
});
|
|
3771
4119
|
return entries;
|
|
3772
4120
|
}, [regularFields, FieldWrapper]);
|
|
3773
|
-
const
|
|
4121
|
+
const {
|
|
4122
|
+
templateFields,
|
|
4123
|
+
accessedFields
|
|
4124
|
+
} = useMemo(() => createTemplateFields(fieldEntries), [fieldEntries]);
|
|
3774
4125
|
const buttons = useMemo(() => {
|
|
3775
4126
|
const result = {
|
|
3776
4127
|
submit: jsx(SubmitButton, {
|
|
@@ -3789,10 +4140,10 @@ function FormGenerator(props) {
|
|
|
3789
4140
|
}
|
|
3790
4141
|
return result;
|
|
3791
4142
|
}, [SubmitButton, disabled, form.formState.isSubmitting, submitText, showReset, resetText, handleReset]);
|
|
3792
|
-
const renderField = useCallback((field_1,
|
|
4143
|
+
const renderField = useCallback((field_1, options_1) => {
|
|
3793
4144
|
return jsx(FieldRenderer, {
|
|
3794
4145
|
field: field_1,
|
|
3795
|
-
namePrefix:
|
|
4146
|
+
namePrefix: options_1 === null || options_1 === void 0 ? void 0 : options_1.namePrefix
|
|
3796
4147
|
});
|
|
3797
4148
|
}, []);
|
|
3798
4149
|
if (!children) {
|
|
@@ -3850,11 +4201,15 @@ function FormGenerator(props) {
|
|
|
3850
4201
|
...form,
|
|
3851
4202
|
children: jsx(FieldsProvider, {
|
|
3852
4203
|
fields: fields,
|
|
3853
|
-
children: jsx(
|
|
3854
|
-
|
|
3855
|
-
|
|
3856
|
-
|
|
3857
|
-
|
|
4204
|
+
children: jsx(TemplateFieldProvider, {
|
|
4205
|
+
fieldEntries: fieldEntries,
|
|
4206
|
+
accessedFields: accessedFields,
|
|
4207
|
+
children: jsx("form", {
|
|
4208
|
+
ref: formRef,
|
|
4209
|
+
onSubmit: handleSubmit,
|
|
4210
|
+
className: className,
|
|
4211
|
+
children: renderFn(renderProps)
|
|
4212
|
+
})
|
|
3858
4213
|
})
|
|
3859
4214
|
})
|
|
3860
4215
|
});
|
|
@@ -3885,6 +4240,12 @@ function StrictFormGenerator(props) {
|
|
|
3885
4240
|
validateTypes
|
|
3886
4241
|
} = props;
|
|
3887
4242
|
const formRef = useRef(null);
|
|
4243
|
+
const fieldsWithRequirements = useMemo(() => {
|
|
4244
|
+
if (schema && typeof schema === 'object' && 'shape' in schema) {
|
|
4245
|
+
return mergeSchemaConstraints(schema, fields);
|
|
4246
|
+
}
|
|
4247
|
+
return fields;
|
|
4248
|
+
}, [schema, fields]);
|
|
3888
4249
|
useMemo(() => {
|
|
3889
4250
|
if (validateTypes) {
|
|
3890
4251
|
const registeredTypes = getRegisteredFieldTypes();
|
|
@@ -3892,30 +4253,44 @@ function StrictFormGenerator(props) {
|
|
|
3892
4253
|
throwOnError: false,
|
|
3893
4254
|
warn: true
|
|
3894
4255
|
};
|
|
3895
|
-
validateFieldTypes(
|
|
4256
|
+
validateFieldTypes(fieldsWithRequirements, registeredTypes, options);
|
|
3896
4257
|
}
|
|
3897
|
-
}, [
|
|
4258
|
+
}, [fieldsWithRequirements, validateTypes]);
|
|
3898
4259
|
const mergedDefaultValues = useMemo(() => {
|
|
3899
|
-
const builtDefaults = buildDefaultValues(
|
|
4260
|
+
const builtDefaults = buildDefaultValues(fieldsWithRequirements);
|
|
3900
4261
|
return defaultValues ? deepMerge(builtDefaults, defaultValues) : builtDefaults;
|
|
3901
|
-
}, [
|
|
4262
|
+
}, [fieldsWithRequirements, defaultValues]);
|
|
3902
4263
|
const form = useForm({
|
|
3903
4264
|
resolver: a(schema),
|
|
3904
4265
|
defaultValues: mergedDefaultValues,
|
|
3905
4266
|
mode
|
|
3906
4267
|
});
|
|
3907
4268
|
const handleSubmit = form.handleSubmit(async data => {
|
|
3908
|
-
|
|
4269
|
+
const context = {
|
|
4270
|
+
isDirty: form.formState.isDirty,
|
|
4271
|
+
dirtyFields: form.formState.dirtyFields,
|
|
4272
|
+
isValid: form.formState.isValid,
|
|
4273
|
+
errors: form.formState.errors,
|
|
4274
|
+
touchedFields: form.formState.touchedFields,
|
|
4275
|
+
isSubmitting: form.formState.isSubmitting,
|
|
4276
|
+
submitCount: form.formState.submitCount,
|
|
4277
|
+
defaultValues: mergedDefaultValues,
|
|
4278
|
+
form: form
|
|
4279
|
+
};
|
|
4280
|
+
await onSubmit(data, context);
|
|
3909
4281
|
});
|
|
3910
4282
|
const handleReset = useCallback(() => {
|
|
3911
4283
|
form.reset(mergedDefaultValues);
|
|
3912
4284
|
}, [form, mergedDefaultValues]);
|
|
3913
4285
|
useImperativeHandle(ref, () => ({
|
|
3914
|
-
setValues: values => {
|
|
4286
|
+
setValues: (values, options_0) => {
|
|
4287
|
+
var _a, _b;
|
|
4288
|
+
const shouldDirty = (_a = options_0 === null || options_0 === void 0 ? void 0 : options_0.shouldDirty) !== null && _a !== void 0 ? _a : true;
|
|
4289
|
+
const shouldValidate = (_b = options_0 === null || options_0 === void 0 ? void 0 : options_0.shouldValidate) !== null && _b !== void 0 ? _b : true;
|
|
3915
4290
|
Object.entries(values).forEach(([key, value]) => {
|
|
3916
4291
|
form.setValue(key, value, {
|
|
3917
|
-
shouldValidate
|
|
3918
|
-
shouldDirty
|
|
4292
|
+
shouldValidate,
|
|
4293
|
+
shouldDirty
|
|
3919
4294
|
});
|
|
3920
4295
|
});
|
|
3921
4296
|
},
|
|
@@ -3927,6 +4302,12 @@ function StrictFormGenerator(props) {
|
|
|
3927
4302
|
form.reset(mergedDefaultValues);
|
|
3928
4303
|
}
|
|
3929
4304
|
},
|
|
4305
|
+
setDefaultValues: values_1 => {
|
|
4306
|
+
const newDefaults = deepMerge(mergedDefaultValues, values_1);
|
|
4307
|
+
form.reset(newDefaults, {
|
|
4308
|
+
keepDirtyValues: false
|
|
4309
|
+
});
|
|
4310
|
+
},
|
|
3930
4311
|
submit: async () => {
|
|
3931
4312
|
await handleSubmit();
|
|
3932
4313
|
},
|
|
@@ -3945,7 +4326,7 @@ function StrictFormGenerator(props) {
|
|
|
3945
4326
|
} = useMemo(() => {
|
|
3946
4327
|
const regular = [];
|
|
3947
4328
|
const arrays = [];
|
|
3948
|
-
|
|
4329
|
+
fieldsWithRequirements.forEach(field => {
|
|
3949
4330
|
if (isArrayField(field)) {
|
|
3950
4331
|
arrays.push(field);
|
|
3951
4332
|
} else {
|
|
@@ -3956,7 +4337,7 @@ function StrictFormGenerator(props) {
|
|
|
3956
4337
|
regularFields: regular,
|
|
3957
4338
|
arrayFields: arrays
|
|
3958
4339
|
};
|
|
3959
|
-
}, [
|
|
4340
|
+
}, [fieldsWithRequirements]);
|
|
3960
4341
|
const fieldEntries = useMemo(() => {
|
|
3961
4342
|
const entries = new Map();
|
|
3962
4343
|
regularFields.forEach((field_0, index) => {
|
|
@@ -3978,7 +4359,10 @@ function StrictFormGenerator(props) {
|
|
|
3978
4359
|
});
|
|
3979
4360
|
return entries;
|
|
3980
4361
|
}, [regularFields, FieldWrapper]);
|
|
3981
|
-
const
|
|
4362
|
+
const {
|
|
4363
|
+
templateFields,
|
|
4364
|
+
accessedFields
|
|
4365
|
+
} = useMemo(() => createTemplateFields(fieldEntries), [fieldEntries]);
|
|
3982
4366
|
const buttons = useMemo(() => {
|
|
3983
4367
|
const result = {
|
|
3984
4368
|
submit: jsx(SubmitButton, {
|
|
@@ -3997,17 +4381,17 @@ function StrictFormGenerator(props) {
|
|
|
3997
4381
|
}
|
|
3998
4382
|
return result;
|
|
3999
4383
|
}, [SubmitButton, disabled, form.formState.isSubmitting, submitText, showReset, resetText, handleReset]);
|
|
4000
|
-
const renderField = useCallback((field_1,
|
|
4384
|
+
const renderField = useCallback((field_1, options_1) => {
|
|
4001
4385
|
return jsx(FieldRenderer, {
|
|
4002
4386
|
field: field_1,
|
|
4003
|
-
namePrefix:
|
|
4387
|
+
namePrefix: options_1 === null || options_1 === void 0 ? void 0 : options_1.namePrefix
|
|
4004
4388
|
});
|
|
4005
4389
|
}, []);
|
|
4006
4390
|
if (!children) {
|
|
4007
4391
|
return jsx(FormProvider, {
|
|
4008
4392
|
...form,
|
|
4009
4393
|
children: jsx(FieldsProvider, {
|
|
4010
|
-
fields:
|
|
4394
|
+
fields: fieldsWithRequirements,
|
|
4011
4395
|
children: jsxs("form", {
|
|
4012
4396
|
ref: formRef,
|
|
4013
4397
|
onSubmit: handleSubmit,
|
|
@@ -4057,12 +4441,16 @@ function StrictFormGenerator(props) {
|
|
|
4057
4441
|
return jsx(FormProvider, {
|
|
4058
4442
|
...form,
|
|
4059
4443
|
children: jsx(FieldsProvider, {
|
|
4060
|
-
fields:
|
|
4061
|
-
children: jsx(
|
|
4062
|
-
|
|
4063
|
-
|
|
4064
|
-
|
|
4065
|
-
|
|
4444
|
+
fields: fieldsWithRequirements,
|
|
4445
|
+
children: jsx(TemplateFieldProvider, {
|
|
4446
|
+
fieldEntries: fieldEntries,
|
|
4447
|
+
accessedFields: accessedFields,
|
|
4448
|
+
children: jsx("form", {
|
|
4449
|
+
ref: formRef,
|
|
4450
|
+
onSubmit: handleSubmit,
|
|
4451
|
+
className: className,
|
|
4452
|
+
children: renderFn(renderProps)
|
|
4453
|
+
})
|
|
4066
4454
|
})
|
|
4067
4455
|
})
|
|
4068
4456
|
});
|
|
@@ -4071,5 +4459,5 @@ function createFieldFactory(_schema) {
|
|
|
4071
4459
|
return field => field;
|
|
4072
4460
|
}
|
|
4073
4461
|
|
|
4074
|
-
export { ArrayFieldRenderer, Controller, FieldRenderer, FieldsProvider, FormGenerator, FormProvider, StrictFormGenerator, clearAllRegistries, clearFieldRegistry, clearFormComponentRegistry, createArrayField, createField, createFieldFactory, getFieldComponent, getFormComponent, getFormComponents, getRegisteredFieldTypes, hasFieldType, hasFormComponent, isArrayField, registerField, registerFields, registerFormComponent, registerFormComponents, resetFormComponentRegistry, strictFields, typedField, typedFields, unregisterField, useArrayField, useFieldArray, useFieldProps, useFieldsContext, useForm, useFormContext, useWatch, validateFieldType, validateFieldTypes };
|
|
4462
|
+
export { ArrayFieldRenderer, Controller, FieldRenderer, FieldsProvider, FormGenerator, FormProvider, RemainingFields, StrictFormGenerator, analyzeSchema, clearAllRegistries, clearFieldRegistry, clearFormComponentRegistry, createArrayField, createField, createFieldFactory, getDateConstraints, getFieldComponent, getFormComponent, getFormComponents, getNumberConstraints, getRegisteredFieldTypes, getSchemaRequirements, getSchemaTypeName, getStringConstraints, hasFieldType, hasFormComponent, isArrayField, isSchemaRequired, mergeSchemaConstraints, mergeSchemaRequirements, registerField, registerFields, registerFormComponent, registerFormComponents, resetFormComponentRegistry, strictFields, typedField, typedFields, unregisterField, unwrapSchema, useArrayField, useFieldArray, useFieldProps, useFieldsContext, useForm, useFormContext, useTemplateField, useWatch, validateFieldType, validateFieldTypes };
|
|
4075
4463
|
//# sourceMappingURL=index.mjs.map
|