@form-instant/react-resolver-zod 0.1.5 → 0.1.7
Sign up to get free protection for your applications and to get access to all the features.
- package/dist/react-resolver-zod.cjs.development.js +5 -11
- package/dist/react-resolver-zod.cjs.development.js.map +1 -1
- package/dist/react-resolver-zod.cjs.production.min.js +1 -1
- package/dist/react-resolver-zod.cjs.production.min.js.map +1 -1
- package/dist/react-resolver-zod.esm.js +5 -11
- package/dist/react-resolver-zod.esm.js.map +1 -1
- package/package.json +3 -3
- package/src/element.tsx +3 -10
@@ -1,8 +1,8 @@
|
|
1
1
|
'use strict';
|
2
2
|
|
3
3
|
var jsxRuntime = require('react/jsx-runtime');
|
4
|
-
var reactInputMapping = require('@form-instant/react-input-mapping');
|
5
4
|
var react = require('react');
|
5
|
+
var reactInputMapping = require('@form-instant/react-input-mapping');
|
6
6
|
var useContextSelector = require('use-context-selector');
|
7
7
|
var zod = require('zod');
|
8
8
|
|
@@ -76,12 +76,12 @@ function inferFieldType(schema, fieldType) {
|
|
76
76
|
return match || 'string'; // Default to string for unknown types
|
77
77
|
}
|
78
78
|
|
79
|
-
var _excluded
|
79
|
+
var _excluded = ["fieldType"];
|
80
80
|
function parseField(key, schema) {
|
81
81
|
var baseSchema = getBaseSchema(schema);
|
82
82
|
var _getFieldConfigInZodS = getFieldConfigInZodStack(schema),
|
83
83
|
fieldType = _getFieldConfigInZodS.fieldType,
|
84
|
-
fieldConfig = _objectWithoutPropertiesLoose(_getFieldConfigInZodS, _excluded
|
84
|
+
fieldConfig = _objectWithoutPropertiesLoose(_getFieldConfigInZodS, _excluded);
|
85
85
|
var type = inferFieldType(baseSchema, fieldType);
|
86
86
|
var defaultValue = getDefaultValueInZodStack(schema);
|
87
87
|
// Enums
|
@@ -152,21 +152,15 @@ var useFields = function useFields(key) {
|
|
152
152
|
return fields[key];
|
153
153
|
};
|
154
154
|
|
155
|
-
var _excluded = ["key"];
|
156
155
|
var FormInstantElement = function FormInstantElement(_ref) {
|
157
156
|
var name = _ref.name;
|
158
|
-
var InputMapping = reactInputMapping.useInputMapping();
|
159
157
|
var field = useFields(name);
|
160
158
|
var inputs = Object.values(field.schema);
|
161
159
|
var id = react.useId();
|
162
160
|
return jsxRuntime.jsx(jsxRuntime.Fragment, {
|
163
161
|
children: inputs.map(function (props) {
|
164
|
-
|
165
|
-
|
166
|
-
prop = _objectWithoutPropertiesLoose(props, _excluded);
|
167
|
-
if (!Element) return null;
|
168
|
-
return react.createElement(Element, _extends({}, prop, {
|
169
|
-
key: key + "-" + id
|
162
|
+
return react.createElement(reactInputMapping.ElementMapping, _extends({}, props, {
|
163
|
+
key: id + "-" + props.key
|
170
164
|
}));
|
171
165
|
})
|
172
166
|
});
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"react-resolver-zod.cjs.development.js","sources":["../src/context.tsx","../src/funcs/default-values.ts","../src/funcs/field-config.ts","../src/funcs/field-type-inference.ts","../src/funcs/schema-parser.ts","../src/useSchema.tsx","../src/element.tsx","../src/provider.tsx"],"sourcesContent":["import { createContext } from 'use-context-selector';\nimport { zodResolverProps } from './type';\n\nexport const ZodResolverContext = createContext<zodResolverProps>({} as zodResolverProps);\n","import { z } from 'zod';\nimport { ZodObjectOrWrapped } from './types';\n\nexport function getDefaultValueInZodStack(schema: z.ZodTypeAny): any {\n if (schema instanceof z.ZodDefault) {\n return schema._def.defaultValue();\n }\n\n if (schema instanceof z.ZodEffects) {\n return getDefaultValueInZodStack(schema.innerType());\n }\n\n return undefined;\n}\n\nexport function getDefaultValues(schema: ZodObjectOrWrapped): Record<string, any> {\n const objectSchema = schema instanceof z.ZodEffects ? schema.innerType() : schema;\n const shape = objectSchema.shape;\n\n const defaultValues: Record<string, any> = {};\n\n for (const [key, field] of Object.entries(shape)) {\n const defaultValue = getDefaultValueInZodStack(field as z.ZodTypeAny);\n if (defaultValue !== undefined) {\n defaultValues[key] = defaultValue;\n }\n }\n\n return defaultValues;\n}\n","import { RefinementEffect, z } from 'zod';\nimport { FieldConfig, SuperRefineFunction } from './types';\nexport const FIELD_CONFIG_SYMBOL = Symbol('GetFieldConfig');\n\nexport const createZodSchemaFieldConfig =\n <AdditionalRenderable = null, FieldTypes = string>() =>\n (config: FieldConfig<AdditionalRenderable, FieldTypes>): SuperRefineFunction => {\n const refinementFunction: SuperRefineFunction = () => {\n // Do nothing.\n };\n\n // @ts-expect-error This is a symbol and not a real value.\n refinementFunction[FIELD_CONFIG_SYMBOL] = config;\n\n return refinementFunction;\n };\n\nexport function getFieldConfigInZodStack(schema: z.ZodTypeAny): FieldConfig {\n const typedSchema = schema as unknown as z.ZodEffects<z.ZodNumber | z.ZodString>;\n\n if (typedSchema._def.typeName === 'ZodEffects') {\n const effect = typedSchema._def.effect as RefinementEffect<any>;\n const refinementFunction = effect.refinement;\n\n if (FIELD_CONFIG_SYMBOL in refinementFunction) {\n return refinementFunction[FIELD_CONFIG_SYMBOL] as FieldConfig;\n }\n }\n\n if ('innerType' in typedSchema._def) {\n return getFieldConfigInZodStack(typedSchema._def.innerType as unknown as z.ZodAny);\n }\n if ('schema' in typedSchema._def) {\n return getFieldConfigInZodStack((typedSchema._def as any).schema as z.ZodAny);\n }\n\n return {};\n}\n","import { z } from 'zod';\nimport { FieldConfig } from './types';\n\nexport function inferFieldType(schema: z.ZodTypeAny, fieldType?: FieldConfig['fieldType']): string {\n if (fieldType) {\n return fieldType;\n }\n\n const type = schema._def.typeName;\n\n const zodToInputType = new Map([\n ['ZodObject', 'object'],\n ['ZodString', 'string'],\n ['ZodNumber', 'number'],\n ['ZodBoolean', 'boolean'],\n ['ZodDate', 'date'],\n ['ZodEnum', 'select'],\n ['ZodNativeEnum', 'select'],\n ['ZodArray', 'array'],\n ]);\n\n const match = zodToInputType.get(type);\n\n return match || 'string'; // Default to string for unknown types\n}\n","import { z } from 'zod';\nimport { getDefaultValueInZodStack } from './default-values';\nimport { getFieldConfigInZodStack } from './field-config';\nimport { inferFieldType } from './field-type-inference';\nimport { ParsedField, ParsedSchema, ZodObjectOrWrapped } from './types';\n\nfunction parseField(key: string, schema: z.ZodTypeAny): ParsedField<any> {\n const baseSchema = getBaseSchema(schema);\n const { fieldType, ...fieldConfig } = getFieldConfigInZodStack(schema);\n const type = inferFieldType(baseSchema, fieldType);\n const defaultValue = getDefaultValueInZodStack(schema);\n\n // Enums\n const options = baseSchema._def.values;\n let optionValues: [string, string][] = [];\n if (options) {\n if (!Array.isArray(options)) {\n optionValues = Object.entries(options);\n } else {\n optionValues = options.map((value) => [value, value]);\n }\n }\n\n // Arrays and objects\n let subSchema: ParsedField<any>[] = [];\n if (baseSchema instanceof z.ZodObject) {\n subSchema = Object.entries(baseSchema.shape).map(([key, field]) =>\n parseField(key, field as z.ZodTypeAny),\n );\n }\n if (baseSchema instanceof z.ZodArray) {\n subSchema = [parseField('0', baseSchema._def.type)];\n }\n\n const resp = {\n key,\n type,\n required: !schema.isOptional(),\n default: defaultValue,\n fieldConfig,\n options: optionValues,\n schema: subSchema,\n description: baseSchema.description,\n };\n\n return resp;\n}\n\nfunction getBaseSchema<ChildType extends z.ZodAny | z.ZodTypeAny | z.AnyZodObject = z.ZodAny>(\n schema: ChildType | z.ZodEffects<ChildType>,\n): ChildType {\n if ('innerType' in schema._def) {\n return getBaseSchema(schema._def.innerType as ChildType);\n }\n if ('schema' in schema._def) {\n return getBaseSchema(schema._def.schema as ChildType);\n }\n\n return schema as ChildType;\n}\n\nexport const parseSchema = (S: ZodObjectOrWrapped): ParsedSchema => {\n const objectSchema = S instanceof z.ZodEffects ? S.innerType() : S;\n const shape = objectSchema.shape;\n\n const fields = Object.fromEntries(\n Object.entries(shape).map(([key, field]) => [key, parseField(key, field as z.ZodTypeAny)]),\n );\n\n return { fields };\n};\n","import { useContext } from 'use-context-selector';\nimport { ZodEffects, ZodObject } from 'zod';\nimport { ZodResolverContext } from './context';\nimport { parseSchema } from './funcs/schema-parser';\n\nexport const useSchema = () =>\n useContext(ZodResolverContext) as ZodObject<never, never> | ZodEffects<never, never>;\n\nexport const useFields = <Sc extends Record<string, any>>(key: keyof Sc) => {\n const S = useSchema();\n\n const { fields } = parseSchema(S);\n\n return fields[key as string];\n};\n","import { useInputMapping } from '@form-instant/react-input-mapping';\nimport { createElement, useId } from 'react';\nimport { useFields } from './useSchema';\n\nexport interface ElementProps<Schema extends Record<string, any>> {\n name: keyof Schema;\n}\n\nexport const FormInstantElement = <S extends Record<string, any>>({ name }: ElementProps<S>) => {\n const InputMapping = useInputMapping();\n const field = useFields(name);\n\n const inputs = Object.values(field.schema!);\n\n const id = useId();\n\n return (\n <>\n {inputs.map((props) => {\n const Element = InputMapping.get(props.type);\n\n const { key, ...prop } = props;\n\n if (!Element) return null;\n\n return createElement(Element, { ...prop, key: `${key}-${id}` });\n })}\n </>\n );\n};\n","import { FC, ReactNode } from 'react';\nimport { ZodResolverContext } from './context';\nimport { zodResolverProps } from './type';\nexport const FormInstantProvider: FC<{ schema: zodResolverProps; children: ReactNode }> = ({\n children,\n schema,\n}) => {\n return <ZodResolverContext.Provider value={schema}>{children}</ZodResolverContext.Provider>;\n};\n"],"names":["ZodResolverContext","createContext","getDefaultValueInZodStack","schema","z","ZodDefault","_def","defaultValue","ZodEffects","innerType","undefined","FIELD_CONFIG_SYMBOL","Symbol","createZodSchemaFieldConfig","config","refinementFunction","getFieldConfigInZodStack","typedSchema","typeName","effect","refinement","inferFieldType","fieldType","type","zodToInputType","Map","match","get","parseField","key","baseSchema","getBaseSchema","_getFieldConfigInZodS","fieldConfig","_objectWithoutPropertiesLoose","_excluded","options","values","optionValues","Array","isArray","Object","entries","map","value","subSchema","ZodObject","shape","_ref","field","ZodArray","resp","required","isOptional","description","parseSchema","S","objectSchema","fields","fromEntries","_ref2","useSchema","useContext","useFields","_parseSchema","FormInstantElement","name","InputMapping","useInputMapping","inputs","id","useId","_jsx","props","Element","prop","createElement","_extends","FormInstantProvider","children","Provider"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;AAGO,IAAMA,kBAAkB,gBAAGC,gCAAa,CAAmB,EAAsB,CAAC;;ACAnF,SAAUC,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IAAI,CAAA;AAEzB,MAAA,OAAOE,mBAAa,CAACF,OAAO,EAAAG,QAAA,KAAOF,IAAI,EAAA;QAAE9C,GAAG,EAAKA,GAAG,GAAIyC,GAAAA,GAAAA,EAAAA;AAAI,OAAA,CAAE,CAAC,CAAA;KAClE,CAAA;AACF,GAAA,CAAA,CAAA;AAEX;;IC1BaQ,mBAAmB,GAA0D,SAA7EA,mBAAmBA,CAAA9B,IAAA,EAG3B;AAAA,EAAA,IAFD+B,QAAQ,GAAA/B,IAAA,CAAR+B,QAAQ;IACR5E,MAAM,GAAA6C,IAAA,CAAN7C,MAAM,CAAA;AAEN,EAAA,OAAOqE,cAAA,CAACxE,kBAAkB,CAACgF,QAAQ,EAAA;AAACpC,IAAAA,KAAK,EAAEzC,MAAM;AAAA4E,IAAAA,QAAA,EAAGA,QAAAA;AAAQ,GAAA,CAA+B,CAAA;AAC/F;;;;;;;;"}
|
1
|
+
{"version":3,"file":"react-resolver-zod.cjs.development.js","sources":["../src/context.tsx","../src/funcs/default-values.ts","../src/funcs/field-config.ts","../src/funcs/field-type-inference.ts","../src/funcs/schema-parser.ts","../src/useSchema.tsx","../src/element.tsx","../src/provider.tsx"],"sourcesContent":["import { createContext } from 'use-context-selector';\nimport { zodResolverProps } from './type';\n\nexport const ZodResolverContext = createContext<zodResolverProps>({} as zodResolverProps);\n","import { z } from 'zod';\nimport { ZodObjectOrWrapped } from './types';\n\nexport function getDefaultValueInZodStack(schema: z.ZodTypeAny): any {\n if (schema instanceof z.ZodDefault) {\n return schema._def.defaultValue();\n }\n\n if (schema instanceof z.ZodEffects) {\n return getDefaultValueInZodStack(schema.innerType());\n }\n\n return undefined;\n}\n\nexport function getDefaultValues(schema: ZodObjectOrWrapped): Record<string, any> {\n const objectSchema = schema instanceof z.ZodEffects ? schema.innerType() : schema;\n const shape = objectSchema.shape;\n\n const defaultValues: Record<string, any> = {};\n\n for (const [key, field] of Object.entries(shape)) {\n const defaultValue = getDefaultValueInZodStack(field as z.ZodTypeAny);\n if (defaultValue !== undefined) {\n defaultValues[key] = defaultValue;\n }\n }\n\n return defaultValues;\n}\n","import { RefinementEffect, z } from 'zod';\nimport { FieldConfig, SuperRefineFunction } from './types';\nexport const FIELD_CONFIG_SYMBOL = Symbol('GetFieldConfig');\n\nexport const createZodSchemaFieldConfig =\n <AdditionalRenderable = null, FieldTypes = string>() =>\n (config: FieldConfig<AdditionalRenderable, FieldTypes>): SuperRefineFunction => {\n const refinementFunction: SuperRefineFunction = () => {\n // Do nothing.\n };\n\n // @ts-expect-error This is a symbol and not a real value.\n refinementFunction[FIELD_CONFIG_SYMBOL] = config;\n\n return refinementFunction;\n };\n\nexport function getFieldConfigInZodStack(schema: z.ZodTypeAny): FieldConfig {\n const typedSchema = schema as unknown as z.ZodEffects<z.ZodNumber | z.ZodString>;\n\n if (typedSchema._def.typeName === 'ZodEffects') {\n const effect = typedSchema._def.effect as RefinementEffect<any>;\n const refinementFunction = effect.refinement;\n\n if (FIELD_CONFIG_SYMBOL in refinementFunction) {\n return refinementFunction[FIELD_CONFIG_SYMBOL] as FieldConfig;\n }\n }\n\n if ('innerType' in typedSchema._def) {\n return getFieldConfigInZodStack(typedSchema._def.innerType as unknown as z.ZodAny);\n }\n if ('schema' in typedSchema._def) {\n return getFieldConfigInZodStack((typedSchema._def as any).schema as z.ZodAny);\n }\n\n return {};\n}\n","import { z } from 'zod';\nimport { FieldConfig } from './types';\n\nexport function inferFieldType(schema: z.ZodTypeAny, fieldType?: FieldConfig['fieldType']): string {\n if (fieldType) {\n return fieldType;\n }\n\n const type = schema._def.typeName;\n\n const zodToInputType = new Map([\n ['ZodObject', 'object'],\n ['ZodString', 'string'],\n ['ZodNumber', 'number'],\n ['ZodBoolean', 'boolean'],\n ['ZodDate', 'date'],\n ['ZodEnum', 'select'],\n ['ZodNativeEnum', 'select'],\n ['ZodArray', 'array'],\n ]);\n\n const match = zodToInputType.get(type);\n\n return match || 'string'; // Default to string for unknown types\n}\n","import { z } from 'zod';\nimport { getDefaultValueInZodStack } from './default-values';\nimport { getFieldConfigInZodStack } from './field-config';\nimport { inferFieldType } from './field-type-inference';\nimport { ParsedField, ParsedSchema, ZodObjectOrWrapped } from './types';\n\nfunction parseField(key: string, schema: z.ZodTypeAny): ParsedField<any> {\n const baseSchema = getBaseSchema(schema);\n const { fieldType, ...fieldConfig } = getFieldConfigInZodStack(schema);\n const type = inferFieldType(baseSchema, fieldType);\n const defaultValue = getDefaultValueInZodStack(schema);\n\n // Enums\n const options = baseSchema._def.values;\n let optionValues: [string, string][] = [];\n if (options) {\n if (!Array.isArray(options)) {\n optionValues = Object.entries(options);\n } else {\n optionValues = options.map((value) => [value, value]);\n }\n }\n\n // Arrays and objects\n let subSchema: ParsedField<any>[] = [];\n if (baseSchema instanceof z.ZodObject) {\n subSchema = Object.entries(baseSchema.shape).map(([key, field]) =>\n parseField(key, field as z.ZodTypeAny),\n );\n }\n if (baseSchema instanceof z.ZodArray) {\n subSchema = [parseField('0', baseSchema._def.type)];\n }\n\n const resp = {\n key,\n type,\n required: !schema.isOptional(),\n default: defaultValue,\n fieldConfig,\n options: optionValues,\n schema: subSchema,\n description: baseSchema.description,\n };\n\n return resp;\n}\n\nfunction getBaseSchema<ChildType extends z.ZodAny | z.ZodTypeAny | z.AnyZodObject = z.ZodAny>(\n schema: ChildType | z.ZodEffects<ChildType>,\n): ChildType {\n if ('innerType' in schema._def) {\n return getBaseSchema(schema._def.innerType as ChildType);\n }\n if ('schema' in schema._def) {\n return getBaseSchema(schema._def.schema as ChildType);\n }\n\n return schema as ChildType;\n}\n\nexport const parseSchema = (S: ZodObjectOrWrapped): ParsedSchema => {\n const objectSchema = S instanceof z.ZodEffects ? S.innerType() : S;\n const shape = objectSchema.shape;\n\n const fields = Object.fromEntries(\n Object.entries(shape).map(([key, field]) => [key, parseField(key, field as z.ZodTypeAny)]),\n );\n\n return { fields };\n};\n","import { useContext } from 'use-context-selector';\nimport { ZodEffects, ZodObject } from 'zod';\nimport { ZodResolverContext } from './context';\nimport { parseSchema } from './funcs/schema-parser';\n\nexport const useSchema = () =>\n useContext(ZodResolverContext) as ZodObject<never, never> | ZodEffects<never, never>;\n\nexport const useFields = <Sc extends Record<string, any>>(key: keyof Sc) => {\n const S = useSchema();\n\n const { fields } = parseSchema(S);\n\n return fields[key as string];\n};\n","import { ElementMapping } from '@form-instant/react-input-mapping';\nimport { useId } from 'react';\nimport { useFields } from './useSchema';\n\nexport interface ElementProps<Schema extends Record<string, any>> {\n name: keyof Schema;\n}\n\nexport const FormInstantElement = <S extends Record<string, any>>({ name }: ElementProps<S>) => {\n const field = useFields(name);\n\n const inputs = Object.values(field.schema!);\n\n const id = useId();\n\n return (\n <>\n {inputs.map((props) => {\n return <ElementMapping {...props} key={`${id}-${props.key}`} />;\n })}\n </>\n );\n};\n","import { FC, ReactNode } from 'react';\nimport { ZodResolverContext } from './context';\nimport { zodResolverProps } from './type';\nexport const FormInstantProvider: FC<{ schema: zodResolverProps; children: ReactNode }> = ({\n children,\n schema,\n}) => {\n return <ZodResolverContext.Provider value={schema}>{children}</ZodResolverContext.Provider>;\n};\n"],"names":["ZodResolverContext","createContext","getDefaultValueInZodStack","schema","z","ZodDefault","_def","defaultValue","ZodEffects","innerType","undefined","FIELD_CONFIG_SYMBOL","Symbol","createZodSchemaFieldConfig","config","refinementFunction","getFieldConfigInZodStack","typedSchema","typeName","effect","refinement","inferFieldType","fieldType","type","zodToInputType","Map","match","get","parseField","key","baseSchema","getBaseSchema","_getFieldConfigInZodS","fieldConfig","_objectWithoutPropertiesLoose","_excluded","options","values","optionValues","Array","isArray","Object","entries","map","value","subSchema","ZodObject","shape","_ref","field","ZodArray","resp","required","isOptional","description","parseSchema","S","objectSchema","fields","fromEntries","_ref2","useSchema","useContext","useFields","_parseSchema","FormInstantElement","name","inputs","id","useId","_jsx","props","_createElement","ElementMapping","_extends","FormInstantProvider","children","Provider"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;AAGO,IAAMA,kBAAkB,gBAAGC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A,EAG3B;AAAA,EAAA,IAFD4B,QAAQ,GAAA5B,IAAA,CAAR4B,QAAQ;IACRzE,MAAM,GAAA6C,IAAA,CAAN7C,MAAM,CAAA;AAEN,EAAA,OAAOmE,cAAA,CAACtE,kBAAkB,CAAC6E,QAAQ,EAAA;AAACjC,IAAAA,KAAK,EAAEzC,MAAM;AAAAyE,IAAAA,QAAA,EAAGA,QAAAA;AAAQ,GAAA,CAA+B,CAAA;AAC/F;;;;;;;;"}
|
@@ -1,2 +1,2 @@
|
|
1
|
-
"use strict";var e=require("react/jsx-runtime"),n=require("@form-instant/react-input-mapping"),
|
1
|
+
"use strict";var e=require("react/jsx-runtime"),n=require("react"),r=require("@form-instant/react-input-mapping"),t=require("use-context-selector"),i=require("zod");function a(){return a=Object.assign?Object.assign.bind():function(e){for(var n=1;n<arguments.length;n++){var r=arguments[n];for(var t in r)({}).hasOwnProperty.call(r,t)&&(e[t]=r[t])}return e},a.apply(null,arguments)}var o=t.createContext({});function f(e){return e instanceof i.z.ZodDefault?e._def.defaultValue():e instanceof i.z.ZodEffects?f(e.innerType()):void 0}var u=Symbol("GetFieldConfig");function c(e){var n=e;if("ZodEffects"===n._def.typeName){var r=n._def.effect.refinement;if(u in r)return r[u]}return"innerType"in n._def?c(n._def.innerType):"schema"in n._def?c(n._def.schema):{}}var s=["fieldType"];function d(e,n){var r=l(n),t=c(n),a=t.fieldType,o=function(e,n){if(null==e)return{};var r={};for(var t in e)if({}.hasOwnProperty.call(e,t)){if(n.includes(t))continue;r[t]=e[t]}return r}(t,s),u=function(e,n){if(n)return n;var r=e._def.typeName;return new Map([["ZodObject","object"],["ZodString","string"],["ZodNumber","number"],["ZodBoolean","boolean"],["ZodDate","date"],["ZodEnum","select"],["ZodNativeEnum","select"],["ZodArray","array"]]).get(r)||"string"}(r,a),p=f(n),m=r._def.values,v=[];m&&(v=Array.isArray(m)?m.map((function(e){return[e,e]})):Object.entries(m));var y=[];return r instanceof i.z.ZodObject&&(y=Object.entries(r.shape).map((function(e){return d(e[0],e[1])}))),r instanceof i.z.ZodArray&&(y=[d("0",r._def.type)]),{key:e,type:u,required:!n.isOptional(),default:p,fieldConfig:o,options:v,schema:y,description:r.description}}function l(e){return"innerType"in e._def?l(e._def.innerType):"schema"in e._def?l(e._def.schema):e}var p=function(){return t.useContext(o)},m=function(e){var n,r,t=(r=(n=p())instanceof i.z.ZodEffects?n.innerType():n,{fields:Object.fromEntries(Object.entries(r.shape).map((function(e){var n=e[0];return[n,d(n,e[1])]})))});return t.fields[e]};exports.FormInstantElement=function(t){var i=m(t.name),o=Object.values(i.schema),f=n.useId();return e.jsx(e.Fragment,{children:o.map((function(e){return n.createElement(r.ElementMapping,a({},e,{key:f+"-"+e.key}))}))})},exports.FormInstantProvider=function(n){return e.jsx(o.Provider,{value:n.schema,children:n.children})},exports.createZodSchemaFieldConfig=function(){return function(e){var n=function(){};return n[u]=e,n}},exports.useFields=m,exports.useSchema=p;
|
2
2
|
//# sourceMappingURL=react-resolver-zod.cjs.production.min.js.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"react-resolver-zod.cjs.production.min.js","sources":["../src/context.tsx","../src/funcs/default-values.ts","../src/funcs/field-config.ts","../src/funcs/schema-parser.ts","../src/funcs/field-type-inference.ts","../src/useSchema.tsx","../src/element.tsx","../src/provider.tsx"],"sourcesContent":["import { createContext } from 'use-context-selector';\nimport { zodResolverProps } from './type';\n\nexport const ZodResolverContext = createContext<zodResolverProps>({} as zodResolverProps);\n","import { z } from 'zod';\nimport { ZodObjectOrWrapped } from './types';\n\nexport function getDefaultValueInZodStack(schema: z.ZodTypeAny): any {\n if (schema instanceof z.ZodDefault) {\n return schema._def.defaultValue();\n }\n\n if (schema instanceof z.ZodEffects) {\n return getDefaultValueInZodStack(schema.innerType());\n }\n\n return undefined;\n}\n\nexport function getDefaultValues(schema: ZodObjectOrWrapped): Record<string, any> {\n const objectSchema = schema instanceof z.ZodEffects ? schema.innerType() : schema;\n const shape = objectSchema.shape;\n\n const defaultValues: Record<string, any> = {};\n\n for (const [key, field] of Object.entries(shape)) {\n const defaultValue = getDefaultValueInZodStack(field as z.ZodTypeAny);\n if (defaultValue !== undefined) {\n defaultValues[key] = defaultValue;\n }\n }\n\n return defaultValues;\n}\n","import { RefinementEffect, z } from 'zod';\nimport { FieldConfig, SuperRefineFunction } from './types';\nexport const FIELD_CONFIG_SYMBOL = Symbol('GetFieldConfig');\n\nexport const createZodSchemaFieldConfig =\n <AdditionalRenderable = null, FieldTypes = string>() =>\n (config: FieldConfig<AdditionalRenderable, FieldTypes>): SuperRefineFunction => {\n const refinementFunction: SuperRefineFunction = () => {\n // Do nothing.\n };\n\n // @ts-expect-error This is a symbol and not a real value.\n refinementFunction[FIELD_CONFIG_SYMBOL] = config;\n\n return refinementFunction;\n };\n\nexport function getFieldConfigInZodStack(schema: z.ZodTypeAny): FieldConfig {\n const typedSchema = schema as unknown as z.ZodEffects<z.ZodNumber | z.ZodString>;\n\n if (typedSchema._def.typeName === 'ZodEffects') {\n const effect = typedSchema._def.effect as RefinementEffect<any>;\n const refinementFunction = effect.refinement;\n\n if (FIELD_CONFIG_SYMBOL in refinementFunction) {\n return refinementFunction[FIELD_CONFIG_SYMBOL] as FieldConfig;\n }\n }\n\n if ('innerType' in typedSchema._def) {\n return getFieldConfigInZodStack(typedSchema._def.innerType as unknown as z.ZodAny);\n }\n if ('schema' in typedSchema._def) {\n return getFieldConfigInZodStack((typedSchema._def as any).schema as z.ZodAny);\n }\n\n return {};\n}\n","import { z } from 'zod';\nimport { getDefaultValueInZodStack } from './default-values';\nimport { getFieldConfigInZodStack } from './field-config';\nimport { inferFieldType } from './field-type-inference';\nimport { ParsedField, ParsedSchema, ZodObjectOrWrapped } from './types';\n\nfunction parseField(key: string, schema: z.ZodTypeAny): ParsedField<any> {\n const baseSchema = getBaseSchema(schema);\n const { fieldType, ...fieldConfig } = getFieldConfigInZodStack(schema);\n const type = inferFieldType(baseSchema, fieldType);\n const defaultValue = getDefaultValueInZodStack(schema);\n\n // Enums\n const options = baseSchema._def.values;\n let optionValues: [string, string][] = [];\n if (options) {\n if (!Array.isArray(options)) {\n optionValues = Object.entries(options);\n } else {\n optionValues = options.map((value) => [value, value]);\n }\n }\n\n // Arrays and objects\n let subSchema: ParsedField<any>[] = [];\n if (baseSchema instanceof z.ZodObject) {\n subSchema = Object.entries(baseSchema.shape).map(([key, field]) =>\n parseField(key, field as z.ZodTypeAny),\n );\n }\n if (baseSchema instanceof z.ZodArray) {\n subSchema = [parseField('0', baseSchema._def.type)];\n }\n\n const resp = {\n key,\n type,\n required: !schema.isOptional(),\n default: defaultValue,\n fieldConfig,\n options: optionValues,\n schema: subSchema,\n description: baseSchema.description,\n };\n\n return resp;\n}\n\nfunction getBaseSchema<ChildType extends z.ZodAny | z.ZodTypeAny | z.AnyZodObject = z.ZodAny>(\n schema: ChildType | z.ZodEffects<ChildType>,\n): ChildType {\n if ('innerType' in schema._def) {\n return getBaseSchema(schema._def.innerType as ChildType);\n }\n if ('schema' in schema._def) {\n return getBaseSchema(schema._def.schema as ChildType);\n }\n\n return schema as ChildType;\n}\n\nexport const parseSchema = (S: ZodObjectOrWrapped): ParsedSchema => {\n const objectSchema = S instanceof z.ZodEffects ? S.innerType() : S;\n const shape = objectSchema.shape;\n\n const fields = Object.fromEntries(\n Object.entries(shape).map(([key, field]) => [key, parseField(key, field as z.ZodTypeAny)]),\n );\n\n return { fields };\n};\n","import { z } from 'zod';\nimport { FieldConfig } from './types';\n\nexport function inferFieldType(schema: z.ZodTypeAny, fieldType?: FieldConfig['fieldType']): string {\n if (fieldType) {\n return fieldType;\n }\n\n const type = schema._def.typeName;\n\n const zodToInputType = new Map([\n ['ZodObject', 'object'],\n ['ZodString', 'string'],\n ['ZodNumber', 'number'],\n ['ZodBoolean', 'boolean'],\n ['ZodDate', 'date'],\n ['ZodEnum', 'select'],\n ['ZodNativeEnum', 'select'],\n ['ZodArray', 'array'],\n ]);\n\n const match = zodToInputType.get(type);\n\n return match || 'string'; // Default to string for unknown types\n}\n","import { useContext } from 'use-context-selector';\nimport { ZodEffects, ZodObject } from 'zod';\nimport { ZodResolverContext } from './context';\nimport { parseSchema } from './funcs/schema-parser';\n\nexport const useSchema = () =>\n useContext(ZodResolverContext) as ZodObject<never, never> | ZodEffects<never, never>;\n\nexport const useFields = <Sc extends Record<string, any>>(key: keyof Sc) => {\n const S = useSchema();\n\n const { fields } = parseSchema(S);\n\n return fields[key as string];\n};\n","import { useInputMapping } from '@form-instant/react-input-mapping';\nimport { createElement, useId } from 'react';\nimport { useFields } from './useSchema';\n\nexport interface ElementProps<Schema extends Record<string, any>> {\n name: keyof Schema;\n}\n\nexport const FormInstantElement = <S extends Record<string, any>>({ name }: ElementProps<S>) => {\n const InputMapping = useInputMapping();\n const field = useFields(name);\n\n const inputs = Object.values(field.schema!);\n\n const id = useId();\n\n return (\n <>\n {inputs.map((props) => {\n const Element = InputMapping.get(props.type);\n\n const { key, ...prop } = props;\n\n if (!Element) return null;\n\n return createElement(Element, { ...prop, key: `${key}-${id}` });\n })}\n </>\n );\n};\n","import { FC, ReactNode } from 'react';\nimport { ZodResolverContext } from './context';\nimport { zodResolverProps } from './type';\nexport const FormInstantProvider: FC<{ schema: zodResolverProps; children: ReactNode }> = ({\n children,\n schema,\n}) => {\n return <ZodResolverContext.Provider value={schema}>{children}</ZodResolverContext.Provider>;\n};\n"],"names":["ZodResolverContext","createContext","getDefaultValueInZodStack","schema","z","ZodDefault","_def","defaultValue","ZodEffects","innerType","FIELD_CONFIG_SYMBOL","Symbol","getFieldConfigInZodStack","typedSchema","typeName","refinementFunction","effect","refinement","parseField","key","baseSchema","getBaseSchema","_getFieldConfigInZodS","fieldType","fieldConfig","_objectWithoutPropertiesLoose","_excluded","type","Map","get","inferFieldType","options","values","optionValues","Array","isArray","map","value","Object","entries","subSchema","ZodObject","shape","_ref","ZodArray","required","isOptional","default","description","useSchema","useContext","useFields","S","objectSchema","_parseSchema","fields","fromEntries","_ref2","name","InputMapping","useInputMapping","field","inputs","id","useId","_jsx","props","Element","prop","createElement","_extends","jsx","Provider","children","config"],"mappings":"sgBAGO,IAAMA,EAAqBC,EAAaA,cAAmB,ICA5D,SAAUC,EAA0BC,GACtC,OAAIA,aAAkBC,EAACA,EAACC,WACbF,EAAOG,KAAKC,eAGnBJ,aAAkBC,EAACA,EAACI,WACbN,EAA0BC,EAAOM,kBAD5C,CAKJ,CCXO,IAAMC,EAAsBC,OAAO,kBAepC,SAAUC,EAAyBT,GACrC,IAAMU,EAAcV,EAEpB,GAAkC,eAA9BU,EAAYP,KAAKQ,SAA2B,CAC5C,IACMC,EADSF,EAAYP,KAAKU,OACEC,WAElC,GAAIP,KAAuBK,EACvB,OAAOA,EAAmBL,EAEjC,CAED,MAAI,cAAeG,EAAYP,KACpBM,EAAyBC,EAAYP,KAAKG,WAEjD,WAAYI,EAAYP,KACjBM,EAA0BC,EAAYP,KAAaH,QAGvD,EACX,qBC/BA,SAASe,EAAWC,EAAahB,GAC7B,IAAMiB,EAAaC,EAAclB,GACjCmB,EAAsCV,EAAyBT,GAAvDoB,EAASD,EAATC,UAAcC,EAAWC,EAAAH,EAAAI,GAC3BC,ECNM,SAAexB,EAAsBoB,GACjD,GAAIA,EACA,OAAOA,EAGX,IAAMI,EAAOxB,EAAOG,KAAKQ,SAezB,OAbuB,IAAIc,IAAI,CAC3B,CAAC,YAAa,UACd,CAAC,YAAa,UACd,CAAC,YAAa,UACd,CAAC,aAAc,WACf,CAAC,UAAW,QACZ,CAAC,UAAW,UACZ,CAAC,gBAAiB,UAClB,CAAC,WAAY,WAGYC,IAAIF,IAEjB,QACpB,CDfiBG,CAAeV,EAAYG,GAClChB,EAAeL,EAA0BC,GAGzC4B,EAAUX,EAAWd,KAAK0B,OAC5BC,EAAmC,GACnCF,IAIIE,EAHCC,MAAMC,QAAQJ,GAGAA,EAAQK,KAAI,SAACC,GAAK,MAAK,CAACA,EAAOA,MAF/BC,OAAOC,QAAQR,IAOtC,IAAIS,EAAgC,GAq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AACF,EAAOG,KAAOF,EAAI,CAAEjD,IAAQA,EAAO4C,IAAAA,KAFnC,SAMrC,8BC1B0F,SAA1DpB,GAI5B,OAAOsB,EAAAM,IAACvE,EAAmBwE,SAAQ,CAACnC,MAF9BM,EAANxC,OAEiDsE,SAHzC9B,EAAR8B,UAIJ,qCLHI,WADmC,OAEnC,SAACC,GACG,IAAM3D,EAA0C,aAOhD,OAFAA,EAAmBL,GAAuBgE,EAEnC3D,EACV"}
|
1
|
+
{"version":3,"file":"react-resolver-zod.cjs.production.min.js","sources":["../src/context.tsx","../src/funcs/default-values.ts","../src/funcs/field-config.ts","../src/funcs/schema-parser.ts","../src/funcs/field-type-inference.ts","../src/useSchema.tsx","../src/element.tsx","../src/provider.tsx"],"sourcesContent":["import { createContext } from 'use-context-selector';\nimport { zodResolverProps } from './type';\n\nexport const ZodResolverContext = createContext<zodResolverProps>({} as zodResolverProps);\n","import { z } from 'zod';\nimport { ZodObjectOrWrapped } from './types';\n\nexport function getDefaultValueInZodStack(schema: z.ZodTypeAny): any {\n if (schema instanceof z.ZodDefault) {\n return schema._def.defaultValue();\n }\n\n if (schema instanceof z.ZodEffects) {\n return getDefaultValueInZodStack(schema.innerType());\n }\n\n return undefined;\n}\n\nexport function getDefaultValues(schema: ZodObjectOrWrapped): Record<string, any> {\n const objectSchema = schema instanceof z.ZodEffects ? schema.innerType() : schema;\n const shape = objectSchema.shape;\n\n const defaultValues: Record<string, any> = {};\n\n for (const [key, field] of Object.entries(shape)) {\n const defaultValue = getDefaultValueInZodStack(field as z.ZodTypeAny);\n if (defaultValue !== undefined) {\n defaultValues[key] = defaultValue;\n }\n }\n\n return defaultValues;\n}\n","import { RefinementEffect, z } from 'zod';\nimport { FieldConfig, SuperRefineFunction } from './types';\nexport const FIELD_CONFIG_SYMBOL = Symbol('GetFieldConfig');\n\nexport const createZodSchemaFieldConfig =\n <AdditionalRenderable = null, FieldTypes = string>() =>\n (config: FieldConfig<AdditionalRenderable, FieldTypes>): SuperRefineFunction => {\n const refinementFunction: SuperRefineFunction = () => {\n // Do nothing.\n };\n\n // @ts-expect-error This is a symbol and not a real value.\n refinementFunction[FIELD_CONFIG_SYMBOL] = config;\n\n return refinementFunction;\n };\n\nexport function getFieldConfigInZodStack(schema: z.ZodTypeAny): FieldConfig {\n const typedSchema = schema as unknown as z.ZodEffects<z.ZodNumber | z.ZodString>;\n\n if (typedSchema._def.typeName === 'ZodEffects') {\n const effect = typedSchema._def.effect as RefinementEffect<any>;\n const refinementFunction = effect.refinement;\n\n if (FIELD_CONFIG_SYMBOL in refinementFunction) {\n return refinementFunction[FIELD_CONFIG_SYMBOL] as FieldConfig;\n }\n }\n\n if ('innerType' in typedSchema._def) {\n return getFieldConfigInZodStack(typedSchema._def.innerType as unknown as z.ZodAny);\n }\n if ('schema' in typedSchema._def) {\n return getFieldConfigInZodStack((typedSchema._def as any).schema as z.ZodAny);\n }\n\n return {};\n}\n","import { z } from 'zod';\nimport { getDefaultValueInZodStack } from './default-values';\nimport { getFieldConfigInZodStack } from './field-config';\nimport { inferFieldType } from './field-type-inference';\nimport { ParsedField, ParsedSchema, ZodObjectOrWrapped } from './types';\n\nfunction parseField(key: string, schema: z.ZodTypeAny): ParsedField<any> {\n const baseSchema = getBaseSchema(schema);\n const { fieldType, ...fieldConfig } = getFieldConfigInZodStack(schema);\n const type = inferFieldType(baseSchema, fieldType);\n const defaultValue = getDefaultValueInZodStack(schema);\n\n // Enums\n const options = baseSchema._def.values;\n let optionValues: [string, string][] = [];\n if (options) {\n if (!Array.isArray(options)) {\n optionValues = Object.entries(options);\n } else {\n optionValues = options.map((value) => [value, value]);\n }\n }\n\n // Arrays and objects\n let subSchema: ParsedField<any>[] = [];\n if (baseSchema instanceof z.ZodObject) {\n subSchema = Object.entries(baseSchema.shape).map(([key, field]) =>\n parseField(key, field as z.ZodTypeAny),\n );\n }\n if (baseSchema instanceof z.ZodArray) {\n subSchema = [parseField('0', baseSchema._def.type)];\n }\n\n const resp = {\n key,\n type,\n required: !schema.isOptional(),\n default: defaultValue,\n fieldConfig,\n options: optionValues,\n schema: subSchema,\n description: baseSchema.description,\n };\n\n return resp;\n}\n\nfunction getBaseSchema<ChildType extends z.ZodAny | z.ZodTypeAny | z.AnyZodObject = z.ZodAny>(\n schema: ChildType | z.ZodEffects<ChildType>,\n): ChildType {\n if ('innerType' in schema._def) {\n return getBaseSchema(schema._def.innerType as ChildType);\n }\n if ('schema' in schema._def) {\n return getBaseSchema(schema._def.schema as ChildType);\n }\n\n return schema as ChildType;\n}\n\nexport const parseSchema = (S: ZodObjectOrWrapped): ParsedSchema => {\n const objectSchema = S instanceof z.ZodEffects ? S.innerType() : S;\n const shape = objectSchema.shape;\n\n const fields = Object.fromEntries(\n Object.entries(shape).map(([key, field]) => [key, parseField(key, field as z.ZodTypeAny)]),\n );\n\n return { fields };\n};\n","import { z } from 'zod';\nimport { FieldConfig } from './types';\n\nexport function inferFieldType(schema: z.ZodTypeAny, fieldType?: FieldConfig['fieldType']): string {\n if (fieldType) {\n return fieldType;\n }\n\n const type = schema._def.typeName;\n\n const zodToInputType = new Map([\n ['ZodObject', 'object'],\n ['ZodString', 'string'],\n ['ZodNumber', 'number'],\n ['ZodBoolean', 'boolean'],\n ['ZodDate', 'date'],\n ['ZodEnum', 'select'],\n ['ZodNativeEnum', 'select'],\n ['ZodArray', 'array'],\n ]);\n\n const match = zodToInputType.get(type);\n\n return match || 'string'; // Default to string for unknown types\n}\n","import { useContext } from 'use-context-selector';\nimport { ZodEffects, ZodObject } from 'zod';\nimport { ZodResolverContext } from './context';\nimport { parseSchema } from './funcs/schema-parser';\n\nexport const useSchema = () =>\n useContext(ZodResolverContext) as ZodObject<never, never> | ZodEffects<never, never>;\n\nexport const useFields = <Sc extends Record<string, any>>(key: keyof Sc) => {\n const S = useSchema();\n\n const { fields } = parseSchema(S);\n\n return fields[key as string];\n};\n","import { ElementMapping } from '@form-instant/react-input-mapping';\nimport { useId } from 'react';\nimport { useFields } from './useSchema';\n\nexport interface ElementProps<Schema extends Record<string, any>> {\n name: keyof Schema;\n}\n\nexport const FormInstantElement = <S extends Record<string, any>>({ name }: ElementProps<S>) => {\n const field = useFields(name);\n\n const inputs = Object.values(field.schema!);\n\n const id = useId();\n\n return (\n <>\n {inputs.map((props) => {\n return <ElementMapping {...props} key={`${id}-${props.key}`} />;\n })}\n </>\n );\n};\n","import { FC, ReactNode } from 'react';\nimport { ZodResolverContext } from './context';\nimport { zodResolverProps } from './type';\nexport const FormInstantProvider: FC<{ schema: zodResolverProps; children: ReactNode }> = ({\n children,\n schema,\n}) => {\n return <ZodResolverContext.Provider value={schema}>{children}</ZodResolverContext.Provider>;\n};\n"],"names":["ZodResolverContext","createContext","getDefaultValueInZodStack","schema","z","ZodDefault","_def","defaultValue","ZodEffects","innerType","FIELD_CONFIG_SYMBOL","Symbol","getFieldConfigInZodStack","typedSchema","typeName","refinementFunction","effect","refinement","parseField","key","baseSchema","getBaseSchema","_getFieldConfigInZodS","fieldType","fieldConfig","_objectWithoutPropertiesLoose","_excluded","type","Map","get","inferFieldType","options","values","optionValues","Array","isArray","map","value","Object","entries","subSchema","ZodObject","shape","_ref","ZodArray","required","isOptional","default","description","useSchema","useContext","useFields","S","objectSchema","_parseSchema","fields","fromEntries","_ref2","field","name","inputs","id","useId","_jsx","props","_createElement","createElement","ElementMapping","_extends","jsx","Provider","children","config"],"mappings":"6XAGO,IAAMA,EAAqBC,EAAaA,cAAmB,ICA5D,SAAUC,EAA0BC,GACtC,OAAIA,aAAkBC,EAACA,EAACC,WACbF,EAAOG,KAAKC,eAGnBJ,aAAkBC,EAACA,EAACI,WACbN,EAA0BC,EAAOM,kBAD5C,CAKJ,CCXO,IAAMC,EAAsBC,OAAO,kBAepC,SAAUC,EAAyBT,GACrC,IAAMU,EAAcV,EAEpB,GAAkC,eAA9BU,EAAYP,KAAKQ,SAA2B,CAC5C,IACMC,EADSF,EAAYP,KAAKU,OACEC,WAElC,GAAIP,KAAuBK,EACvB,OAAOA,EAAmBL,E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WAAYN,EAAOG,KACZe,EAAclB,EAAOG,KAAKH,QAG9BA,CACX,CAEO,IExDM8C,EAAY,WAAH,OAClBC,EAAAA,WAAWlD,EAAyE,EAE3EmD,EAAY,SAAiChC,GACtD,IFoDwBiC,EAClBC,EEnDNC,GFmDMD,GADkBD,EEpDdH,eFqDwB7C,EAACA,EAACI,WAAa4C,EAAE3C,YAAc2C,EAO1D,CAAEG,OAJMjB,OAAOkB,YAClBlB,OAAOC,QAHGc,EAAaX,OAGDN,KAAI,SAAAqB,GAAA,IAAEtC,EAAGsC,EAAA,GAAO,MAAM,CAACtC,EAAKD,EAAWC,EAAvBsC,EAAA,IAAmD,OErD7F,OAFcH,EAANC,OAEMpC,EAClB,6BCNkC,SAAHwB,GAAgE,IACrFe,EAAQP,EADsDR,EAAJgB,MAG1DC,EAAStB,OAAON,OAAO0B,EAAMvD,QAE7B0D,EAAKC,EAAAA,QAEX,OACIC,EAAAA,yBACKH,EAAOxB,KAAI,SAAC4B,GACT,OAAOC,EAACC,cAAAC,EAAcA,eAAAC,KAAKJ,EAAK,CAAE7C,IAAQ0C,EAAMG,IAAAA,EAAM7C,WAItE,8BCnB0F,SAA1DwB,GAI5B,OAAOoB,EAAAM,IAACrE,EAAmBsE,SAAQ,CAACjC,MAF9BM,EAANxC,OAEiDoE,SAHzC5B,EAAR4B,UAIJ,qCLHI,WADmC,OAEnC,SAACC,GACG,IAAMzD,EAA0C,aAOhD,OAFAA,EAAmBL,GAAuB8D,EAEnCzD,EACV"}
|
@@ -1,6 +1,6 @@
|
|
1
1
|
import { jsx, Fragment } from 'react/jsx-runtime';
|
2
|
-
import { useInputMapping } from '@form-instant/react-input-mapping';
|
3
2
|
import { useId, createElement } from 'react';
|
3
|
+
import { ElementMapping } from '@form-instant/react-input-mapping';
|
4
4
|
import { createContext, useContext } from 'use-context-selector';
|
5
5
|
import { z } from 'zod';
|
6
6
|
|
@@ -74,12 +74,12 @@ function inferFieldType(schema, fieldType) {
|
|
74
74
|
return match || 'string'; // Default to string for unknown types
|
75
75
|
}
|
76
76
|
|
77
|
-
var _excluded
|
77
|
+
var _excluded = ["fieldType"];
|
78
78
|
function parseField(key, schema) {
|
79
79
|
var baseSchema = getBaseSchema(schema);
|
80
80
|
var _getFieldConfigInZodS = getFieldConfigInZodStack(schema),
|
81
81
|
fieldType = _getFieldConfigInZodS.fieldType,
|
82
|
-
fieldConfig = _objectWithoutPropertiesLoose(_getFieldConfigInZodS, _excluded
|
82
|
+
fieldConfig = _objectWithoutPropertiesLoose(_getFieldConfigInZodS, _excluded);
|
83
83
|
var type = inferFieldType(baseSchema, fieldType);
|
84
84
|
var defaultValue = getDefaultValueInZodStack(schema);
|
85
85
|
// Enums
|
@@ -150,21 +150,15 @@ var useFields = function useFields(key) {
|
|
150
150
|
return fields[key];
|
151
151
|
};
|
152
152
|
|
153
|
-
var _excluded = ["key"];
|
154
153
|
var FormInstantElement = function FormInstantElement(_ref) {
|
155
154
|
var name = _ref.name;
|
156
|
-
var InputMapping = useInputMapping();
|
157
155
|
var field = useFields(name);
|
158
156
|
var inputs = Object.values(field.schema);
|
159
157
|
var id = useId();
|
160
158
|
return jsx(Fragment, {
|
161
159
|
children: inputs.map(function (props) {
|
162
|
-
|
163
|
-
|
164
|
-
prop = _objectWithoutPropertiesLoose(props, _excluded);
|
165
|
-
if (!Element) return null;
|
166
|
-
return createElement(Element, _extends({}, prop, {
|
167
|
-
key: key + "-" + id
|
160
|
+
return createElement(ElementMapping, _extends({}, props, {
|
161
|
+
key: id + "-" + props.key
|
168
162
|
}));
|
169
163
|
})
|
170
164
|
});
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"react-resolver-zod.esm.js","sources":["../src/context.tsx","../src/funcs/default-values.ts","../src/funcs/field-config.ts","../src/funcs/field-type-inference.ts","../src/funcs/schema-parser.ts","../src/useSchema.tsx","../src/element.tsx","../src/provider.tsx"],"sourcesContent":["import { createContext } from 'use-context-selector';\nimport { zodResolverProps } from './type';\n\nexport const ZodResolverContext = createContext<zodResolverProps>({} as zodResolverProps);\n","import { z } from 'zod';\nimport { ZodObjectOrWrapped } from './types';\n\nexport function getDefaultValueInZodStack(schema: z.ZodTypeAny): any {\n if (schema instanceof z.ZodDefault) {\n return schema._def.defaultValue();\n }\n\n if (schema instanceof z.ZodEffects) {\n return getDefaultValueInZodStack(schema.innerType());\n }\n\n return undefined;\n}\n\nexport function getDefaultValues(schema: ZodObjectOrWrapped): Record<string, any> {\n const objectSchema = schema instanceof z.ZodEffects ? schema.innerType() : schema;\n const shape = objectSchema.shape;\n\n const defaultValues: Record<string, any> = {};\n\n for (const [key, field] of Object.entries(shape)) {\n const defaultValue = getDefaultValueInZodStack(field as z.ZodTypeAny);\n if (defaultValue !== undefined) {\n defaultValues[key] = defaultValue;\n }\n }\n\n return defaultValues;\n}\n","import { RefinementEffect, z } from 'zod';\nimport { FieldConfig, SuperRefineFunction } from './types';\nexport const FIELD_CONFIG_SYMBOL = Symbol('GetFieldConfig');\n\nexport const createZodSchemaFieldConfig =\n <AdditionalRenderable = null, FieldTypes = string>() =>\n (config: FieldConfig<AdditionalRenderable, FieldTypes>): SuperRefineFunction => {\n const refinementFunction: SuperRefineFunction = () => {\n // Do nothing.\n };\n\n // @ts-expect-error This is a symbol and not a real value.\n refinementFunction[FIELD_CONFIG_SYMBOL] = config;\n\n return refinementFunction;\n };\n\nexport function getFieldConfigInZodStack(schema: z.ZodTypeAny): FieldConfig {\n const typedSchema = schema as unknown as z.ZodEffects<z.ZodNumber | z.ZodString>;\n\n if (typedSchema._def.typeName === 'ZodEffects') {\n const effect = typedSchema._def.effect as RefinementEffect<any>;\n const refinementFunction = effect.refinement;\n\n if (FIELD_CONFIG_SYMBOL in refinementFunction) {\n return refinementFunction[FIELD_CONFIG_SYMBOL] as FieldConfig;\n }\n }\n\n if ('innerType' in typedSchema._def) {\n return getFieldConfigInZodStack(typedSchema._def.innerType as unknown as z.ZodAny);\n }\n if ('schema' in typedSchema._def) {\n return getFieldConfigInZodStack((typedSchema._def as any).schema as z.ZodAny);\n }\n\n return {};\n}\n","import { z } from 'zod';\nimport { FieldConfig } from './types';\n\nexport function inferFieldType(schema: z.ZodTypeAny, fieldType?: FieldConfig['fieldType']): string {\n if (fieldType) {\n return fieldType;\n }\n\n const type = schema._def.typeName;\n\n const zodToInputType = new Map([\n ['ZodObject', 'object'],\n ['ZodString', 'string'],\n ['ZodNumber', 'number'],\n ['ZodBoolean', 'boolean'],\n ['ZodDate', 'date'],\n ['ZodEnum', 'select'],\n ['ZodNativeEnum', 'select'],\n ['ZodArray', 'array'],\n ]);\n\n const match = zodToInputType.get(type);\n\n return match || 'string'; // Default to string for unknown types\n}\n","import { z } from 'zod';\nimport { getDefaultValueInZodStack } from './default-values';\nimport { getFieldConfigInZodStack } from './field-config';\nimport { inferFieldType } from './field-type-inference';\nimport { ParsedField, ParsedSchema, ZodObjectOrWrapped } from './types';\n\nfunction parseField(key: string, schema: z.ZodTypeAny): ParsedField<any> {\n const baseSchema = getBaseSchema(schema);\n const { fieldType, ...fieldConfig } = getFieldConfigInZodStack(schema);\n const type = inferFieldType(baseSchema, fieldType);\n const defaultValue = getDefaultValueInZodStack(schema);\n\n // Enums\n const options = baseSchema._def.values;\n let optionValues: [string, string][] = [];\n if (options) {\n if (!Array.isArray(options)) {\n optionValues = Object.entries(options);\n } else {\n optionValues = options.map((value) => [value, value]);\n }\n }\n\n // Arrays and objects\n let subSchema: ParsedField<any>[] = [];\n if (baseSchema instanceof z.ZodObject) {\n subSchema = Object.entries(baseSchema.shape).map(([key, field]) =>\n parseField(key, field as z.ZodTypeAny),\n );\n }\n if (baseSchema instanceof z.ZodArray) {\n subSchema = [parseField('0', baseSchema._def.type)];\n }\n\n const resp = {\n key,\n type,\n required: !schema.isOptional(),\n default: defaultValue,\n fieldConfig,\n options: optionValues,\n schema: subSchema,\n description: baseSchema.description,\n };\n\n return resp;\n}\n\nfunction getBaseSchema<ChildType extends z.ZodAny | z.ZodTypeAny | z.AnyZodObject = z.ZodAny>(\n schema: ChildType | z.ZodEffects<ChildType>,\n): ChildType {\n if ('innerType' in schema._def) {\n return getBaseSchema(schema._def.innerType as ChildType);\n }\n if ('schema' in schema._def) {\n return getBaseSchema(schema._def.schema as ChildType);\n }\n\n return schema as ChildType;\n}\n\nexport const parseSchema = (S: ZodObjectOrWrapped): ParsedSchema => {\n const objectSchema = S instanceof z.ZodEffects ? S.innerType() : S;\n const shape = objectSchema.shape;\n\n const fields = Object.fromEntries(\n Object.entries(shape).map(([key, field]) => [key, parseField(key, field as z.ZodTypeAny)]),\n );\n\n return { fields };\n};\n","import { useContext } from 'use-context-selector';\nimport { ZodEffects, ZodObject } from 'zod';\nimport { ZodResolverContext } from './context';\nimport { parseSchema } from './funcs/schema-parser';\n\nexport const useSchema = () =>\n useContext(ZodResolverContext) as ZodObject<never, never> | ZodEffects<never, never>;\n\nexport const useFields = <Sc extends Record<string, any>>(key: keyof Sc) => {\n const S = useSchema();\n\n const { fields } = parseSchema(S);\n\n return fields[key as string];\n};\n","import { useInputMapping } from '@form-instant/react-input-mapping';\nimport { createElement, useId } from 'react';\nimport { useFields } from './useSchema';\n\nexport interface ElementProps<Schema extends Record<string, any>> {\n name: keyof Schema;\n}\n\nexport const FormInstantElement = <S extends Record<string, any>>({ name }: ElementProps<S>) => {\n const InputMapping = useInputMapping();\n const field = useFields(name);\n\n const inputs = Object.values(field.schema!);\n\n const id = useId();\n\n return (\n <>\n {inputs.map((props) => {\n const Element = InputMapping.get(props.type);\n\n const { key, ...prop } = props;\n\n if (!Element) return null;\n\n return createElement(Element, { ...prop, key: `${key}-${id}` });\n })}\n </>\n );\n};\n","import { FC, ReactNode } from 'react';\nimport { ZodResolverContext } from './context';\nimport { zodResolverProps } from './type';\nexport const FormInstantProvider: FC<{ schema: zodResolverProps; children: ReactNode }> = ({\n children,\n schema,\n}) => {\n return <ZodResolverContext.Provider value={schema}>{children}</ZodResolverContext.Provider>;\n};\n"],"names":["ZodResolverContext","createContext","getDefaultValueInZodStack","schema","z","ZodDefault","_def","defaultValue","ZodEffects","innerType","undefined","FIELD_CONFIG_SYMBOL","Symbol","createZodSchemaFieldConfig","config","refinementFunction","getFieldConfigInZodStack","typedSchema","typeName","effect","refinement","inferFieldType","fieldType","type","zodToInputType","Map","match","get","parseField","key","baseSchema","getBaseSchema","_getFieldConfigInZodS","fieldConfig","_objectWithoutPropertiesLoose","_excluded","options","values","optionValues","Array","isArray","Object","entries","map","value","subSchema","ZodObject","shape","_ref","field","ZodArray","resp","required","isOptional","description","parseSchema","S","objectSchema","fields","fromEntries","_ref2","useSchema","useContext","useFields","_parseSchema","FormInstantElement","name","InputMapping","useInputMapping","inputs","id","useId","_jsx","props","Element","prop","createElement","_extends","FormInstantProvider","children","Provider"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;AAGO,IAAMA,kBAAkB,gBAAGC,aAAa,CAAmB,EAAsB,CAAC;;ACAnF,SAAUC,yBAAyBA,CAACC,MAAoB,EAAA;AAC1D,EAAA,IAAIA,MAAM,YAAYC,CAAC,CAACC,UAAU,EAAE;AAChC,IAAA,OAAOF,MAAM,CAACG,IAAI,CAACC,YAAY,EAAE,CAAA;AACpC,GAAA;AAED,EAAA,IAAIJ,MAAM,YAAYC,CAAC,CAACI,UAAU,EAAE;AAChC,IAAA,OAAON,yBAAyB,CAACC,MAAM,CAACM,SAAS,EAAE,CAAC,CAAA;AACvD,GAAA;AAED,EAAA,OAAOC,SAAS,CAAA;AACpB;;ACXO,IAAMC,mBAAmB,gBAAGC,MAAM,CAAC,gBAAgB,CAAC,CAAA;AAE9CC,IAAAA,0BAA0B,GACnC,SADSA,0BAA0BA,GAAA;EAAA,OAEnC,UAACC,MAAqD,EAAyB;AAC3E,IAAA,IAAMC,kBAAkB,GAAwB,SAA1CA,kBAAkBA,GAA6B;AACjD;KACH,CAAA;AAED;AACAA,IAAAA,kBAAkB,CAACJ,mBAAmB,CAAC,GAAGG,MAAM,CAAA;AAEhD,IAAA,OAAOC,kBAAkB,CAAA;G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,YAAY,GAAGC,eAAe,EAAE,CAAA;AACtC,EAAA,IAAMnB,KAAK,GAAGc,SAAS,CAACG,IAAI,CAAC,CAAA;EAE7B,IAAMG,MAAM,GAAG5B,MAAM,CAACJ,MAAM,CAACY,KAAK,CAAC9C,MAAO,CAAC,CAAA;AAE3C,EAAA,IAAMmE,EAAE,GAAGC,KAAK,EAAE,CAAA;EAElB,OACIC;cACKH,MAAM,CAAC1B,GAAG,CAAC,UAAC8B,KAAK,EAAI;MAClB,IAAMC,OAAO,GAAGP,YAAY,CAACxC,GAAG,CAAC8C,KAAK,CAAClD,IAAI,CAAC,CAAA;AAE5C,MAAA,IAAQM,GAAG,GAAc4C,KAAK,CAAtB5C,GAAG;AAAK8C,QAAAA,IAAI,GAAAzC,6BAAA,CAAKuC,KAAK,EAAAtC,SAAA,CAAA,CAAA;AAE9B,MAAA,IAAI,CAACuC,OAAO,EAAE,OAAO,IAAI,CAAA;AAEzB,MAAA,OAAOE,aAAa,CAACF,OAAO,EAAAG,QAAA,KAAOF,IAAI,EAAA;QAAE9C,GAAG,EAAKA,GAAG,GAAIyC,GAAAA,GAAAA,EAAAA;AAAI,OAAA,CAAE,CAAC,CAAA;KAClE,CAAA;AACF,GAAA,CAAA,CAAA;AAEX;;IC1BaQ,mBAAmB,GAA0D,SAA7EA,mBAAmBA,CAAA9B,IAAA,EAG3B;AAAA,EAAA,IAFD+B,QAAQ,GAAA/B,IAAA,CAAR+B,QAAQ;IACR5E,MAAM,GAAA6C,IAAA,CAAN7C,MAAM,CAAA;AAEN,EAAA,OAAOqE,GAAA,CAACxE,kBAAkB,CAACgF,QAAQ,EAAA;AAACpC,IAAAA,KAAK,EAAEzC,MAAM;AAAA4E,IAAAA,QAAA,EAAGA,QAAAA;AAAQ,GAAA,CAA+B,CAAA;AAC/F;;;;"}
|
1
|
+
{"version":3,"file":"react-resolver-zod.esm.js","sources":["../src/context.tsx","../src/funcs/default-values.ts","../src/funcs/field-config.ts","../src/funcs/field-type-inference.ts","../src/funcs/schema-parser.ts","../src/useSchema.tsx","../src/element.tsx","../src/provider.tsx"],"sourcesContent":["import { createContext } from 'use-context-selector';\nimport { zodResolverProps } from './type';\n\nexport const ZodResolverContext = createContext<zodResolverProps>({} as zodResolverProps);\n","import { z } from 'zod';\nimport { ZodObjectOrWrapped } from './types';\n\nexport function getDefaultValueInZodStack(schema: z.ZodTypeAny): any {\n if (schema instanceof z.ZodDefault) {\n return schema._def.defaultValue();\n }\n\n if (schema instanceof z.ZodEffects) {\n return getDefaultValueInZodStack(schema.innerType());\n }\n\n return undefined;\n}\n\nexport function getDefaultValues(schema: ZodObjectOrWrapped): Record<string, any> {\n const objectSchema = schema instanceof z.ZodEffects ? schema.innerType() : schema;\n const shape = objectSchema.shape;\n\n const defaultValues: Record<string, any> = {};\n\n for (const [key, field] of Object.entries(shape)) {\n const defaultValue = getDefaultValueInZodStack(field as z.ZodTypeAny);\n if (defaultValue !== undefined) {\n defaultValues[key] = defaultValue;\n }\n }\n\n return defaultValues;\n}\n","import { RefinementEffect, z } from 'zod';\nimport { FieldConfig, SuperRefineFunction } from './types';\nexport const FIELD_CONFIG_SYMBOL = Symbol('GetFieldConfig');\n\nexport const createZodSchemaFieldConfig =\n <AdditionalRenderable = null, FieldTypes = string>() =>\n (config: FieldConfig<AdditionalRenderable, FieldTypes>): SuperRefineFunction => {\n const refinementFunction: SuperRefineFunction = () => {\n // Do nothing.\n };\n\n // @ts-expect-error This is a symbol and not a real value.\n refinementFunction[FIELD_CONFIG_SYMBOL] = config;\n\n return refinementFunction;\n };\n\nexport function getFieldConfigInZodStack(schema: z.ZodTypeAny): FieldConfig {\n const typedSchema = schema as unknown as z.ZodEffects<z.ZodNumber | z.ZodString>;\n\n if (typedSchema._def.typeName === 'ZodEffects') {\n const effect = typedSchema._def.effect as RefinementEffect<any>;\n const refinementFunction = effect.refinement;\n\n if (FIELD_CONFIG_SYMBOL in refinementFunction) {\n return refinementFunction[FIELD_CONFIG_SYMBOL] as FieldConfig;\n }\n }\n\n if ('innerType' in typedSchema._def) {\n return getFieldConfigInZodStack(typedSchema._def.innerType as unknown as z.ZodAny);\n }\n if ('schema' in typedSchema._def) {\n return getFieldConfigInZodStack((typedSchema._def as any).schema as z.ZodAny);\n }\n\n return {};\n}\n","import { z } from 'zod';\nimport { FieldConfig } from './types';\n\nexport function inferFieldType(schema: z.ZodTypeAny, fieldType?: FieldConfig['fieldType']): string {\n if (fieldType) {\n return fieldType;\n }\n\n const type = schema._def.typeName;\n\n const zodToInputType = new Map([\n ['ZodObject', 'object'],\n ['ZodString', 'string'],\n ['ZodNumber', 'number'],\n ['ZodBoolean', 'boolean'],\n ['ZodDate', 'date'],\n ['ZodEnum', 'select'],\n ['ZodNativeEnum', 'select'],\n ['ZodArray', 'array'],\n ]);\n\n const match = zodToInputType.get(type);\n\n return match || 'string'; // Default to string for unknown types\n}\n","import { z } from 'zod';\nimport { getDefaultValueInZodStack } from './default-values';\nimport { getFieldConfigInZodStack } from './field-config';\nimport { inferFieldType } from './field-type-inference';\nimport { ParsedField, ParsedSchema, ZodObjectOrWrapped } from './types';\n\nfunction parseField(key: string, schema: z.ZodTypeAny): ParsedField<any> {\n const baseSchema = getBaseSchema(schema);\n const { fieldType, ...fieldConfig } = getFieldConfigInZodStack(schema);\n const type = inferFieldType(baseSchema, fieldType);\n const defaultValue = getDefaultValueInZodStack(schema);\n\n // Enums\n const options = baseSchema._def.values;\n let optionValues: [string, string][] = [];\n if (options) {\n if (!Array.isArray(options)) {\n optionValues = Object.entries(options);\n } else {\n optionValues = options.map((value) => [value, value]);\n }\n }\n\n // Arrays and objects\n let subSchema: ParsedField<any>[] = [];\n if (baseSchema instanceof z.ZodObject) {\n subSchema = Object.entries(baseSchema.shape).map(([key, field]) =>\n parseField(key, field as z.ZodTypeAny),\n );\n }\n if (baseSchema instanceof z.ZodArray) {\n subSchema = [parseField('0', baseSchema._def.type)];\n }\n\n const resp = {\n key,\n type,\n required: !schema.isOptional(),\n default: defaultValue,\n fieldConfig,\n options: optionValues,\n schema: subSchema,\n description: baseSchema.description,\n };\n\n return resp;\n}\n\nfunction getBaseSchema<ChildType extends z.ZodAny | z.ZodTypeAny | z.AnyZodObject = z.ZodAny>(\n schema: ChildType | z.ZodEffects<ChildType>,\n): ChildType {\n if ('innerType' in schema._def) {\n return getBaseSchema(schema._def.innerType as ChildType);\n }\n if ('schema' in schema._def) {\n return getBaseSchema(schema._def.schema as ChildType);\n }\n\n return schema as ChildType;\n}\n\nexport const parseSchema = (S: ZodObjectOrWrapped): ParsedSchema => {\n const objectSchema = S instanceof z.ZodEffects ? S.innerType() : S;\n const shape = objectSchema.shape;\n\n const fields = Object.fromEntries(\n Object.entries(shape).map(([key, field]) => [key, parseField(key, field as z.ZodTypeAny)]),\n );\n\n return { fields };\n};\n","import { useContext } from 'use-context-selector';\nimport { ZodEffects, ZodObject } from 'zod';\nimport { ZodResolverContext } from './context';\nimport { parseSchema } from './funcs/schema-parser';\n\nexport const useSchema = () =>\n useContext(ZodResolverContext) as ZodObject<never, never> | ZodEffects<never, never>;\n\nexport const useFields = <Sc extends Record<string, any>>(key: keyof Sc) => {\n const S = useSchema();\n\n const { fields } = parseSchema(S);\n\n return fields[key as string];\n};\n","import { ElementMapping } from '@form-instant/react-input-mapping';\nimport { useId } from 'react';\nimport { useFields } from './useSchema';\n\nexport interface ElementProps<Schema extends Record<string, any>> {\n name: keyof Schema;\n}\n\nexport const FormInstantElement = <S extends Record<string, any>>({ name }: ElementProps<S>) => {\n const field = useFields(name);\n\n const inputs = Object.values(field.schema!);\n\n const id = useId();\n\n return (\n <>\n {inputs.map((props) => {\n return <ElementMapping {...props} key={`${id}-${props.key}`} />;\n })}\n </>\n );\n};\n","import { FC, ReactNode } from 'react';\nimport { ZodResolverContext } from './context';\nimport { zodResolverProps } from './type';\nexport const FormInstantProvider: FC<{ schema: zodResolverProps; children: ReactNode }> = ({\n children,\n schema,\n}) => {\n return <ZodResolverContext.Provider value={schema}>{children}</ZodResolverContext.Provider>;\n};\n"],"names":["ZodResolverContext","createContext","getDefaultValueInZodStack","schema","z","ZodDefault","_def","defaultValue","ZodEffects","innerType","undefined","FIELD_CONFIG_SYMBOL","Symbol","createZodSchemaFieldConfig","config","refinementFunction","getFieldConfigInZodStack","typedSchema","typeName","effect","refinement","inferFieldType","fieldType","type","zodToInputType","Map","match","get","parseField","key","baseSchema","getBaseSchema","_getFieldConfigInZodS","fieldConfig","_objectWithoutPropertiesLoose","_excluded","options","values","optionValues","Array","isArray","Object","entries","map","value","subSchema","ZodObject","shape","_ref","field","ZodArray","resp","required","isOptional","description","parseSchema","S","objectSchema","fields","fromEntries","_ref2","useSchema","useContext","useFields","_parseSchema","FormInstantElement","name","inputs","id","useId","_jsx","props","_createElement","ElementMapping","_extends","FormInstantProvider","children","Provider"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;AAGO,IAAMA,kBAAkB,gBAAGC,aAAa,CAAmB,EAAsB,CAAC;;ACAnF,SAAUC,yBAAyBA,CAACC,MAAoB,EAAA;AAC1D,EAAA,IAAIA,MAAM,YAAYC,CAAC,CAACC,UAAU,EAAE;AAChC,IAAA,OAAOF,MAAM,CAACG,IAAI,CAACC,YAAY,EAAE,CAAA;AACpC,GAAA;AAED,EAAA,IAAIJ,MAAM,YAAYC,CAAC,CAACI,UAAU,EAAE;AAChC,IAAA,OAAON,yBAAyB,CAACC,MAAM,CAACM,SAAS,EAAE,CAAC,CAAA;AACvD,GAAA;AAED,EAAA,OAAOC,SAAS,CAAA;AACpB;;ACXO,IAAMC,mBAAmB,gBAAGC,MAAM,CAAC,gBAAgB,CAAC,CAAA;AAE9CC,IAAAA,0BAA0B,GACnC,SADSA,0BAA0BA,GAAA;EAAA,OAEnC,UAACC,MAAqD,EAAyB;AAC3E,IAAA,IAAMC,kBAAkB,GAAwB,SAA1CA,kBAAkBA,GAA6B;AACjD;KACH,CAAA;AAED;AACAA,IAAAA,kBAAkB,CAACJ,mBAAmB,CAAC,GAAGG,MAAM,CAAA;AAEhD,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AI,CAAA;AACpE,EAAA,IAAMjB,KAAK,GAAGc,SAAS,CAACG,IAAI,CAAC,CAAA;EAE7B,IAAMC,MAAM,GAAG1B,MAAM,CAACJ,MAAM,CAACY,KAAK,CAAC9C,MAAO,CAAC,CAAA;AAE3C,EAAA,IAAMiE,EAAE,GAAGC,KAAK,EAAE,CAAA;EAElB,OACIC;cACKH,MAAM,CAACxB,GAAG,CAAC,UAAC4B,KAAK,EAAI;AAClB,MAAA,OAAOC,aAAC,CAAAC,cAAc,EAAAC,QAAA,KAAKH,KAAK,EAAA;AAAE1C,QAAAA,GAAG,EAAKuC,EAAE,GAAIG,GAAAA,GAAAA,KAAK,CAAC1C,GAAAA;AAAK,OAAA,EAAI,CAAA;KAClE,CAAA;AACF,GAAA,CAAA,CAAA;AAEX;;ICnBa8C,mBAAmB,GAA0D,SAA7EA,mBAAmBA,CAAA3B,IAAA,EAG3B;AAAA,EAAA,IAFD4B,QAAQ,GAAA5B,IAAA,CAAR4B,QAAQ;IACRzE,MAAM,GAAA6C,IAAA,CAAN7C,MAAM,CAAA;AAEN,EAAA,OAAOmE,GAAA,CAACtE,kBAAkB,CAAC6E,QAAQ,EAAA;AAACjC,IAAAA,KAAK,EAAEzC,MAAM;AAAAyE,IAAAA,QAAA,EAAGA,QAAAA;AAAQ,GAAA,CAA+B,CAAA;AAC/F;;;;"}
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@form-instant/react-resolver-zod",
|
3
|
-
"version": "0.1.
|
3
|
+
"version": "0.1.7",
|
4
4
|
"license": "MIT",
|
5
5
|
"author": {
|
6
6
|
"name": "leomerida15",
|
@@ -53,7 +53,7 @@
|
|
53
53
|
}
|
54
54
|
],
|
55
55
|
"dependencies": {
|
56
|
-
"@form-instant/react-input-mapping": "^0.
|
56
|
+
"@form-instant/react-input-mapping": "^0.1.9",
|
57
57
|
"use-context-selector": "^2.0.0"
|
58
58
|
},
|
59
59
|
"devDependencies": {
|
@@ -78,7 +78,7 @@
|
|
78
78
|
"homepage": "https://leomerida15.github.io/form-instant-react-mapping",
|
79
79
|
"repository": {
|
80
80
|
"type": "git",
|
81
|
-
"url": "git+https://github.com/leomerida15/form-instant-react-
|
81
|
+
"url": "git+https://github.com/leomerida15/form-instant-react-resolver-zod.git"
|
82
82
|
},
|
83
83
|
"keywords": [
|
84
84
|
"@form-instant",
|
package/src/element.tsx
CHANGED
@@ -1,5 +1,5 @@
|
|
1
|
-
import {
|
2
|
-
import {
|
1
|
+
import { ElementMapping } from '@form-instant/react-input-mapping';
|
2
|
+
import { useId } from 'react';
|
3
3
|
import { useFields } from './useSchema';
|
4
4
|
|
5
5
|
export interface ElementProps<Schema extends Record<string, any>> {
|
@@ -7,7 +7,6 @@ export interface ElementProps<Schema extends Record<string, any>> {
|
|
7
7
|
}
|
8
8
|
|
9
9
|
export const FormInstantElement = <S extends Record<string, any>>({ name }: ElementProps<S>) => {
|
10
|
-
const InputMapping = useInputMapping();
|
11
10
|
const field = useFields(name);
|
12
11
|
|
13
12
|
const inputs = Object.values(field.schema!);
|
@@ -17,13 +16,7 @@ export const FormInstantElement = <S extends Record<string, any>>({ name }: Elem
|
|
17
16
|
return (
|
18
17
|
<>
|
19
18
|
{inputs.map((props) => {
|
20
|
-
|
21
|
-
|
22
|
-
const { key, ...prop } = props;
|
23
|
-
|
24
|
-
if (!Element) return null;
|
25
|
-
|
26
|
-
return createElement(Element, { ...prop, key: `${key}-${id}` });
|
19
|
+
return <ElementMapping {...props} key={`${id}-${props.key}`} />;
|
27
20
|
})}
|
28
21
|
</>
|
29
22
|
);
|