@connect-soft/form-generator 1.0.0-alpha → 1.1.0-alpha10
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 +1102 -28
- package/dist/index.js +1184 -391
- package/dist/index.js.map +1 -1
- package/dist/{index.m.js → index.mjs} +1163 -389
- package/dist/index.mjs.map +1 -0
- package/dist/types/components/form/array-field-renderer.d.ts +39 -0
- package/dist/types/components/form/array-field-renderer.d.ts.map +1 -0
- package/dist/types/components/form/create-template-fields.d.ts +3 -0
- package/dist/types/components/form/create-template-fields.d.ts.map +1 -0
- package/dist/types/components/form/field-renderer.d.ts +1 -6
- package/dist/types/components/form/field-renderer.d.ts.map +1 -1
- package/dist/types/components/form/fields-context.d.ts +26 -0
- package/dist/types/components/form/fields-context.d.ts.map +1 -0
- package/dist/types/components/form/form-generator-typed.d.ts +47 -0
- package/dist/types/components/form/form-generator-typed.d.ts.map +1 -0
- package/dist/types/components/form/form-generator.d.ts +43 -6
- package/dist/types/components/form/form-generator.d.ts.map +1 -1
- package/dist/types/components/form/form-utils.d.ts +47 -0
- package/dist/types/components/form/form-utils.d.ts.map +1 -0
- package/dist/types/components/form/index.d.ts +5 -3
- package/dist/types/components/form/index.d.ts.map +1 -1
- package/dist/types/index.d.ts +13 -6
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/lib/field-registry.d.ts +19 -59
- package/dist/types/lib/field-registry.d.ts.map +1 -1
- package/dist/types/lib/field-types.d.ts +127 -26
- package/dist/types/lib/field-types.d.ts.map +1 -1
- package/dist/types/lib/index.d.ts +3 -4
- package/dist/types/lib/index.d.ts.map +1 -1
- package/dist/types/lib/template-types.d.ts +55 -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
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import React, { createElement, memo } from 'react';
|
|
1
|
+
import { jsx, jsxs, Fragment as Fragment$1 } from 'react/jsx-runtime';
|
|
2
|
+
import React, { createElement, Fragment, memo, useCallback, useContext, createContext, useRef, useMemo, useImperativeHandle } from 'react';
|
|
4
3
|
import * as n$1 from 'zod/v4/core';
|
|
4
|
+
import { c } from 'react/compiler-runtime';
|
|
5
5
|
import { z } from 'zod';
|
|
6
6
|
export { z } from 'zod';
|
|
7
7
|
|
|
@@ -2772,52 +2772,56 @@ const r=(t,r,o)=>{if(t&&"reportValidity"in t){const s=get(o,r);t.setCustomValidi
|
|
|
2772
2772
|
|
|
2773
2773
|
function t(r,e){try{var o=r();}catch(r){return e(r)}return o&&o.then?o.then(void 0,e):o}function s(r,e){for(var n={};r.length;){var t=r[0],s=t.code,i=t.message,a=t.path.join(".");if(!n[a])if("unionErrors"in t){var u=t.unionErrors[0].errors[0];n[a]={message:u.message,type:u.code};}else n[a]={message:i,type:s};if("unionErrors"in t&&t.unionErrors.forEach(function(e){return e.errors.forEach(function(e){return r.push(e)})}),e){var c=n[a].types,f=c&&c[t.code];n[a]=appendErrors(a,e,n,s,f?[].concat(f,t.message):t.message);}r.shift();}return n}function i(r,e){for(var n={};r.length;){var t=r[0],s=t.code,i=t.message,a=t.path.join(".");if(!n[a])if("invalid_union"===t.code&&t.errors.length>0){var u=t.errors[0][0];n[a]={message:u.message,type:u.code};}else n[a]={message:i,type:s};if("invalid_union"===t.code&&t.errors.forEach(function(e){return e.forEach(function(e){return r.push(e)})}),e){var c=n[a].types,f=c&&c[t.code];n[a]=appendErrors(a,e,n,s,f?[].concat(f,t.message):t.message);}r.shift();}return n}function a(o$1,a,u){if(void 0===u&&(u={}),function(r){return "_def"in r&&"object"==typeof r._def&&"typeName"in r._def}(o$1))return function(n,i,c){try{return Promise.resolve(t(function(){return Promise.resolve(o$1["sync"===u.mode?"parse":"parseAsync"](n,a)).then(function(e){return c.shouldUseNativeValidation&&o({},c),{errors:{},values:u.raw?Object.assign({},n):e}})},function(r){if(function(r){return Array.isArray(null==r?void 0:r.issues)}(r))return {values:{},errors:s$1(s(r.errors,!c.shouldUseNativeValidation&&"all"===c.criteriaMode),c)};throw r}))}catch(r){return Promise.reject(r)}};if(function(r){return "_zod"in r&&"object"==typeof r._zod}(o$1))return function(s,c,f){try{return Promise.resolve(t(function(){return Promise.resolve(("sync"===u.mode?n$1.parse:n$1.parseAsync)(o$1,s,a)).then(function(e){return f.shouldUseNativeValidation&&o({},f),{errors:{},values:u.raw?Object.assign({},s):e}})},function(r){if(function(r){return r instanceof n$1.$ZodError}(r))return {values:{},errors:s$1(i(r.issues,!f.shouldUseNativeValidation&&"all"===f.criteriaMode),f)};throw r}))}catch(r){return Promise.reject(r)}};throw new Error("Invalid input: not a Zod schema")}
|
|
2774
2774
|
|
|
2775
|
-
function
|
|
2776
|
-
return
|
|
2775
|
+
function isArrayField(field) {
|
|
2776
|
+
return field.type === 'array' && 'fields' in field;
|
|
2777
|
+
}
|
|
2778
|
+
function createField(field) {
|
|
2779
|
+
return field;
|
|
2780
|
+
}
|
|
2781
|
+
function createArrayField(field) {
|
|
2782
|
+
return {
|
|
2783
|
+
...field,
|
|
2784
|
+
type: 'array'
|
|
2785
|
+
};
|
|
2786
|
+
}
|
|
2787
|
+
function strictFields(fields) {
|
|
2788
|
+
return fields;
|
|
2789
|
+
}
|
|
2790
|
+
function validateFieldType(type, registeredTypes) {
|
|
2791
|
+
if (!registeredTypes.includes(type) && type !== 'array') {
|
|
2792
|
+
throw new Error(`Unregistered field type: "${type}". ` + `Registered types: ${registeredTypes.join(', ')}. ` + `Use registerField("${type}", YourComponent) to register it.`);
|
|
2793
|
+
}
|
|
2794
|
+
return type;
|
|
2795
|
+
}
|
|
2796
|
+
function validateFieldTypes(fields, registeredTypes, options = {}) {
|
|
2797
|
+
const {
|
|
2798
|
+
throwOnError = false,
|
|
2799
|
+
warn = true
|
|
2800
|
+
} = options;
|
|
2801
|
+
const invalidTypes = [];
|
|
2802
|
+
const checkField = field => {
|
|
2803
|
+
if (field.type === 'array' && 'fields' in field) {
|
|
2804
|
+
field.fields.forEach(checkField);
|
|
2805
|
+
} else if (!registeredTypes.includes(field.type) && field.type !== 'array') {
|
|
2806
|
+
invalidTypes.push(field.type);
|
|
2807
|
+
}
|
|
2808
|
+
};
|
|
2809
|
+
fields.forEach(checkField);
|
|
2810
|
+
if (invalidTypes.length > 0) {
|
|
2811
|
+
const uniqueInvalid = [...new Set(invalidTypes)];
|
|
2812
|
+
const message = `Unregistered field type(s): ${uniqueInvalid.map(t => `"${t}"`).join(', ')}. ` + `Registered types: ${registeredTypes.join(', ')}. ` + 'Register custom types with registerField() or use module augmentation for TypeScript support.';
|
|
2813
|
+
if (throwOnError) {
|
|
2814
|
+
throw new Error(message);
|
|
2815
|
+
} else if (warn && typeof console !== 'undefined') {
|
|
2816
|
+
console.warn(`[FormGenerator] ${message}`);
|
|
2817
|
+
}
|
|
2818
|
+
}
|
|
2819
|
+
return fields;
|
|
2777
2820
|
}
|
|
2778
2821
|
|
|
2779
2822
|
const fieldRegistry = new Map();
|
|
2780
2823
|
const componentRegistry = {};
|
|
2781
|
-
const layoutRegistry = {};
|
|
2782
2824
|
const defaultFormComponents = {
|
|
2783
|
-
FormItem: ({
|
|
2784
|
-
className,
|
|
2785
|
-
children
|
|
2786
|
-
}) => /*#__PURE__*/createElement('div', {
|
|
2787
|
-
className: className !== null && className !== void 0 ? className : 'form-item'
|
|
2788
|
-
}, children),
|
|
2789
|
-
FormInputLabelWrapper: ({
|
|
2790
|
-
className,
|
|
2791
|
-
orientation,
|
|
2792
|
-
children
|
|
2793
|
-
}) => /*#__PURE__*/createElement('div', {
|
|
2794
|
-
'className': className !== null && className !== void 0 ? className : 'form-input-label-wrapper',
|
|
2795
|
-
'data-orientation': orientation !== null && orientation !== void 0 ? orientation : 'vertical'
|
|
2796
|
-
}, children),
|
|
2797
|
-
FormLabel: ({
|
|
2798
|
-
className,
|
|
2799
|
-
children,
|
|
2800
|
-
required,
|
|
2801
|
-
htmlFor
|
|
2802
|
-
}) => /*#__PURE__*/createElement('label', {
|
|
2803
|
-
className: className !== null && className !== void 0 ? className : 'form-label',
|
|
2804
|
-
htmlFor
|
|
2805
|
-
}, children, required && /*#__PURE__*/createElement('span', {
|
|
2806
|
-
className: 'required-indicator'
|
|
2807
|
-
}, ' *')),
|
|
2808
|
-
FormDescription: ({
|
|
2809
|
-
className,
|
|
2810
|
-
children
|
|
2811
|
-
}) => /*#__PURE__*/createElement('p', {
|
|
2812
|
-
className: className !== null && className !== void 0 ? className : 'form-description'
|
|
2813
|
-
}, children),
|
|
2814
|
-
FormMessage: ({
|
|
2815
|
-
className,
|
|
2816
|
-
name
|
|
2817
|
-
}) => /*#__PURE__*/createElement('p', {
|
|
2818
|
-
'className': className !== null && className !== void 0 ? className : 'form-message',
|
|
2819
|
-
'data-field': name
|
|
2820
|
-
}),
|
|
2821
2825
|
SubmitButton: ({
|
|
2822
2826
|
className,
|
|
2823
2827
|
children,
|
|
@@ -2827,7 +2831,13 @@ const defaultFormComponents = {
|
|
|
2827
2831
|
type: 'submit',
|
|
2828
2832
|
className: className !== null && className !== void 0 ? className : 'form-submit',
|
|
2829
2833
|
disabled: disabled || isSubmitting
|
|
2830
|
-
}, isSubmitting ? 'Submitting...' : children)
|
|
2834
|
+
}, isSubmitting ? 'Submitting...' : children),
|
|
2835
|
+
FieldWrapper: ({
|
|
2836
|
+
children
|
|
2837
|
+
}) => /*#__PURE__*/createElement(Fragment, null, children),
|
|
2838
|
+
FieldsWrapper: ({
|
|
2839
|
+
children
|
|
2840
|
+
}) => /*#__PURE__*/createElement(Fragment, null, children)
|
|
2831
2841
|
};
|
|
2832
2842
|
const htmlInputTypes = {
|
|
2833
2843
|
text: 'text',
|
|
@@ -2970,27 +2980,15 @@ function createDefaultFieldComponent(type) {
|
|
|
2970
2980
|
};
|
|
2971
2981
|
return FallbackComponent;
|
|
2972
2982
|
}
|
|
2973
|
-
function registerField(type, component, options = {}
|
|
2974
|
-
const {
|
|
2975
|
-
override = true
|
|
2976
|
-
} = config;
|
|
2977
|
-
const exists = fieldRegistry.has(type);
|
|
2978
|
-
if (override === 'only' && !exists) return;
|
|
2979
|
-
if (override === false && exists) return;
|
|
2983
|
+
function registerField(type, component, options = {}) {
|
|
2980
2984
|
fieldRegistry.set(type, {
|
|
2981
2985
|
component,
|
|
2982
2986
|
options
|
|
2983
2987
|
});
|
|
2984
2988
|
}
|
|
2985
|
-
function registerFields(fields
|
|
2986
|
-
const {
|
|
2987
|
-
override = true
|
|
2988
|
-
} = config;
|
|
2989
|
+
function registerFields(fields) {
|
|
2989
2990
|
Object.entries(fields).forEach(([type, definition]) => {
|
|
2990
2991
|
var _a;
|
|
2991
|
-
const exists = fieldRegistry.has(type);
|
|
2992
|
-
if (override === 'only' && !exists) return;
|
|
2993
|
-
if (override === false && exists) return;
|
|
2994
2992
|
if (typeof definition === 'function') {
|
|
2995
2993
|
fieldRegistry.set(type, {
|
|
2996
2994
|
component: definition,
|
|
@@ -3012,10 +3010,6 @@ function getFieldComponent(type) {
|
|
|
3012
3010
|
}
|
|
3013
3011
|
return createDefaultFieldComponent(type);
|
|
3014
3012
|
}
|
|
3015
|
-
function getFieldOptions(type) {
|
|
3016
|
-
var _a;
|
|
3017
|
-
return (_a = fieldRegistry.get(type)) === null || _a === void 0 ? void 0 : _a.options;
|
|
3018
|
-
}
|
|
3019
3013
|
function getFieldValidator(type) {
|
|
3020
3014
|
var _a;
|
|
3021
3015
|
return (_a = fieldRegistry.get(type)) === null || _a === void 0 ? void 0 : _a.options.validator;
|
|
@@ -3029,24 +3023,12 @@ function unregisterField(type) {
|
|
|
3029
3023
|
function getRegisteredFieldTypes() {
|
|
3030
3024
|
return Array.from(fieldRegistry.keys());
|
|
3031
3025
|
}
|
|
3032
|
-
function registerFormComponents(components
|
|
3033
|
-
const {
|
|
3034
|
-
override = true
|
|
3035
|
-
} = config;
|
|
3026
|
+
function registerFormComponents(components) {
|
|
3036
3027
|
Object.keys(components).forEach(key => {
|
|
3037
|
-
const exists = key in componentRegistry;
|
|
3038
|
-
if (override === 'only' && !exists) return;
|
|
3039
|
-
if (override === false && exists) return;
|
|
3040
3028
|
componentRegistry[key] = components[key];
|
|
3041
3029
|
});
|
|
3042
3030
|
}
|
|
3043
|
-
function registerFormComponent(name, component
|
|
3044
|
-
const {
|
|
3045
|
-
override = true
|
|
3046
|
-
} = config;
|
|
3047
|
-
const exists = name in componentRegistry;
|
|
3048
|
-
if (override === 'only' && !exists) return;
|
|
3049
|
-
if (override === false && exists) return;
|
|
3031
|
+
function registerFormComponent(name, component) {
|
|
3050
3032
|
componentRegistry[name] = component;
|
|
3051
3033
|
}
|
|
3052
3034
|
function getFormComponent(name) {
|
|
@@ -3061,37 +3043,6 @@ function getFormComponents() {
|
|
|
3061
3043
|
...componentRegistry
|
|
3062
3044
|
};
|
|
3063
3045
|
}
|
|
3064
|
-
function registerLayoutComponents(components, config = {}) {
|
|
3065
|
-
const {
|
|
3066
|
-
override = true
|
|
3067
|
-
} = config;
|
|
3068
|
-
Object.keys(components).forEach(key => {
|
|
3069
|
-
const exists = key in layoutRegistry;
|
|
3070
|
-
if (override === 'only' && !exists) return;
|
|
3071
|
-
if (override === false && exists) return;
|
|
3072
|
-
layoutRegistry[key] = components[key];
|
|
3073
|
-
});
|
|
3074
|
-
}
|
|
3075
|
-
function registerLayoutComponent(name, component, config = {}) {
|
|
3076
|
-
const {
|
|
3077
|
-
override = true
|
|
3078
|
-
} = config;
|
|
3079
|
-
const exists = name in layoutRegistry;
|
|
3080
|
-
if (override === 'only' && !exists) return;
|
|
3081
|
-
if (override === false && exists) return;
|
|
3082
|
-
layoutRegistry[name] = component;
|
|
3083
|
-
}
|
|
3084
|
-
function getLayoutComponent(name) {
|
|
3085
|
-
return layoutRegistry[name];
|
|
3086
|
-
}
|
|
3087
|
-
function hasLayoutComponent(name) {
|
|
3088
|
-
return name in layoutRegistry;
|
|
3089
|
-
}
|
|
3090
|
-
function clearLayoutRegistry() {
|
|
3091
|
-
Object.keys(layoutRegistry).forEach(key => {
|
|
3092
|
-
delete layoutRegistry[key];
|
|
3093
|
-
});
|
|
3094
|
-
}
|
|
3095
3046
|
function clearFieldRegistry() {
|
|
3096
3047
|
fieldRegistry.clear();
|
|
3097
3048
|
}
|
|
@@ -3107,7 +3058,6 @@ function resetFormComponentRegistry() {
|
|
|
3107
3058
|
function clearAllRegistries() {
|
|
3108
3059
|
clearFieldRegistry();
|
|
3109
3060
|
clearFormComponentRegistry();
|
|
3110
|
-
clearLayoutRegistry();
|
|
3111
3061
|
}
|
|
3112
3062
|
|
|
3113
3063
|
const FieldRenderer = /*#__PURE__*/memo(t0 => {
|
|
@@ -3120,6 +3070,10 @@ const FieldRenderer = /*#__PURE__*/memo(t0 => {
|
|
|
3120
3070
|
if (field.hidden) {
|
|
3121
3071
|
return null;
|
|
3122
3072
|
}
|
|
3073
|
+
if (!field.name) {
|
|
3074
|
+
console.error(`Field is missing required "name" property. Field type: "${field.type}". ` + "Every field must have a unique name for form state management.");
|
|
3075
|
+
return null;
|
|
3076
|
+
}
|
|
3123
3077
|
let t1;
|
|
3124
3078
|
let t2;
|
|
3125
3079
|
if ($[0] !== field || $[1] !== form || $[2] !== namePrefix) {
|
|
@@ -3131,18 +3085,6 @@ const FieldRenderer = /*#__PURE__*/memo(t0 => {
|
|
|
3131
3085
|
t2 = null;
|
|
3132
3086
|
break bb0;
|
|
3133
3087
|
}
|
|
3134
|
-
const FormItemComponent = getFormComponent("FormItem");
|
|
3135
|
-
const FormLabelComponent = getFormComponent("FormLabel");
|
|
3136
|
-
const FormInputLabelWrapper = getFormComponent("FormInputLabelWrapper");
|
|
3137
|
-
const FormDescriptionComponent = getFormComponent("FormDescription");
|
|
3138
|
-
const FormMessageComponent = getFormComponent("FormMessage");
|
|
3139
|
-
if (!FormItemComponent) {
|
|
3140
|
-
console.warn("FormItem component not registered. Use registerFormComponent(\"FormItem\", Component) to register one.");
|
|
3141
|
-
t2 = null;
|
|
3142
|
-
break bb0;
|
|
3143
|
-
}
|
|
3144
|
-
const fieldOptions = getFieldOptions(field.type);
|
|
3145
|
-
const itemClassName = [`form-field-${field.type}`, fieldOptions === null || fieldOptions === void 0 ? void 0 : fieldOptions.className, field.className].filter(Boolean).join(" ");
|
|
3146
3088
|
const fieldName = namePrefix ? `${namePrefix}.${field.name}` : field.name;
|
|
3147
3089
|
t1 = jsx(Controller, {
|
|
3148
3090
|
control: form.control,
|
|
@@ -3152,36 +3094,18 @@ const FieldRenderer = /*#__PURE__*/memo(t0 => {
|
|
|
3152
3094
|
field: formField,
|
|
3153
3095
|
fieldState
|
|
3154
3096
|
} = t3;
|
|
3155
|
-
return
|
|
3156
|
-
|
|
3157
|
-
|
|
3158
|
-
orientation: fieldOptions === null || fieldOptions === void 0 ? void 0 : fieldOptions.inputLabelWrapperOrientation,
|
|
3159
|
-
className: fieldOptions === null || fieldOptions === void 0 ? void 0 : fieldOptions.inputLabelWrapper,
|
|
3160
|
-
children: [field.label && FormLabelComponent && jsx(FormLabelComponent, {
|
|
3161
|
-
className: fieldOptions === null || fieldOptions === void 0 ? void 0 : fieldOptions.labelClassName,
|
|
3162
|
-
required: field.required,
|
|
3163
|
-
htmlFor: fieldName,
|
|
3164
|
-
children: field.label
|
|
3165
|
-
}), jsx(FieldComponent, {
|
|
3166
|
-
field: {
|
|
3167
|
-
...field,
|
|
3168
|
-
name: fieldName
|
|
3169
|
-
},
|
|
3170
|
-
formField,
|
|
3171
|
-
fieldState: {
|
|
3172
|
-
invalid: fieldState.invalid,
|
|
3173
|
-
error: fieldState.error,
|
|
3174
|
-
isDirty: fieldState.isDirty,
|
|
3175
|
-
isTouched: fieldState.isTouched
|
|
3176
|
-
}
|
|
3177
|
-
})]
|
|
3178
|
-
}), field.description && FormDescriptionComponent && jsx(FormDescriptionComponent, {
|
|
3179
|
-
className: fieldOptions === null || fieldOptions === void 0 ? void 0 : fieldOptions.descriptionClassName,
|
|
3180
|
-
children: field.description
|
|
3181
|
-
}), FormMessageComponent && jsx(FormMessageComponent, {
|
|
3182
|
-
className: fieldOptions === null || fieldOptions === void 0 ? void 0 : fieldOptions.messageClassName,
|
|
3097
|
+
return jsx(FieldComponent, {
|
|
3098
|
+
field: {
|
|
3099
|
+
...field,
|
|
3183
3100
|
name: fieldName
|
|
3184
|
-
}
|
|
3101
|
+
},
|
|
3102
|
+
formField,
|
|
3103
|
+
fieldState: {
|
|
3104
|
+
invalid: fieldState.invalid,
|
|
3105
|
+
error: fieldState.error,
|
|
3106
|
+
isDirty: fieldState.isDirty,
|
|
3107
|
+
isTouched: fieldState.isTouched
|
|
3108
|
+
}
|
|
3185
3109
|
});
|
|
3186
3110
|
}
|
|
3187
3111
|
});
|
|
@@ -3201,135 +3125,655 @@ const FieldRenderer = /*#__PURE__*/memo(t0 => {
|
|
|
3201
3125
|
return t1;
|
|
3202
3126
|
});
|
|
3203
3127
|
FieldRenderer.displayName = 'FieldRenderer';
|
|
3204
|
-
|
|
3205
|
-
|
|
3128
|
+
|
|
3129
|
+
function useArrayField(field) {
|
|
3206
3130
|
const {
|
|
3207
|
-
|
|
3208
|
-
|
|
3131
|
+
control
|
|
3132
|
+
} = useFormContext();
|
|
3133
|
+
const fieldArray = useFieldArray({
|
|
3134
|
+
control,
|
|
3135
|
+
name: field.name
|
|
3136
|
+
});
|
|
3137
|
+
const {
|
|
3138
|
+
fields: items,
|
|
3139
|
+
append,
|
|
3140
|
+
prepend,
|
|
3141
|
+
remove,
|
|
3142
|
+
move,
|
|
3143
|
+
swap,
|
|
3144
|
+
insert
|
|
3145
|
+
} = fieldArray;
|
|
3146
|
+
const buildEmptyItem = useCallback(() => {
|
|
3147
|
+
const item = {};
|
|
3148
|
+
field.fields.forEach(f => {
|
|
3149
|
+
var _a;
|
|
3150
|
+
item[f.name] = (_a = f.defaultValue) !== null && _a !== void 0 ? _a : '';
|
|
3151
|
+
});
|
|
3152
|
+
return item;
|
|
3153
|
+
}, [field.fields]);
|
|
3154
|
+
const handleAppend = useCallback(() => {
|
|
3155
|
+
append(buildEmptyItem());
|
|
3156
|
+
}, [append, buildEmptyItem]);
|
|
3157
|
+
const handleAppendWith = useCallback(values => {
|
|
3158
|
+
append(values);
|
|
3159
|
+
}, [append]);
|
|
3160
|
+
const handlePrepend = useCallback(() => {
|
|
3161
|
+
prepend(buildEmptyItem());
|
|
3162
|
+
}, [prepend, buildEmptyItem]);
|
|
3163
|
+
const handleInsert = useCallback((index, values_0) => {
|
|
3164
|
+
insert(index, values_0 !== null && values_0 !== void 0 ? values_0 : buildEmptyItem());
|
|
3165
|
+
}, [insert, buildEmptyItem]);
|
|
3166
|
+
const canAppend = field.maxItems === undefined || items.length < field.maxItems;
|
|
3167
|
+
const canRemove = field.minItems === undefined || items.length > field.minItems;
|
|
3168
|
+
const renderField = useCallback((index_0, fieldName) => {
|
|
3169
|
+
const childField = field.fields.find(f_0 => f_0.name === fieldName);
|
|
3170
|
+
if (!childField) {
|
|
3171
|
+
console.warn(`Field "${fieldName}" not found in array field "${field.name}"`);
|
|
3172
|
+
return jsx(Fragment$1, {});
|
|
3173
|
+
}
|
|
3174
|
+
return jsx(FieldRenderer, {
|
|
3175
|
+
field: childField,
|
|
3176
|
+
namePrefix: `${field.name}.${index_0}`
|
|
3177
|
+
}, `${field.name}.${index_0}.${fieldName}`);
|
|
3178
|
+
}, [field]);
|
|
3179
|
+
const renderItem = useCallback(index_1 => {
|
|
3180
|
+
const result = {};
|
|
3181
|
+
field.fields.forEach(childField_0 => {
|
|
3182
|
+
result[childField_0.name] = jsx(FieldRenderer, {
|
|
3183
|
+
field: childField_0,
|
|
3184
|
+
namePrefix: `${field.name}.${index_1}`
|
|
3185
|
+
}, `${field.name}.${index_1}.${childField_0.name}`);
|
|
3186
|
+
});
|
|
3187
|
+
return result;
|
|
3188
|
+
}, [field]);
|
|
3189
|
+
return {
|
|
3190
|
+
fieldArray,
|
|
3191
|
+
append: handleAppend,
|
|
3192
|
+
appendWith: handleAppendWith,
|
|
3193
|
+
prepend: handlePrepend,
|
|
3194
|
+
remove,
|
|
3195
|
+
move,
|
|
3196
|
+
swap,
|
|
3197
|
+
insert: handleInsert,
|
|
3198
|
+
field,
|
|
3199
|
+
items: items.map((item_0, index_2) => ({
|
|
3200
|
+
id: item_0.id,
|
|
3201
|
+
index: index_2
|
|
3202
|
+
})),
|
|
3203
|
+
canAppend,
|
|
3204
|
+
canRemove,
|
|
3205
|
+
renderField,
|
|
3206
|
+
renderItem
|
|
3207
|
+
};
|
|
3208
|
+
}
|
|
3209
|
+
const ArrayFieldRenderer = /*#__PURE__*/memo(t0 => {
|
|
3210
|
+
const $ = c(28);
|
|
3211
|
+
const {
|
|
3212
|
+
field,
|
|
3213
|
+
children
|
|
3209
3214
|
} = t0;
|
|
3210
|
-
|
|
3211
|
-
|
|
3212
|
-
|
|
3213
|
-
|
|
3214
|
-
|
|
3215
|
-
|
|
3216
|
-
|
|
3217
|
-
|
|
3218
|
-
|
|
3219
|
-
|
|
3220
|
-
|
|
3221
|
-
|
|
3222
|
-
|
|
3223
|
-
|
|
3224
|
-
|
|
3225
|
-
|
|
3226
|
-
|
|
3227
|
-
|
|
3228
|
-
|
|
3229
|
-
|
|
3215
|
+
const {
|
|
3216
|
+
items,
|
|
3217
|
+
append,
|
|
3218
|
+
remove,
|
|
3219
|
+
move,
|
|
3220
|
+
canAppend,
|
|
3221
|
+
renderItem,
|
|
3222
|
+
field: arrayField
|
|
3223
|
+
} = useArrayField(field);
|
|
3224
|
+
if (field.hidden) {
|
|
3225
|
+
return null;
|
|
3226
|
+
}
|
|
3227
|
+
if (!children) {
|
|
3228
|
+
let t1;
|
|
3229
|
+
if ($[0] !== append || $[1] !== canAppend || $[2] !== field.className || $[3] !== field.description || $[4] !== field.label || $[5] !== items || $[6] !== renderItem) {
|
|
3230
|
+
let t2;
|
|
3231
|
+
if ($[8] !== renderItem) {
|
|
3232
|
+
t2 = t3 => {
|
|
3233
|
+
const {
|
|
3234
|
+
id,
|
|
3235
|
+
index
|
|
3236
|
+
} = t3;
|
|
3237
|
+
return jsx("div", {
|
|
3238
|
+
"data-array-item-index": index,
|
|
3239
|
+
children: Object.values(renderItem(index))
|
|
3240
|
+
}, id);
|
|
3241
|
+
};
|
|
3242
|
+
$[8] = renderItem;
|
|
3243
|
+
$[9] = t2;
|
|
3230
3244
|
} else {
|
|
3231
|
-
|
|
3245
|
+
t2 = $[9];
|
|
3232
3246
|
}
|
|
3233
|
-
|
|
3234
|
-
|
|
3235
|
-
|
|
3236
|
-
|
|
3247
|
+
t1 = jsxs("div", {
|
|
3248
|
+
className: field.className,
|
|
3249
|
+
children: [field.label && jsx("label", {
|
|
3250
|
+
children: field.label
|
|
3251
|
+
}), field.description && jsx("p", {
|
|
3252
|
+
children: field.description
|
|
3253
|
+
}), items.map(t2), canAppend && jsx("button", {
|
|
3254
|
+
type: "button",
|
|
3255
|
+
onClick: append,
|
|
3256
|
+
children: "Add Item"
|
|
3257
|
+
})]
|
|
3237
3258
|
});
|
|
3259
|
+
$[0] = append;
|
|
3260
|
+
$[1] = canAppend;
|
|
3261
|
+
$[2] = field.className;
|
|
3262
|
+
$[3] = field.description;
|
|
3263
|
+
$[4] = field.label;
|
|
3264
|
+
$[5] = items;
|
|
3265
|
+
$[6] = renderItem;
|
|
3266
|
+
$[7] = t1;
|
|
3267
|
+
} else {
|
|
3268
|
+
t1 = $[7];
|
|
3238
3269
|
}
|
|
3239
|
-
|
|
3240
|
-
$[1] = namePrefix;
|
|
3241
|
-
$[2] = t1;
|
|
3242
|
-
$[3] = t2;
|
|
3243
|
-
} else {
|
|
3244
|
-
t1 = $[2];
|
|
3245
|
-
t2 = $[3];
|
|
3270
|
+
return t1;
|
|
3246
3271
|
}
|
|
3247
|
-
|
|
3248
|
-
|
|
3272
|
+
let t1;
|
|
3273
|
+
if ($[10] !== arrayField || $[11] !== children || $[12] !== field.className || $[13] !== field.description || $[14] !== field.label || $[15] !== field.name || $[16] !== items || $[17] !== move || $[18] !== remove || $[19] !== renderItem) {
|
|
3274
|
+
let t2;
|
|
3275
|
+
if ($[21] !== arrayField || $[22] !== children || $[23] !== field.name || $[24] !== move || $[25] !== remove || $[26] !== renderItem) {
|
|
3276
|
+
t2 = t3 => {
|
|
3277
|
+
const {
|
|
3278
|
+
id: id_0,
|
|
3279
|
+
index: index_0
|
|
3280
|
+
} = t3;
|
|
3281
|
+
return children({
|
|
3282
|
+
index: index_0,
|
|
3283
|
+
id: id_0,
|
|
3284
|
+
remove: () => remove(index_0),
|
|
3285
|
+
move: toIndex => move(index_0, toIndex),
|
|
3286
|
+
fields: renderItem(index_0),
|
|
3287
|
+
fieldNames: arrayField.fields.map(_temp),
|
|
3288
|
+
namePrefix: `${field.name}.${index_0}`
|
|
3289
|
+
});
|
|
3290
|
+
};
|
|
3291
|
+
$[21] = arrayField;
|
|
3292
|
+
$[22] = children;
|
|
3293
|
+
$[23] = field.name;
|
|
3294
|
+
$[24] = move;
|
|
3295
|
+
$[25] = remove;
|
|
3296
|
+
$[26] = renderItem;
|
|
3297
|
+
$[27] = t2;
|
|
3298
|
+
} else {
|
|
3299
|
+
t2 = $[27];
|
|
3300
|
+
}
|
|
3301
|
+
t1 = jsxs("div", {
|
|
3302
|
+
className: field.className,
|
|
3303
|
+
children: [field.label && jsx("label", {
|
|
3304
|
+
children: field.label
|
|
3305
|
+
}), field.description && jsx("p", {
|
|
3306
|
+
children: field.description
|
|
3307
|
+
}), items.map(t2)]
|
|
3308
|
+
});
|
|
3309
|
+
$[10] = arrayField;
|
|
3310
|
+
$[11] = children;
|
|
3311
|
+
$[12] = field.className;
|
|
3312
|
+
$[13] = field.description;
|
|
3313
|
+
$[14] = field.label;
|
|
3314
|
+
$[15] = field.name;
|
|
3315
|
+
$[16] = items;
|
|
3316
|
+
$[17] = move;
|
|
3317
|
+
$[18] = remove;
|
|
3318
|
+
$[19] = renderItem;
|
|
3319
|
+
$[20] = t1;
|
|
3320
|
+
} else {
|
|
3321
|
+
t1 = $[20];
|
|
3249
3322
|
}
|
|
3250
3323
|
return t1;
|
|
3251
3324
|
});
|
|
3252
|
-
|
|
3253
|
-
|
|
3254
|
-
|
|
3325
|
+
ArrayFieldRenderer.displayName = 'ArrayFieldRenderer';
|
|
3326
|
+
function _temp(f) {
|
|
3327
|
+
return f.name;
|
|
3328
|
+
}
|
|
3329
|
+
|
|
3330
|
+
const RESERVED_PROPS = new Set(['all', 'remaining', 'names', 'has', 'render']);
|
|
3331
|
+
function createTemplateFields(fieldEntries) {
|
|
3332
|
+
const accessedFields = new Set();
|
|
3333
|
+
const handler = {
|
|
3334
|
+
get(_, prop) {
|
|
3335
|
+
if (typeof prop === 'symbol') {
|
|
3336
|
+
return undefined;
|
|
3337
|
+
}
|
|
3338
|
+
if (prop === 'all') {
|
|
3339
|
+
return Array.from(fieldEntries.values()).map(entry => entry.element);
|
|
3340
|
+
}
|
|
3341
|
+
if (prop === 'remaining') {
|
|
3342
|
+
return Array.from(fieldEntries.entries()).filter(([name]) => !accessedFields.has(name)).map(([, entry]) => entry.element);
|
|
3343
|
+
}
|
|
3344
|
+
if (prop === 'names') {
|
|
3345
|
+
return Array.from(fieldEntries.keys());
|
|
3346
|
+
}
|
|
3347
|
+
if (prop === 'has') {
|
|
3348
|
+
return name => fieldEntries.has(name);
|
|
3349
|
+
}
|
|
3350
|
+
if (prop === 'render') {
|
|
3351
|
+
return (...names) => {
|
|
3352
|
+
return names.filter(name => fieldEntries.has(name)).map(name => {
|
|
3353
|
+
accessedFields.add(name);
|
|
3354
|
+
return fieldEntries.get(name).element;
|
|
3355
|
+
});
|
|
3356
|
+
};
|
|
3357
|
+
}
|
|
3358
|
+
if (fieldEntries.has(prop)) {
|
|
3359
|
+
accessedFields.add(prop);
|
|
3360
|
+
return fieldEntries.get(prop).element;
|
|
3361
|
+
}
|
|
3362
|
+
return undefined;
|
|
3363
|
+
},
|
|
3364
|
+
has(_, prop) {
|
|
3365
|
+
if (typeof prop === 'symbol') {
|
|
3366
|
+
return false;
|
|
3367
|
+
}
|
|
3368
|
+
return RESERVED_PROPS.has(prop) || fieldEntries.has(prop);
|
|
3369
|
+
},
|
|
3370
|
+
ownKeys() {
|
|
3371
|
+
return [...RESERVED_PROPS, ...fieldEntries.keys()];
|
|
3372
|
+
},
|
|
3373
|
+
getOwnPropertyDescriptor(_, prop) {
|
|
3374
|
+
if (typeof prop === 'symbol') {
|
|
3375
|
+
return undefined;
|
|
3376
|
+
}
|
|
3377
|
+
if (RESERVED_PROPS.has(prop) || fieldEntries.has(prop)) {
|
|
3378
|
+
return {
|
|
3379
|
+
configurable: true,
|
|
3380
|
+
enumerable: true,
|
|
3381
|
+
value: this.get(_, prop, {})
|
|
3382
|
+
};
|
|
3383
|
+
}
|
|
3384
|
+
return undefined;
|
|
3385
|
+
}
|
|
3386
|
+
};
|
|
3387
|
+
return new Proxy({}, handler);
|
|
3388
|
+
}
|
|
3389
|
+
|
|
3390
|
+
const FieldsContext = /*#__PURE__*/createContext(null);
|
|
3391
|
+
function FieldsProvider(t0) {
|
|
3392
|
+
const $ = c(9);
|
|
3255
3393
|
const {
|
|
3256
|
-
|
|
3394
|
+
children,
|
|
3395
|
+
fields,
|
|
3257
3396
|
namePrefix
|
|
3258
3397
|
} = t0;
|
|
3398
|
+
let map;
|
|
3399
|
+
if ($[0] !== fields || $[1] !== namePrefix) {
|
|
3400
|
+
map = new Map();
|
|
3401
|
+
const addFields = (items, prefix) => {
|
|
3402
|
+
items.forEach(item => {
|
|
3403
|
+
if ("fields" in item && Array.isArray(item.fields)) {
|
|
3404
|
+
const arrayPrefix = prefix ? `${prefix}.${item.name}` : item.name;
|
|
3405
|
+
addFields(item.fields, arrayPrefix);
|
|
3406
|
+
} else {
|
|
3407
|
+
const fieldName = prefix ? `${prefix}.${item.name}` : item.name;
|
|
3408
|
+
map.set(fieldName, item);
|
|
3409
|
+
if (prefix) {
|
|
3410
|
+
map.set(item.name, item);
|
|
3411
|
+
}
|
|
3412
|
+
}
|
|
3413
|
+
});
|
|
3414
|
+
};
|
|
3415
|
+
addFields(fields, namePrefix);
|
|
3416
|
+
$[0] = fields;
|
|
3417
|
+
$[1] = namePrefix;
|
|
3418
|
+
$[2] = map;
|
|
3419
|
+
} else {
|
|
3420
|
+
map = $[2];
|
|
3421
|
+
}
|
|
3422
|
+
const fieldsMap = map;
|
|
3259
3423
|
let t1;
|
|
3424
|
+
if ($[3] !== fieldsMap || $[4] !== namePrefix) {
|
|
3425
|
+
t1 = {
|
|
3426
|
+
fields: fieldsMap,
|
|
3427
|
+
namePrefix
|
|
3428
|
+
};
|
|
3429
|
+
$[3] = fieldsMap;
|
|
3430
|
+
$[4] = namePrefix;
|
|
3431
|
+
$[5] = t1;
|
|
3432
|
+
} else {
|
|
3433
|
+
t1 = $[5];
|
|
3434
|
+
}
|
|
3435
|
+
const value = t1;
|
|
3260
3436
|
let t2;
|
|
3261
|
-
if ($[
|
|
3262
|
-
t2 =
|
|
3263
|
-
|
|
3264
|
-
|
|
3265
|
-
|
|
3266
|
-
|
|
3267
|
-
|
|
3268
|
-
|
|
3269
|
-
|
|
3270
|
-
|
|
3271
|
-
|
|
3272
|
-
|
|
3273
|
-
|
|
3274
|
-
|
|
3275
|
-
|
|
3276
|
-
|
|
3277
|
-
|
|
3278
|
-
|
|
3279
|
-
|
|
3280
|
-
|
|
3437
|
+
if ($[6] !== children || $[7] !== value) {
|
|
3438
|
+
t2 = jsx(FieldsContext.Provider, {
|
|
3439
|
+
value,
|
|
3440
|
+
children
|
|
3441
|
+
});
|
|
3442
|
+
$[6] = children;
|
|
3443
|
+
$[7] = value;
|
|
3444
|
+
$[8] = t2;
|
|
3445
|
+
} else {
|
|
3446
|
+
t2 = $[8];
|
|
3447
|
+
}
|
|
3448
|
+
return t2;
|
|
3449
|
+
}
|
|
3450
|
+
function useFieldProps(name) {
|
|
3451
|
+
const $ = c(23);
|
|
3452
|
+
const context = useContext(FieldsContext);
|
|
3453
|
+
const form = useFormContext();
|
|
3454
|
+
if (!context) {
|
|
3455
|
+
throw new Error("useFieldProps must be used within FormGenerator or FieldsProvider");
|
|
3456
|
+
}
|
|
3457
|
+
if (!form) {
|
|
3458
|
+
throw new Error("useFieldProps must be used within a FormProvider context");
|
|
3459
|
+
}
|
|
3460
|
+
const {
|
|
3461
|
+
fields,
|
|
3462
|
+
namePrefix
|
|
3463
|
+
} = context;
|
|
3464
|
+
const fullName = namePrefix ? `${namePrefix}.${name}` : name;
|
|
3465
|
+
let t0;
|
|
3466
|
+
if ($[0] !== fields || $[1] !== fullName || $[2] !== name) {
|
|
3467
|
+
t0 = fields.get(fullName) || fields.get(name);
|
|
3468
|
+
$[0] = fields;
|
|
3469
|
+
$[1] = fullName;
|
|
3470
|
+
$[2] = name;
|
|
3471
|
+
$[3] = t0;
|
|
3472
|
+
} else {
|
|
3473
|
+
t0 = $[3];
|
|
3474
|
+
}
|
|
3475
|
+
const field = t0;
|
|
3476
|
+
if (!field) {
|
|
3477
|
+
throw new Error(`Field "${name}" not found in form fields. ` + `Available fields: ${Array.from(fields.keys()).join(", ")}`);
|
|
3478
|
+
}
|
|
3479
|
+
let t1;
|
|
3480
|
+
if ($[4] !== form.control || $[5] !== fullName) {
|
|
3481
|
+
t1 = {
|
|
3482
|
+
control: form.control,
|
|
3483
|
+
name: fullName
|
|
3484
|
+
};
|
|
3485
|
+
$[4] = form.control;
|
|
3486
|
+
$[5] = fullName;
|
|
3487
|
+
$[6] = t1;
|
|
3488
|
+
} else {
|
|
3489
|
+
t1 = $[6];
|
|
3490
|
+
}
|
|
3491
|
+
const {
|
|
3492
|
+
field: controllerField,
|
|
3493
|
+
fieldState
|
|
3494
|
+
} = useController(t1);
|
|
3495
|
+
let t2;
|
|
3496
|
+
if ($[7] !== field || $[8] !== fullName) {
|
|
3497
|
+
t2 = {
|
|
3498
|
+
...field,
|
|
3499
|
+
name: fullName
|
|
3500
|
+
};
|
|
3501
|
+
$[7] = field;
|
|
3502
|
+
$[8] = fullName;
|
|
3503
|
+
$[9] = t2;
|
|
3504
|
+
} else {
|
|
3505
|
+
t2 = $[9];
|
|
3506
|
+
}
|
|
3507
|
+
let t3;
|
|
3508
|
+
if ($[10] !== fieldState.error || $[11] !== fieldState.invalid || $[12] !== fieldState.isDirty || $[13] !== fieldState.isTouched) {
|
|
3509
|
+
t3 = {
|
|
3510
|
+
invalid: fieldState.invalid,
|
|
3511
|
+
error: fieldState.error,
|
|
3512
|
+
isDirty: fieldState.isDirty,
|
|
3513
|
+
isTouched: fieldState.isTouched
|
|
3514
|
+
};
|
|
3515
|
+
$[10] = fieldState.error;
|
|
3516
|
+
$[11] = fieldState.invalid;
|
|
3517
|
+
$[12] = fieldState.isDirty;
|
|
3518
|
+
$[13] = fieldState.isTouched;
|
|
3519
|
+
$[14] = t3;
|
|
3520
|
+
} else {
|
|
3521
|
+
t3 = $[14];
|
|
3522
|
+
}
|
|
3523
|
+
let t4;
|
|
3524
|
+
if ($[15] !== controllerField.name || $[16] !== controllerField.onBlur || $[17] !== controllerField.onChange || $[18] !== controllerField.ref || $[19] !== controllerField.value || $[20] !== t2 || $[21] !== t3) {
|
|
3525
|
+
t4 = {
|
|
3526
|
+
name: controllerField.name,
|
|
3527
|
+
value: controllerField.value,
|
|
3528
|
+
onChange: controllerField.onChange,
|
|
3529
|
+
onBlur: controllerField.onBlur,
|
|
3530
|
+
ref: controllerField.ref,
|
|
3531
|
+
field: t2,
|
|
3532
|
+
fieldState: t3
|
|
3533
|
+
};
|
|
3534
|
+
$[15] = controllerField.name;
|
|
3535
|
+
$[16] = controllerField.onBlur;
|
|
3536
|
+
$[17] = controllerField.onChange;
|
|
3537
|
+
$[18] = controllerField.ref;
|
|
3538
|
+
$[19] = controllerField.value;
|
|
3539
|
+
$[20] = t2;
|
|
3540
|
+
$[21] = t3;
|
|
3541
|
+
$[22] = t4;
|
|
3542
|
+
} else {
|
|
3543
|
+
t4 = $[22];
|
|
3544
|
+
}
|
|
3545
|
+
return t4;
|
|
3546
|
+
}
|
|
3547
|
+
function useFieldsContext() {
|
|
3548
|
+
return useContext(FieldsContext);
|
|
3549
|
+
}
|
|
3550
|
+
|
|
3551
|
+
function isSchemaRequired(schema) {
|
|
3552
|
+
return !schema.isOptional() && !schema.isNullable();
|
|
3553
|
+
}
|
|
3554
|
+
function unwrapSchema(schema) {
|
|
3555
|
+
let current = schema;
|
|
3556
|
+
while (current instanceof z.ZodOptional || current instanceof z.ZodNullable) {
|
|
3557
|
+
current = current.unwrap();
|
|
3558
|
+
}
|
|
3559
|
+
if (current instanceof z.ZodDefault) {
|
|
3560
|
+
current = current._def.innerType;
|
|
3561
|
+
}
|
|
3562
|
+
return current;
|
|
3563
|
+
}
|
|
3564
|
+
function getSchemaTypeName(schema) {
|
|
3565
|
+
const unwrapped = unwrapSchema(schema);
|
|
3566
|
+
if (unwrapped instanceof z.ZodString) return 'string';
|
|
3567
|
+
if (unwrapped instanceof z.ZodNumber) return 'number';
|
|
3568
|
+
if (unwrapped instanceof z.ZodBoolean) return 'boolean';
|
|
3569
|
+
if (unwrapped instanceof z.ZodDate) return 'date';
|
|
3570
|
+
if (unwrapped instanceof z.ZodArray) return 'array';
|
|
3571
|
+
if (unwrapped instanceof z.ZodObject) return 'object';
|
|
3572
|
+
if (unwrapped instanceof z.ZodEnum) return 'enum';
|
|
3573
|
+
return 'unknown';
|
|
3574
|
+
}
|
|
3575
|
+
function getCheckDef(check) {
|
|
3576
|
+
var _a;
|
|
3577
|
+
if ((_a = check._zod) === null || _a === void 0 ? void 0 : _a.def) {
|
|
3578
|
+
const def = check._zod.def;
|
|
3579
|
+
return {
|
|
3580
|
+
type: def.check,
|
|
3581
|
+
value: def.value,
|
|
3582
|
+
minimum: def.minimum,
|
|
3583
|
+
maximum: def.maximum,
|
|
3584
|
+
length: def.length,
|
|
3585
|
+
regex: def.pattern || def.regex,
|
|
3586
|
+
format: def.format,
|
|
3587
|
+
inclusive: def.inclusive
|
|
3588
|
+
};
|
|
3589
|
+
}
|
|
3590
|
+
if (check.kind) {
|
|
3591
|
+
return {
|
|
3592
|
+
type: check.kind,
|
|
3593
|
+
value: check.value,
|
|
3594
|
+
regex: check.regex
|
|
3595
|
+
};
|
|
3596
|
+
}
|
|
3597
|
+
return null;
|
|
3598
|
+
}
|
|
3599
|
+
function getNumberConstraints(schema) {
|
|
3600
|
+
const unwrapped = unwrapSchema(schema);
|
|
3601
|
+
if (!(unwrapped instanceof z.ZodNumber)) {
|
|
3602
|
+
return {};
|
|
3603
|
+
}
|
|
3604
|
+
const constraints = {};
|
|
3605
|
+
const checks = unwrapped._def.checks;
|
|
3606
|
+
if (checks) {
|
|
3607
|
+
for (const check of checks) {
|
|
3608
|
+
const def = getCheckDef(check);
|
|
3609
|
+
if (!def) continue;
|
|
3610
|
+
if (def.type === 'greater_than' || def.type === 'min') {
|
|
3611
|
+
constraints.min = def.value;
|
|
3612
|
+
} else if (def.type === 'less_than' || def.type === 'max') {
|
|
3613
|
+
constraints.max = def.value;
|
|
3614
|
+
} else if (def.type === 'multiple_of' || def.type === 'multipleOf') {
|
|
3615
|
+
constraints.step = def.value;
|
|
3616
|
+
} else if (def.type === 'number_format' && (def.format === 'safeint' || def.format === 'int')) {
|
|
3617
|
+
constraints.step = 1;
|
|
3618
|
+
} else if (def.type === 'int' || def.type === 'integer') {
|
|
3619
|
+
constraints.step = 1;
|
|
3281
3620
|
}
|
|
3282
|
-
const renderItem = t3;
|
|
3283
|
-
t1 = jsx(SectionComponent, {
|
|
3284
|
-
layout,
|
|
3285
|
-
renderItem
|
|
3286
|
-
});
|
|
3287
3621
|
}
|
|
3288
|
-
$[0] = layout;
|
|
3289
|
-
$[1] = namePrefix;
|
|
3290
|
-
$[2] = t1;
|
|
3291
|
-
$[3] = t2;
|
|
3292
|
-
} else {
|
|
3293
|
-
t1 = $[2];
|
|
3294
|
-
t2 = $[3];
|
|
3295
3622
|
}
|
|
3296
|
-
|
|
3297
|
-
|
|
3623
|
+
return constraints;
|
|
3624
|
+
}
|
|
3625
|
+
function getStringConstraints(schema) {
|
|
3626
|
+
const unwrapped = unwrapSchema(schema);
|
|
3627
|
+
if (!(unwrapped instanceof z.ZodString)) {
|
|
3628
|
+
return {};
|
|
3298
3629
|
}
|
|
3299
|
-
|
|
3300
|
-
|
|
3301
|
-
|
|
3302
|
-
const
|
|
3303
|
-
|
|
3304
|
-
|
|
3305
|
-
|
|
3306
|
-
|
|
3307
|
-
|
|
3630
|
+
const constraints = {};
|
|
3631
|
+
const checks = unwrapped._def.checks;
|
|
3632
|
+
if (checks) {
|
|
3633
|
+
for (const check of checks) {
|
|
3634
|
+
const def = getCheckDef(check);
|
|
3635
|
+
if (!def) continue;
|
|
3636
|
+
if (def.type === 'min_length' && def.minimum !== undefined) {
|
|
3637
|
+
constraints.minLength = def.minimum;
|
|
3638
|
+
} else if (def.type === 'min' && def.value !== undefined) {
|
|
3639
|
+
constraints.minLength = def.value;
|
|
3640
|
+
} else if (def.type === 'max_length' && def.maximum !== undefined) {
|
|
3641
|
+
constraints.maxLength = def.maximum;
|
|
3642
|
+
} else if (def.type === 'max' && def.value !== undefined) {
|
|
3643
|
+
constraints.maxLength = def.value;
|
|
3644
|
+
} else if (def.type === 'length_equals' && def.length !== undefined) {
|
|
3645
|
+
constraints.minLength = def.length;
|
|
3646
|
+
constraints.maxLength = def.length;
|
|
3647
|
+
} else if (def.type === 'length' && def.value !== undefined) {
|
|
3648
|
+
constraints.minLength = def.value;
|
|
3649
|
+
constraints.maxLength = def.value;
|
|
3650
|
+
} else if (def.type === 'string_format' && def.regex) {
|
|
3651
|
+
constraints.pattern = def.regex.source;
|
|
3652
|
+
} else if (def.type === 'regex' && def.regex) {
|
|
3653
|
+
constraints.pattern = def.regex.source;
|
|
3654
|
+
}
|
|
3655
|
+
}
|
|
3308
3656
|
}
|
|
3309
|
-
|
|
3310
|
-
|
|
3311
|
-
|
|
3312
|
-
|
|
3313
|
-
|
|
3314
|
-
|
|
3315
|
-
|
|
3316
|
-
|
|
3317
|
-
|
|
3318
|
-
|
|
3319
|
-
|
|
3320
|
-
|
|
3321
|
-
|
|
3322
|
-
|
|
3323
|
-
|
|
3657
|
+
return constraints;
|
|
3658
|
+
}
|
|
3659
|
+
function getDateConstraints(schema) {
|
|
3660
|
+
const unwrapped = unwrapSchema(schema);
|
|
3661
|
+
if (!(unwrapped instanceof z.ZodDate)) {
|
|
3662
|
+
return {};
|
|
3663
|
+
}
|
|
3664
|
+
const constraints = {};
|
|
3665
|
+
const checks = unwrapped._def.checks;
|
|
3666
|
+
if (checks) {
|
|
3667
|
+
for (const check of checks) {
|
|
3668
|
+
const def = getCheckDef(check);
|
|
3669
|
+
if (!def) continue;
|
|
3670
|
+
if (def.type === 'greater_than' || def.type === 'min') {
|
|
3671
|
+
const value = def.value;
|
|
3672
|
+
constraints.min = value instanceof Date ? value : new Date(value);
|
|
3673
|
+
} else if (def.type === 'less_than' || def.type === 'max') {
|
|
3674
|
+
const value = def.value;
|
|
3675
|
+
constraints.max = value instanceof Date ? value : new Date(value);
|
|
3676
|
+
}
|
|
3324
3677
|
}
|
|
3325
3678
|
}
|
|
3326
|
-
return
|
|
3327
|
-
|
|
3328
|
-
|
|
3679
|
+
return constraints;
|
|
3680
|
+
}
|
|
3681
|
+
function getSchemaRequirements(schema) {
|
|
3682
|
+
const result = {};
|
|
3683
|
+
const shape = schema.shape;
|
|
3684
|
+
for (const key in shape) {
|
|
3685
|
+
result[key] = isSchemaRequired(shape[key]);
|
|
3686
|
+
}
|
|
3687
|
+
return result;
|
|
3688
|
+
}
|
|
3689
|
+
function analyzeSchema(schema) {
|
|
3690
|
+
const shape = schema.shape;
|
|
3691
|
+
return Object.entries(shape).map(([name, fieldSchema]) => {
|
|
3692
|
+
const type = getSchemaTypeName(fieldSchema);
|
|
3693
|
+
const info = {
|
|
3694
|
+
name,
|
|
3695
|
+
required: isSchemaRequired(fieldSchema),
|
|
3696
|
+
type,
|
|
3697
|
+
schema: fieldSchema
|
|
3698
|
+
};
|
|
3699
|
+
if (type === 'number') {
|
|
3700
|
+
const constraints = getNumberConstraints(fieldSchema);
|
|
3701
|
+
if (constraints.min !== undefined) info.min = constraints.min;
|
|
3702
|
+
if (constraints.max !== undefined) info.max = constraints.max;
|
|
3703
|
+
if (constraints.step !== undefined) info.step = constraints.step;
|
|
3704
|
+
} else if (type === 'string') {
|
|
3705
|
+
const constraints = getStringConstraints(fieldSchema);
|
|
3706
|
+
if (constraints.minLength !== undefined) info.minLength = constraints.minLength;
|
|
3707
|
+
if (constraints.maxLength !== undefined) info.maxLength = constraints.maxLength;
|
|
3708
|
+
if (constraints.pattern !== undefined) info.pattern = constraints.pattern;
|
|
3709
|
+
} else if (type === 'date') {
|
|
3710
|
+
const constraints = getDateConstraints(fieldSchema);
|
|
3711
|
+
if (constraints.min !== undefined) info.minDate = constraints.min;
|
|
3712
|
+
if (constraints.max !== undefined) info.maxDate = constraints.max;
|
|
3713
|
+
}
|
|
3714
|
+
return info;
|
|
3329
3715
|
});
|
|
3330
|
-
}
|
|
3331
|
-
|
|
3332
|
-
|
|
3716
|
+
}
|
|
3717
|
+
function mergeSchemaRequirements(schema, fields) {
|
|
3718
|
+
const requirements = getSchemaRequirements(schema);
|
|
3719
|
+
return fields.map(field => {
|
|
3720
|
+
const schemaRequired = requirements[field.name];
|
|
3721
|
+
if (schemaRequired !== undefined) {
|
|
3722
|
+
return {
|
|
3723
|
+
...field,
|
|
3724
|
+
required: schemaRequired
|
|
3725
|
+
};
|
|
3726
|
+
}
|
|
3727
|
+
return field;
|
|
3728
|
+
});
|
|
3729
|
+
}
|
|
3730
|
+
function mergeSchemaConstraints(schema, fields) {
|
|
3731
|
+
const fieldInfoMap = new Map();
|
|
3732
|
+
analyzeSchema(schema).forEach(info => {
|
|
3733
|
+
fieldInfoMap.set(info.name, info);
|
|
3734
|
+
});
|
|
3735
|
+
return fields.map(field => {
|
|
3736
|
+
const schemaInfo = fieldInfoMap.get(field.name);
|
|
3737
|
+
if (!schemaInfo) {
|
|
3738
|
+
return field;
|
|
3739
|
+
}
|
|
3740
|
+
const merged = {
|
|
3741
|
+
...field,
|
|
3742
|
+
required: schemaInfo.required
|
|
3743
|
+
};
|
|
3744
|
+
if (schemaInfo.type === 'number') {
|
|
3745
|
+
if (schemaInfo.min !== undefined) {
|
|
3746
|
+
merged.min = schemaInfo.min;
|
|
3747
|
+
}
|
|
3748
|
+
if (schemaInfo.max !== undefined) {
|
|
3749
|
+
merged.max = schemaInfo.max;
|
|
3750
|
+
}
|
|
3751
|
+
if (schemaInfo.step !== undefined) {
|
|
3752
|
+
merged.step = schemaInfo.step;
|
|
3753
|
+
}
|
|
3754
|
+
}
|
|
3755
|
+
if (schemaInfo.type === 'string') {
|
|
3756
|
+
if (schemaInfo.minLength !== undefined) {
|
|
3757
|
+
merged.minLength = schemaInfo.minLength;
|
|
3758
|
+
}
|
|
3759
|
+
if (schemaInfo.maxLength !== undefined) {
|
|
3760
|
+
merged.maxLength = schemaInfo.maxLength;
|
|
3761
|
+
}
|
|
3762
|
+
if (schemaInfo.pattern !== undefined) {
|
|
3763
|
+
merged.pattern = schemaInfo.pattern;
|
|
3764
|
+
}
|
|
3765
|
+
}
|
|
3766
|
+
if (schemaInfo.type === 'date') {
|
|
3767
|
+
if (schemaInfo.minDate !== undefined) {
|
|
3768
|
+
merged.min = schemaInfo.minDate.toISOString().split('T')[0];
|
|
3769
|
+
}
|
|
3770
|
+
if (schemaInfo.maxDate !== undefined) {
|
|
3771
|
+
merged.max = schemaInfo.maxDate.toISOString().split('T')[0];
|
|
3772
|
+
}
|
|
3773
|
+
}
|
|
3774
|
+
return merged;
|
|
3775
|
+
});
|
|
3776
|
+
}
|
|
3333
3777
|
function deepMerge(target, source) {
|
|
3334
3778
|
const result = {
|
|
3335
3779
|
...target
|
|
@@ -3355,8 +3799,9 @@ const defaultValidators = {
|
|
|
3355
3799
|
tel: stringValidator,
|
|
3356
3800
|
number: field => {
|
|
3357
3801
|
let schema = z.coerce.number();
|
|
3358
|
-
|
|
3359
|
-
if (
|
|
3802
|
+
const numberField = field;
|
|
3803
|
+
if (numberField.min !== undefined) schema = schema.min(numberField.min);
|
|
3804
|
+
if (numberField.max !== undefined) schema = schema.max(numberField.max);
|
|
3360
3805
|
return schema;
|
|
3361
3806
|
},
|
|
3362
3807
|
checkbox: () => z.coerce.boolean(),
|
|
@@ -3383,173 +3828,502 @@ function getValidatorForField(field) {
|
|
|
3383
3828
|
}
|
|
3384
3829
|
return z.any();
|
|
3385
3830
|
}
|
|
3386
|
-
function
|
|
3831
|
+
function buildArrayItemSchema(arrayField) {
|
|
3832
|
+
const itemShape = {};
|
|
3833
|
+
for (const field of arrayField.fields) {
|
|
3834
|
+
const validator = getValidatorForField(field);
|
|
3835
|
+
itemShape[field.name] = field.required ? validator : validator.optional();
|
|
3836
|
+
}
|
|
3837
|
+
return z.object(itemShape);
|
|
3838
|
+
}
|
|
3839
|
+
function buildSchema(fields) {
|
|
3387
3840
|
const shape = {};
|
|
3388
|
-
for (const
|
|
3389
|
-
if (
|
|
3390
|
-
let
|
|
3391
|
-
if (
|
|
3392
|
-
|
|
3393
|
-
childItems.push(...column.children);
|
|
3394
|
-
}
|
|
3395
|
-
} else if (item.type === 'section') {
|
|
3396
|
-
childItems = item.children;
|
|
3841
|
+
for (const field of fields) {
|
|
3842
|
+
if (isArrayField(field)) {
|
|
3843
|
+
let arraySchema = z.array(buildArrayItemSchema(field));
|
|
3844
|
+
if (field.minItems !== undefined) {
|
|
3845
|
+
arraySchema = arraySchema.min(field.minItems);
|
|
3397
3846
|
}
|
|
3398
|
-
if (
|
|
3399
|
-
|
|
3400
|
-
} else {
|
|
3401
|
-
const childSchema = buildSchema(childItems);
|
|
3402
|
-
if (childSchema instanceof z.ZodObject) {
|
|
3403
|
-
Object.assign(shape, childSchema.shape);
|
|
3404
|
-
}
|
|
3847
|
+
if (field.maxItems !== undefined) {
|
|
3848
|
+
arraySchema = arraySchema.max(field.maxItems);
|
|
3405
3849
|
}
|
|
3850
|
+
shape[field.name] = arraySchema;
|
|
3406
3851
|
} else {
|
|
3407
|
-
const field = item;
|
|
3408
3852
|
const validator = getValidatorForField(field);
|
|
3409
3853
|
shape[field.name] = field.required ? validator : validator.optional();
|
|
3410
3854
|
}
|
|
3411
3855
|
}
|
|
3412
3856
|
return z.object(shape);
|
|
3413
3857
|
}
|
|
3414
|
-
function
|
|
3858
|
+
function buildArrayItemDefaults(arrayField) {
|
|
3859
|
+
var _a;
|
|
3860
|
+
const item = {};
|
|
3861
|
+
for (const field of arrayField.fields) {
|
|
3862
|
+
item[field.name] = (_a = field.defaultValue) !== null && _a !== void 0 ? _a : '';
|
|
3863
|
+
}
|
|
3864
|
+
return item;
|
|
3865
|
+
}
|
|
3866
|
+
function buildDefaultValues(fields) {
|
|
3415
3867
|
var _a;
|
|
3416
3868
|
const values = {};
|
|
3417
|
-
for (const
|
|
3418
|
-
if (
|
|
3419
|
-
|
|
3420
|
-
|
|
3421
|
-
|
|
3422
|
-
|
|
3423
|
-
|
|
3424
|
-
|
|
3425
|
-
childItems = item.children;
|
|
3426
|
-
}
|
|
3427
|
-
if (item.wrapFieldNames && item.name) {
|
|
3428
|
-
values[item.name] = buildDefaultValues(childItems);
|
|
3869
|
+
for (const field of fields) {
|
|
3870
|
+
if (isArrayField(field)) {
|
|
3871
|
+
if (field.defaultValue && field.defaultValue.length > 0) {
|
|
3872
|
+
values[field.name] = field.defaultValue;
|
|
3873
|
+
} else if (field.minItems && field.minItems > 0) {
|
|
3874
|
+
values[field.name] = Array.from({
|
|
3875
|
+
length: field.minItems
|
|
3876
|
+
}, () => buildArrayItemDefaults(field));
|
|
3429
3877
|
} else {
|
|
3430
|
-
|
|
3878
|
+
values[field.name] = [];
|
|
3431
3879
|
}
|
|
3432
3880
|
} else {
|
|
3433
|
-
const field = item;
|
|
3434
3881
|
values[field.name] = (_a = field.defaultValue) !== null && _a !== void 0 ? _a : '';
|
|
3435
3882
|
}
|
|
3436
3883
|
}
|
|
3437
3884
|
return values;
|
|
3438
3885
|
}
|
|
3439
|
-
|
|
3440
|
-
|
|
3441
|
-
return `layout-${item.type}-${index}`;
|
|
3442
|
-
}
|
|
3443
|
-
return item.name;
|
|
3444
|
-
}
|
|
3445
|
-
function FormGenerator(t0) {
|
|
3446
|
-
const $ = c(20);
|
|
3886
|
+
|
|
3887
|
+
function FormGenerator(props) {
|
|
3447
3888
|
const {
|
|
3448
3889
|
fields,
|
|
3449
|
-
onSubmit,
|
|
3450
3890
|
defaultValues,
|
|
3451
|
-
schema: userSchema,
|
|
3452
3891
|
className,
|
|
3453
|
-
submitText
|
|
3892
|
+
submitText = 'Submit',
|
|
3454
3893
|
disabled,
|
|
3455
|
-
mode
|
|
3456
|
-
|
|
3457
|
-
|
|
3458
|
-
|
|
3459
|
-
|
|
3460
|
-
|
|
3461
|
-
|
|
3462
|
-
|
|
3463
|
-
|
|
3464
|
-
|
|
3465
|
-
|
|
3466
|
-
|
|
3467
|
-
|
|
3468
|
-
|
|
3469
|
-
|
|
3470
|
-
|
|
3471
|
-
|
|
3894
|
+
mode = 'onChange',
|
|
3895
|
+
title,
|
|
3896
|
+
description,
|
|
3897
|
+
showReset = false,
|
|
3898
|
+
resetText = 'Reset',
|
|
3899
|
+
children,
|
|
3900
|
+
ref,
|
|
3901
|
+
validateTypes
|
|
3902
|
+
} = props;
|
|
3903
|
+
const onSubmit = props.onSubmit;
|
|
3904
|
+
const formRef = useRef(null);
|
|
3905
|
+
useMemo(() => {
|
|
3906
|
+
if (validateTypes) {
|
|
3907
|
+
const registeredTypes = getRegisteredFieldTypes();
|
|
3908
|
+
const options = typeof validateTypes === 'object' ? validateTypes : {
|
|
3909
|
+
throwOnError: false,
|
|
3910
|
+
warn: true
|
|
3911
|
+
};
|
|
3912
|
+
validateFieldTypes(fields, registeredTypes, options);
|
|
3472
3913
|
}
|
|
3473
|
-
|
|
3474
|
-
|
|
3475
|
-
const
|
|
3476
|
-
let t4;
|
|
3477
|
-
if ($[2] !== defaultValues || $[3] !== fields) {
|
|
3914
|
+
}, [fields, validateTypes]);
|
|
3915
|
+
const schema = useMemo(() => buildSchema(fields), [fields]);
|
|
3916
|
+
const mergedDefaultValues = useMemo(() => {
|
|
3478
3917
|
const builtDefaults = buildDefaultValues(fields);
|
|
3479
|
-
|
|
3480
|
-
|
|
3481
|
-
|
|
3482
|
-
|
|
3483
|
-
|
|
3484
|
-
|
|
3485
|
-
}
|
|
3486
|
-
const
|
|
3487
|
-
|
|
3488
|
-
|
|
3489
|
-
|
|
3490
|
-
|
|
3491
|
-
|
|
3492
|
-
|
|
3493
|
-
|
|
3494
|
-
|
|
3495
|
-
let t6;
|
|
3496
|
-
if ($[7] !== mergedDefaultValues || $[8] !== mode || $[9] !== t5) {
|
|
3497
|
-
t6 = {
|
|
3498
|
-
resolver: t5,
|
|
3918
|
+
return defaultValues ? deepMerge(builtDefaults, defaultValues) : builtDefaults;
|
|
3919
|
+
}, [fields, defaultValues]);
|
|
3920
|
+
const form = useForm({
|
|
3921
|
+
resolver: a(schema),
|
|
3922
|
+
defaultValues: mergedDefaultValues,
|
|
3923
|
+
mode
|
|
3924
|
+
});
|
|
3925
|
+
const handleSubmit = form.handleSubmit(async data => {
|
|
3926
|
+
const context = {
|
|
3927
|
+
isDirty: form.formState.isDirty,
|
|
3928
|
+
dirtyFields: form.formState.dirtyFields,
|
|
3929
|
+
isValid: form.formState.isValid,
|
|
3930
|
+
errors: form.formState.errors,
|
|
3931
|
+
touchedFields: form.formState.touchedFields,
|
|
3932
|
+
isSubmitting: form.formState.isSubmitting,
|
|
3933
|
+
submitCount: form.formState.submitCount,
|
|
3499
3934
|
defaultValues: mergedDefaultValues,
|
|
3500
|
-
|
|
3935
|
+
form: form
|
|
3501
3936
|
};
|
|
3502
|
-
|
|
3503
|
-
|
|
3504
|
-
|
|
3505
|
-
|
|
3506
|
-
}
|
|
3507
|
-
|
|
3937
|
+
await onSubmit(data, context);
|
|
3938
|
+
});
|
|
3939
|
+
const handleReset = useCallback(() => {
|
|
3940
|
+
form.reset(mergedDefaultValues);
|
|
3941
|
+
}, [form, mergedDefaultValues]);
|
|
3942
|
+
useImperativeHandle(ref, () => ({
|
|
3943
|
+
setValues: values => {
|
|
3944
|
+
Object.entries(values).forEach(([key, value]) => {
|
|
3945
|
+
form.setValue(key, value, {
|
|
3946
|
+
shouldValidate: true,
|
|
3947
|
+
shouldDirty: true
|
|
3948
|
+
});
|
|
3949
|
+
});
|
|
3950
|
+
},
|
|
3951
|
+
getValues: () => form.getValues(),
|
|
3952
|
+
reset: values_0 => {
|
|
3953
|
+
if (values_0) {
|
|
3954
|
+
form.reset(deepMerge(mergedDefaultValues, values_0));
|
|
3955
|
+
} else {
|
|
3956
|
+
form.reset(mergedDefaultValues);
|
|
3957
|
+
}
|
|
3958
|
+
},
|
|
3959
|
+
submit: async () => {
|
|
3960
|
+
await handleSubmit();
|
|
3961
|
+
},
|
|
3962
|
+
clearErrors: () => form.clearErrors(),
|
|
3963
|
+
setError: (name, error) => form.setError(name, error),
|
|
3964
|
+
isValid: () => form.formState.isValid,
|
|
3965
|
+
isDirty: () => form.formState.isDirty,
|
|
3966
|
+
form: form
|
|
3967
|
+
}), [form, handleSubmit, mergedDefaultValues]);
|
|
3968
|
+
const SubmitButton = getFormComponent('SubmitButton');
|
|
3969
|
+
const FieldWrapper = getFormComponent('FieldWrapper');
|
|
3970
|
+
const FieldsWrapper = getFormComponent('FieldsWrapper');
|
|
3971
|
+
const {
|
|
3972
|
+
regularFields,
|
|
3973
|
+
arrayFields
|
|
3974
|
+
} = useMemo(() => {
|
|
3975
|
+
const regular = [];
|
|
3976
|
+
const arrays = [];
|
|
3977
|
+
fields.forEach(field => {
|
|
3978
|
+
if (isArrayField(field)) {
|
|
3979
|
+
arrays.push(field);
|
|
3980
|
+
} else {
|
|
3981
|
+
regular.push(field);
|
|
3982
|
+
}
|
|
3983
|
+
});
|
|
3984
|
+
return {
|
|
3985
|
+
regularFields: regular,
|
|
3986
|
+
arrayFields: arrays
|
|
3987
|
+
};
|
|
3988
|
+
}, [fields]);
|
|
3989
|
+
const fieldEntries = useMemo(() => {
|
|
3990
|
+
const entries = new Map();
|
|
3991
|
+
regularFields.forEach((field_0, index) => {
|
|
3992
|
+
if (!field_0.hidden) {
|
|
3993
|
+
const element = jsx(FieldWrapper, {
|
|
3994
|
+
name: field_0.name,
|
|
3995
|
+
type: field_0.type,
|
|
3996
|
+
className: field_0.className,
|
|
3997
|
+
children: jsx(FieldRenderer, {
|
|
3998
|
+
field: field_0
|
|
3999
|
+
})
|
|
4000
|
+
}, field_0.name || `field-${index}`);
|
|
4001
|
+
entries.set(field_0.name, {
|
|
4002
|
+
field: field_0,
|
|
4003
|
+
element,
|
|
4004
|
+
accessed: false
|
|
4005
|
+
});
|
|
4006
|
+
}
|
|
4007
|
+
});
|
|
4008
|
+
return entries;
|
|
4009
|
+
}, [regularFields, FieldWrapper]);
|
|
4010
|
+
const templateFields = useMemo(() => createTemplateFields(fieldEntries), [fieldEntries]);
|
|
4011
|
+
const buttons = useMemo(() => {
|
|
4012
|
+
const result = {
|
|
4013
|
+
submit: jsx(SubmitButton, {
|
|
4014
|
+
disabled: disabled || form.formState.isSubmitting,
|
|
4015
|
+
isSubmitting: form.formState.isSubmitting,
|
|
4016
|
+
children: submitText
|
|
4017
|
+
}, "submit")
|
|
4018
|
+
};
|
|
4019
|
+
if (showReset) {
|
|
4020
|
+
result.reset = jsx("button", {
|
|
4021
|
+
type: "button",
|
|
4022
|
+
onClick: handleReset,
|
|
4023
|
+
disabled: disabled,
|
|
4024
|
+
children: resetText
|
|
4025
|
+
}, "reset");
|
|
4026
|
+
}
|
|
4027
|
+
return result;
|
|
4028
|
+
}, [SubmitButton, disabled, form.formState.isSubmitting, submitText, showReset, resetText, handleReset]);
|
|
4029
|
+
const renderField = useCallback((field_1, options_0) => {
|
|
4030
|
+
return jsx(FieldRenderer, {
|
|
4031
|
+
field: field_1,
|
|
4032
|
+
namePrefix: options_0 === null || options_0 === void 0 ? void 0 : options_0.namePrefix
|
|
4033
|
+
});
|
|
4034
|
+
}, []);
|
|
4035
|
+
if (!children) {
|
|
4036
|
+
return jsx(FormProvider, {
|
|
4037
|
+
...form,
|
|
4038
|
+
children: jsx(FieldsProvider, {
|
|
4039
|
+
fields: fields,
|
|
4040
|
+
children: jsxs("form", {
|
|
4041
|
+
ref: formRef,
|
|
4042
|
+
onSubmit: handleSubmit,
|
|
4043
|
+
className: className,
|
|
4044
|
+
children: [jsxs(FieldsWrapper, {
|
|
4045
|
+
children: [regularFields.map((field_2, index_0) => jsx(FieldWrapper, {
|
|
4046
|
+
name: field_2.name,
|
|
4047
|
+
type: field_2.type,
|
|
4048
|
+
className: field_2.className,
|
|
4049
|
+
children: jsx(FieldRenderer, {
|
|
4050
|
+
field: field_2
|
|
4051
|
+
})
|
|
4052
|
+
}, field_2.name || `field-${index_0}`)), arrayFields.map(arrayField => jsx(FieldWrapper, {
|
|
4053
|
+
name: arrayField.name,
|
|
4054
|
+
type: "array",
|
|
4055
|
+
className: arrayField.className,
|
|
4056
|
+
children: jsx(ArrayFieldRenderer, {
|
|
4057
|
+
field: arrayField
|
|
4058
|
+
})
|
|
4059
|
+
}, arrayField.name))]
|
|
4060
|
+
}), jsx(SubmitButton, {
|
|
4061
|
+
disabled: disabled || form.formState.isSubmitting,
|
|
4062
|
+
isSubmitting: form.formState.isSubmitting,
|
|
4063
|
+
children: submitText
|
|
4064
|
+
})]
|
|
4065
|
+
})
|
|
4066
|
+
})
|
|
4067
|
+
});
|
|
3508
4068
|
}
|
|
3509
|
-
const
|
|
3510
|
-
|
|
3511
|
-
|
|
3512
|
-
|
|
3513
|
-
|
|
3514
|
-
|
|
3515
|
-
|
|
4069
|
+
const renderProps = {
|
|
4070
|
+
fields: templateFields,
|
|
4071
|
+
arrays: Object.fromEntries(arrayFields.map(arrayField_0 => [arrayField_0.name, {
|
|
4072
|
+
field: arrayField_0
|
|
4073
|
+
}])),
|
|
4074
|
+
buttons,
|
|
4075
|
+
title,
|
|
4076
|
+
description,
|
|
4077
|
+
form: form,
|
|
4078
|
+
isSubmitting: form.formState.isSubmitting,
|
|
4079
|
+
isValid: form.formState.isValid,
|
|
4080
|
+
isDirty: form.formState.isDirty,
|
|
4081
|
+
renderField,
|
|
4082
|
+
FieldWrapper,
|
|
4083
|
+
FieldsWrapper
|
|
4084
|
+
};
|
|
4085
|
+
const renderFn = children;
|
|
4086
|
+
return jsx(FormProvider, {
|
|
4087
|
+
...form,
|
|
4088
|
+
children: jsx(FieldsProvider, {
|
|
4089
|
+
fields: fields,
|
|
4090
|
+
children: jsx("form", {
|
|
4091
|
+
ref: formRef,
|
|
4092
|
+
onSubmit: handleSubmit,
|
|
4093
|
+
className: className,
|
|
4094
|
+
children: renderFn(renderProps)
|
|
4095
|
+
})
|
|
4096
|
+
})
|
|
4097
|
+
});
|
|
4098
|
+
}
|
|
4099
|
+
|
|
4100
|
+
function typedField() {
|
|
4101
|
+
return field => field;
|
|
4102
|
+
}
|
|
4103
|
+
function typedFields(fields) {
|
|
4104
|
+
return fields;
|
|
4105
|
+
}
|
|
4106
|
+
function StrictFormGenerator(props) {
|
|
4107
|
+
const {
|
|
4108
|
+
schema,
|
|
4109
|
+
fields,
|
|
4110
|
+
onSubmit,
|
|
4111
|
+
defaultValues,
|
|
4112
|
+
className,
|
|
4113
|
+
submitText = 'Submit',
|
|
4114
|
+
disabled,
|
|
4115
|
+
mode = 'onChange',
|
|
4116
|
+
title,
|
|
4117
|
+
description,
|
|
4118
|
+
showReset = false,
|
|
4119
|
+
resetText = 'Reset',
|
|
4120
|
+
children,
|
|
4121
|
+
ref,
|
|
4122
|
+
validateTypes
|
|
4123
|
+
} = props;
|
|
4124
|
+
const formRef = useRef(null);
|
|
4125
|
+
const fieldsWithRequirements = useMemo(() => {
|
|
4126
|
+
if (schema && typeof schema === 'object' && 'shape' in schema) {
|
|
4127
|
+
return mergeSchemaConstraints(schema, fields);
|
|
4128
|
+
}
|
|
4129
|
+
return fields;
|
|
4130
|
+
}, [schema, fields]);
|
|
4131
|
+
useMemo(() => {
|
|
4132
|
+
if (validateTypes) {
|
|
4133
|
+
const registeredTypes = getRegisteredFieldTypes();
|
|
4134
|
+
const options = typeof validateTypes === 'object' ? validateTypes : {
|
|
4135
|
+
throwOnError: false,
|
|
4136
|
+
warn: true
|
|
3516
4137
|
};
|
|
3517
|
-
|
|
3518
|
-
$[19] = t8;
|
|
3519
|
-
} else {
|
|
3520
|
-
t8 = $[19];
|
|
4138
|
+
validateFieldTypes(fieldsWithRequirements, registeredTypes, options);
|
|
3521
4139
|
}
|
|
3522
|
-
|
|
3523
|
-
|
|
3524
|
-
|
|
4140
|
+
}, [fieldsWithRequirements, validateTypes]);
|
|
4141
|
+
const mergedDefaultValues = useMemo(() => {
|
|
4142
|
+
const builtDefaults = buildDefaultValues(fieldsWithRequirements);
|
|
4143
|
+
return defaultValues ? deepMerge(builtDefaults, defaultValues) : builtDefaults;
|
|
4144
|
+
}, [fieldsWithRequirements, defaultValues]);
|
|
4145
|
+
const form = useForm({
|
|
4146
|
+
resolver: a(schema),
|
|
4147
|
+
defaultValues: mergedDefaultValues,
|
|
4148
|
+
mode
|
|
4149
|
+
});
|
|
4150
|
+
const handleSubmit = form.handleSubmit(async data => {
|
|
4151
|
+
const context = {
|
|
4152
|
+
isDirty: form.formState.isDirty,
|
|
4153
|
+
dirtyFields: form.formState.dirtyFields,
|
|
4154
|
+
isValid: form.formState.isValid,
|
|
4155
|
+
errors: form.formState.errors,
|
|
4156
|
+
touchedFields: form.formState.touchedFields,
|
|
4157
|
+
isSubmitting: form.formState.isSubmitting,
|
|
4158
|
+
submitCount: form.formState.submitCount,
|
|
4159
|
+
defaultValues: mergedDefaultValues,
|
|
4160
|
+
form: form
|
|
4161
|
+
};
|
|
4162
|
+
await onSubmit(data, context);
|
|
4163
|
+
});
|
|
4164
|
+
const handleReset = useCallback(() => {
|
|
4165
|
+
form.reset(mergedDefaultValues);
|
|
4166
|
+
}, [form, mergedDefaultValues]);
|
|
4167
|
+
useImperativeHandle(ref, () => ({
|
|
4168
|
+
setValues: values => {
|
|
4169
|
+
Object.entries(values).forEach(([key, value]) => {
|
|
4170
|
+
form.setValue(key, value, {
|
|
4171
|
+
shouldValidate: true,
|
|
4172
|
+
shouldDirty: true
|
|
4173
|
+
});
|
|
4174
|
+
});
|
|
4175
|
+
},
|
|
4176
|
+
getValues: () => form.getValues(),
|
|
4177
|
+
reset: values_0 => {
|
|
4178
|
+
if (values_0) {
|
|
4179
|
+
form.reset(deepMerge(mergedDefaultValues, values_0));
|
|
4180
|
+
} else {
|
|
4181
|
+
form.reset(mergedDefaultValues);
|
|
4182
|
+
}
|
|
4183
|
+
},
|
|
4184
|
+
submit: async () => {
|
|
4185
|
+
await handleSubmit();
|
|
4186
|
+
},
|
|
4187
|
+
clearErrors: () => form.clearErrors(),
|
|
4188
|
+
setError: (name, error) => form.setError(name, error),
|
|
4189
|
+
isValid: () => form.formState.isValid,
|
|
4190
|
+
isDirty: () => form.formState.isDirty,
|
|
4191
|
+
form: form
|
|
4192
|
+
}), [form, handleSubmit, mergedDefaultValues]);
|
|
4193
|
+
const SubmitButton = getFormComponent('SubmitButton');
|
|
4194
|
+
const FieldWrapper = getFormComponent('FieldWrapper');
|
|
4195
|
+
const FieldsWrapper = getFormComponent('FieldsWrapper');
|
|
4196
|
+
const {
|
|
4197
|
+
regularFields,
|
|
4198
|
+
arrayFields
|
|
4199
|
+
} = useMemo(() => {
|
|
4200
|
+
const regular = [];
|
|
4201
|
+
const arrays = [];
|
|
4202
|
+
fieldsWithRequirements.forEach(field => {
|
|
4203
|
+
if (isArrayField(field)) {
|
|
4204
|
+
arrays.push(field);
|
|
4205
|
+
} else {
|
|
4206
|
+
regular.push(field);
|
|
4207
|
+
}
|
|
4208
|
+
});
|
|
4209
|
+
return {
|
|
4210
|
+
regularFields: regular,
|
|
4211
|
+
arrayFields: arrays
|
|
4212
|
+
};
|
|
4213
|
+
}, [fieldsWithRequirements]);
|
|
4214
|
+
const fieldEntries = useMemo(() => {
|
|
4215
|
+
const entries = new Map();
|
|
4216
|
+
regularFields.forEach((field_0, index) => {
|
|
4217
|
+
if (!field_0.hidden) {
|
|
4218
|
+
const element = jsx(FieldWrapper, {
|
|
4219
|
+
name: field_0.name,
|
|
4220
|
+
type: field_0.type,
|
|
4221
|
+
className: field_0.className,
|
|
4222
|
+
children: jsx(FieldRenderer, {
|
|
4223
|
+
field: field_0
|
|
4224
|
+
})
|
|
4225
|
+
}, field_0.name || `field-${index}`);
|
|
4226
|
+
entries.set(field_0.name, {
|
|
4227
|
+
field: field_0,
|
|
4228
|
+
element,
|
|
4229
|
+
accessed: false
|
|
4230
|
+
});
|
|
4231
|
+
}
|
|
4232
|
+
});
|
|
4233
|
+
return entries;
|
|
4234
|
+
}, [regularFields, FieldWrapper]);
|
|
4235
|
+
const templateFields = useMemo(() => createTemplateFields(fieldEntries), [fieldEntries]);
|
|
4236
|
+
const buttons = useMemo(() => {
|
|
4237
|
+
const result = {
|
|
4238
|
+
submit: jsx(SubmitButton, {
|
|
4239
|
+
disabled: disabled || form.formState.isSubmitting,
|
|
4240
|
+
isSubmitting: form.formState.isSubmitting,
|
|
4241
|
+
children: submitText
|
|
4242
|
+
}, "submit")
|
|
4243
|
+
};
|
|
4244
|
+
if (showReset) {
|
|
4245
|
+
result.reset = jsx("button", {
|
|
4246
|
+
type: "button",
|
|
4247
|
+
onClick: handleReset,
|
|
4248
|
+
disabled: disabled,
|
|
4249
|
+
children: resetText
|
|
4250
|
+
}, "reset");
|
|
4251
|
+
}
|
|
4252
|
+
return result;
|
|
4253
|
+
}, [SubmitButton, disabled, form.formState.isSubmitting, submitText, showReset, resetText, handleReset]);
|
|
4254
|
+
const renderField = useCallback((field_1, options_0) => {
|
|
4255
|
+
return jsx(FieldRenderer, {
|
|
4256
|
+
field: field_1,
|
|
4257
|
+
namePrefix: options_0 === null || options_0 === void 0 ? void 0 : options_0.namePrefix
|
|
4258
|
+
});
|
|
4259
|
+
}, []);
|
|
4260
|
+
if (!children) {
|
|
4261
|
+
return jsx(FormProvider, {
|
|
3525
4262
|
...form,
|
|
3526
|
-
children:
|
|
3527
|
-
|
|
3528
|
-
|
|
3529
|
-
|
|
3530
|
-
|
|
3531
|
-
|
|
3532
|
-
children:
|
|
3533
|
-
|
|
4263
|
+
children: jsx(FieldsProvider, {
|
|
4264
|
+
fields: fieldsWithRequirements,
|
|
4265
|
+
children: jsxs("form", {
|
|
4266
|
+
ref: formRef,
|
|
4267
|
+
onSubmit: handleSubmit,
|
|
4268
|
+
className: className,
|
|
4269
|
+
children: [jsxs(FieldsWrapper, {
|
|
4270
|
+
children: [regularFields.map((field_2, index_0) => jsx(FieldWrapper, {
|
|
4271
|
+
name: field_2.name,
|
|
4272
|
+
type: field_2.type,
|
|
4273
|
+
className: field_2.className,
|
|
4274
|
+
children: jsx(FieldRenderer, {
|
|
4275
|
+
field: field_2
|
|
4276
|
+
})
|
|
4277
|
+
}, field_2.name || `field-${index_0}`)), arrayFields.map(arrayField => jsx(FieldWrapper, {
|
|
4278
|
+
name: arrayField.name,
|
|
4279
|
+
type: "array",
|
|
4280
|
+
className: arrayField.className,
|
|
4281
|
+
children: jsx(ArrayFieldRenderer, {
|
|
4282
|
+
field: arrayField
|
|
4283
|
+
})
|
|
4284
|
+
}, arrayField.name))]
|
|
4285
|
+
}), jsx(SubmitButton, {
|
|
4286
|
+
disabled: disabled || form.formState.isSubmitting,
|
|
4287
|
+
isSubmitting: form.formState.isSubmitting,
|
|
4288
|
+
children: submitText
|
|
4289
|
+
})]
|
|
4290
|
+
})
|
|
3534
4291
|
})
|
|
3535
4292
|
});
|
|
3536
|
-
$[11] = className;
|
|
3537
|
-
$[12] = disabled;
|
|
3538
|
-
$[13] = fields;
|
|
3539
|
-
$[14] = form;
|
|
3540
|
-
$[15] = onSubmit;
|
|
3541
|
-
$[16] = submitText;
|
|
3542
|
-
$[17] = t7;
|
|
3543
|
-
} else {
|
|
3544
|
-
t7 = $[17];
|
|
3545
4293
|
}
|
|
3546
|
-
|
|
4294
|
+
const renderProps = {
|
|
4295
|
+
fields: templateFields,
|
|
4296
|
+
arrays: Object.fromEntries(arrayFields.map(arrayField_0 => [arrayField_0.name, {
|
|
4297
|
+
field: arrayField_0
|
|
4298
|
+
}])),
|
|
4299
|
+
buttons,
|
|
4300
|
+
title,
|
|
4301
|
+
description,
|
|
4302
|
+
form: form,
|
|
4303
|
+
isSubmitting: form.formState.isSubmitting,
|
|
4304
|
+
isValid: form.formState.isValid,
|
|
4305
|
+
isDirty: form.formState.isDirty,
|
|
4306
|
+
renderField,
|
|
4307
|
+
FieldWrapper,
|
|
4308
|
+
FieldsWrapper
|
|
4309
|
+
};
|
|
4310
|
+
const renderFn = children;
|
|
4311
|
+
return jsx(FormProvider, {
|
|
4312
|
+
...form,
|
|
4313
|
+
children: jsx(FieldsProvider, {
|
|
4314
|
+
fields: fieldsWithRequirements,
|
|
4315
|
+
children: jsx("form", {
|
|
4316
|
+
ref: formRef,
|
|
4317
|
+
onSubmit: handleSubmit,
|
|
4318
|
+
className: className,
|
|
4319
|
+
children: renderFn(renderProps)
|
|
4320
|
+
})
|
|
4321
|
+
})
|
|
4322
|
+
});
|
|
3547
4323
|
}
|
|
3548
|
-
function
|
|
3549
|
-
return
|
|
3550
|
-
item
|
|
3551
|
-
}, getItemKey(item, index));
|
|
4324
|
+
function createFieldFactory(_schema) {
|
|
4325
|
+
return field => field;
|
|
3552
4326
|
}
|
|
3553
4327
|
|
|
3554
|
-
export { Controller, FieldRenderer, FormGenerator, FormProvider, clearAllRegistries, clearFieldRegistry, clearFormComponentRegistry,
|
|
3555
|
-
//# sourceMappingURL=index.
|
|
4328
|
+
export { ArrayFieldRenderer, Controller, FieldRenderer, FieldsProvider, FormGenerator, FormProvider, 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, useWatch, validateFieldType, validateFieldTypes };
|
|
4329
|
+
//# sourceMappingURL=index.mjs.map
|