@saas-ui/forms 2.0.0-next.19 → 2.0.0-next.20

Sign up to get free protection for your applications and to get access to all the features.
@@ -1 +1 @@
1
- {"version":3,"sources":["../../yup/src/yup-resolver.ts","../../yup/src/create-yup-form.ts","../../yup/src/index.ts"],"names":["createYupForm"],"mappings":";AAAA,SAAS,aAAa;AACtB,SAAS,mBAAmB;AAa5B,IAAM,UAAU,CAAC,UAAe;AAdhC;AAeE,OAAI,WAAM,KAAK,SAAX,mBAAiB,MAAM;AACzB,WAAO,MAAM,KAAK,KAAK;AAAA,EACzB;AAEA,UAAQ,MAAM,MAAM;AAAA,IAClB,KAAK;AACH,aAAO;AAAA,IACT,KAAK;AACH,aAAO;AAAA,IACT,KAAK;AACH,aAAO;AAAA,IACT,KAAK;AACH,aAAO;AAAA,IACT,KAAK;AAAA,IACL;AACE,aAAO;AAAA,EACX;AACF;AAEA,IAAM,iBAAiB,CAAC,OAAY,SAAiB;AAlCrD;AAmCE,aAAW,QAAQ,MAAM,OAAO;AAC9B,SAAI,UAAK,YAAL,mBAAc,OAAO;AAAO,aAAO,KAAK,QAAQ,OAAO,IAAI;AAAA,EACjE;AACF;AAQO,IAAM,sBAAsB,CAAC,WAA0C;AAC5E,QAAM,SAAS,CAAC;AAEhB,MAAI,eAAoC,CAAC;AACzC,MAAI,OAAO,SAAS,SAAS;AAE3B,mBAAe,OAAO,UAAU;AAAA,EAClC,OAAO;AACL,mBAAe,OAAO;AAAA,EACxB;AAEA,aAAW,QAAQ,cAAc;AAC/B,UAAM,QAAQ,aAAa,IAAI;AAE/B,UAAM,UAAmB,CAAC;AAC1B,QAAI,MAAM,SAAS,SAAS;AAC1B,cAAQ,MAAM,eAAe,OAAO,KAAK;AACzC,cAAQ,MAAM,eAAe,OAAO,KAAK;AAAA,IAC3C;AAEA,WAAO,KAAK;AAAA,MACV;AAAA,MACA,OAAO,MAAM,KAAK,SAAS;AAAA,MAC3B,MAAM,QAAQ,KAAK;AAAA,MACnB,GAAG;AAAA,IACL,CAAC;AAAA,EACH;AACA,SAAO;AACT;AAEO,IAAM,kBAAkB,CAAC,QAAyB,SAAiB;AACxE,SAAO,MAAM,QAAQ,IAAI;AAC3B;AAEO,IAAM,mBAAqC,CAAC,WAA4B;AAC7E,SAAO;AAAA,IACL,YAAY;AACV,aAAO,oBAAoB,MAAM;AAAA,IACnC;AAAA,IACA,gBAAgB,MAAc;AAC5B,aAAO,oBAAoB,gBAAgB,QAAQ,IAAI,CAAC;AAAA,IAC1D;AAAA,EACF;AACF;;;ACzFA;AAAA,EACE;AAAA,OAKK;AAoCA,IAAM,gBAAgB,CAC3B,YACG;AACH,QAAM,UAAU,WAAW;AAAA,IACzB,UAAU,CAAC,WACT,YAAY,QAAQ,mCAAS,eAAe,mCAAS,eAAe;AAAA,IACtE,eAAe;AAAA,IACf,GAAG;AAAA,EACL,CAAC;AAED,UAAQ,cAAc;AACtB,UAAQ,KAAK;AAEb,SAAO;AACT;;;ACrDA,SAAS,iBAAAA,sBAAqB;AAEvB,IAAM,OAAOA,eAAc","sourcesContent":["import { reach } from 'yup'\nimport { yupResolver } from '@hookform/resolvers/yup'\n\nimport { FieldProps, GetFieldResolver } from '@saas-ui/forms'\nimport { AnyObjectSchema } from './types'\n\nexport { yupResolver }\n\nexport type Options = {\n min?: number\n max?: number\n}\n\n// @TODO get proper typings for the schema fields\nconst getType = (field: any) => {\n if (field.spec.meta?.type) {\n return field.spec.meta.type\n }\n\n switch (field.type) {\n case 'array':\n return 'array'\n case 'object':\n return 'object'\n case 'number':\n return 'number'\n case 'date':\n return 'date'\n case 'string':\n default:\n return 'text'\n }\n}\n\nconst getArrayOption = (field: any, name: string) => {\n for (const test of field.tests) {\n if (test.OPTIONS?.params[name]) return test.OPTIONS.params[name]\n }\n}\n\n/**\n * A helper function to render forms automatically based on a Yup schema\n *\n * @param schema The Yup schema\n * @returns {FieldProps[]}\n */\nexport const getFieldsFromSchema = (schema: AnyObjectSchema): FieldProps[] => {\n const fields = []\n\n let schemaFields: Record<string, any> = {}\n if (schema.type === 'array') {\n /* @ts-ignore this is actually valid */\n schemaFields = schema.innerType.fields\n } else {\n schemaFields = schema.fields\n }\n\n for (const name in schemaFields) {\n const field = schemaFields[name]\n\n const options: Options = {}\n if (field.type === 'array') {\n options.min = getArrayOption(field, 'min')\n options.max = getArrayOption(field, 'max')\n }\n\n fields.push({\n name,\n label: field.spec.label || name,\n type: getType(field),\n ...options,\n })\n }\n return fields\n}\n\nexport const getNestedSchema = (schema: AnyObjectSchema, path: string) => {\n return reach(schema, path) as AnyObjectSchema\n}\n\nexport const yupFieldResolver: GetFieldResolver = (schema: AnyObjectSchema) => {\n return {\n getFields() {\n return getFieldsFromSchema(schema)\n },\n getNestedFields(name: string) {\n return getFieldsFromSchema(getNestedSchema(schema, name))\n },\n }\n}\n","import {\n createForm,\n CreateFormProps,\n FormProps,\n WithFields,\n FieldValues,\n} from '@saas-ui/forms'\nimport { yupFieldResolver, yupResolver } from './yup-resolver'\nimport { InferType } from 'yup'\nimport React from 'react'\nimport { AnyObjectSchema } from './types'\n\ntype ResolverArgs = Parameters<typeof yupResolver>\n\nexport interface CreateYupFormProps<FieldDefs>\n extends CreateFormProps<FieldDefs> {\n schemaOptions?: ResolverArgs[1]\n resolverOptions?: ResolverArgs[2]\n}\n\nexport type YupFormType<\n FieldDefs,\n ExtraProps = object,\n ExtraOverrides = object,\n Type extends 'yup' = 'yup'\n> = (<\n TSchema extends AnyObjectSchema = AnyObjectSchema,\n TFieldValues extends InferType<TSchema> = InferType<TSchema>, // placeholder\n TContext extends object = object\n>(\n props: WithFields<\n FormProps<TFieldValues, TContext, TSchema>,\n FieldDefs,\n ExtraOverrides\n > & {\n ref?: React.ForwardedRef<HTMLFormElement>\n } & ExtraProps\n) => React.ReactElement) & {\n displayName?: string\n id?: 'YupForm'\n}\n\nexport const createYupForm = <FieldDefs>(\n options?: CreateYupFormProps<FieldDefs>\n) => {\n const YupForm = createForm({\n resolver: (schema: any) =>\n yupResolver(schema, options?.schemaOptions, options?.resolverOptions),\n fieldResolver: yupFieldResolver,\n ...options,\n })\n\n YupForm.displayName = 'YupForm'\n YupForm.id = 'YupForm'\n\n return YupForm as YupFormType<FieldDefs>\n}\n","export * from './yup-resolver'\nexport { createYupForm, type YupFormType } from './create-yup-form'\nexport type { AnyObjectSchema } from './types'\nimport { createYupForm } from '@saas-ui/forms/yup'\n\nexport const Form = createYupForm()\n"]}
1
+ {"version":3,"sources":["../../yup/src/yup-resolver.ts","../../yup/src/create-yup-form.ts","../../yup/src/create-yup-step-form.ts","../../yup/src/index.ts"],"names":["Form"],"mappings":";AAAA,SAAS,aAAa;AACtB,SAAS,mBAAmB;AAa5B,IAAM,UAAU,CAAC,UAAe;AAdhC;AAeE,OAAI,WAAM,KAAK,SAAX,mBAAiB,MAAM;AACzB,WAAO,MAAM,KAAK,KAAK;AAAA,EACzB;AAEA,UAAQ,MAAM,MAAM;AAAA,IAClB,KAAK;AACH,aAAO;AAAA,IACT,KAAK;AACH,aAAO;AAAA,IACT,KAAK;AACH,aAAO;AAAA,IACT,KAAK;AACH,aAAO;AAAA,IACT,KAAK;AAAA,IACL;AACE,aAAO;AAAA,EACX;AACF;AAEA,IAAM,iBAAiB,CAAC,OAAY,SAAiB;AAlCrD;AAmCE,aAAW,QAAQ,MAAM,OAAO;AAC9B,SAAI,UAAK,YAAL,mBAAc,OAAO;AAAO,aAAO,KAAK,QAAQ,OAAO,IAAI;AAAA,EACjE;AACF;AAQO,IAAM,sBAAsB,CAAC,WAA0C;AAC5E,QAAM,SAAS,CAAC;AAEhB,MAAI,eAAoC,CAAC;AACzC,MAAI,OAAO,SAAS,SAAS;AAE3B,mBAAe,OAAO,UAAU;AAAA,EAClC,OAAO;AACL,mBAAe,OAAO;AAAA,EACxB;AAEA,aAAW,QAAQ,cAAc;AAC/B,UAAM,QAAQ,aAAa,IAAI;AAE/B,UAAM,UAAmB,CAAC;AAC1B,QAAI,MAAM,SAAS,SAAS;AAC1B,cAAQ,MAAM,eAAe,OAAO,KAAK;AACzC,cAAQ,MAAM,eAAe,OAAO,KAAK;AAAA,IAC3C;AAEA,WAAO,KAAK;AAAA,MACV;AAAA,MACA,OAAO,MAAM,KAAK,SAAS;AAAA,MAC3B,MAAM,QAAQ,KAAK;AAAA,MACnB,GAAG;AAAA,IACL,CAAC;AAAA,EACH;AACA,SAAO;AACT;AAEO,IAAM,kBAAkB,CAAC,QAAyB,SAAiB;AACxE,SAAO,MAAM,QAAQ,IAAI;AAC3B;AAEO,IAAM,mBAAqC,CAAC,WAA4B;AAC7E,SAAO;AAAA,IACL,YAAY;AACV,aAAO,oBAAoB,MAAM;AAAA,IACnC;AAAA,IACA,gBAAgB,MAAc;AAC5B,aAAO,oBAAoB,gBAAgB,QAAQ,IAAI,CAAC;AAAA,IAC1D;AAAA,EACF;AACF;;;ACzFA;AAAA,EACE;AAAA,OAKK;AAoCA,IAAM,gBAAgB,CAC3B,YACG;AACH,QAAM,UAAU,WAAW;AAAA,IACzB,UAAU,CAAC,WACT,YAAY,QAAQ,mCAAS,eAAe,mCAAS,eAAe;AAAA,IACtE,eAAe;AAAA,IACf,GAAG;AAAA,EACL,CAAC;AAED,UAAQ,cAAc;AACtB,UAAQ,KAAK;AAEb,SAAO;AACT;;;ACxDA;AAAA,EAME;AAAA,OAIK;AA4CA,IAAM,oBAAoB,CAC/B,YACG;AACH,QAAM,cAAc,eAAmC;AAAA,IACrD,UAAU,CAAC,WACT,YAAY,QAAQ,mCAAS,eAAe,mCAAS,eAAe;AAAA,IACtE,eAAe;AAAA,IACf,GAAG;AAAA,EACL,CAAC;AAED,cAAY,cAAc;AAC1B,cAAY,KAAK;AAEjB,SAAO;AACT;;;AC9DO,IAAMA,QAAO,cAAc;AAE3B,IAAM,WAAW,kBAAkB","sourcesContent":["import { reach } from 'yup'\nimport { yupResolver } from '@hookform/resolvers/yup'\n\nimport { FieldProps, GetFieldResolver } from '@saas-ui/forms'\nimport { AnyObjectSchema } from './types'\n\nexport { yupResolver }\n\nexport type Options = {\n min?: number\n max?: number\n}\n\n// @TODO get proper typings for the schema fields\nconst getType = (field: any) => {\n if (field.spec.meta?.type) {\n return field.spec.meta.type\n }\n\n switch (field.type) {\n case 'array':\n return 'array'\n case 'object':\n return 'object'\n case 'number':\n return 'number'\n case 'date':\n return 'date'\n case 'string':\n default:\n return 'text'\n }\n}\n\nconst getArrayOption = (field: any, name: string) => {\n for (const test of field.tests) {\n if (test.OPTIONS?.params[name]) return test.OPTIONS.params[name]\n }\n}\n\n/**\n * A helper function to render forms automatically based on a Yup schema\n *\n * @param schema The Yup schema\n * @returns {FieldProps[]}\n */\nexport const getFieldsFromSchema = (schema: AnyObjectSchema): FieldProps[] => {\n const fields = []\n\n let schemaFields: Record<string, any> = {}\n if (schema.type === 'array') {\n /* @ts-ignore this is actually valid */\n schemaFields = schema.innerType.fields\n } else {\n schemaFields = schema.fields\n }\n\n for (const name in schemaFields) {\n const field = schemaFields[name]\n\n const options: Options = {}\n if (field.type === 'array') {\n options.min = getArrayOption(field, 'min')\n options.max = getArrayOption(field, 'max')\n }\n\n fields.push({\n name,\n label: field.spec.label || name,\n type: getType(field),\n ...options,\n })\n }\n return fields\n}\n\nexport const getNestedSchema = (schema: AnyObjectSchema, path: string) => {\n return reach(schema, path) as AnyObjectSchema\n}\n\nexport const yupFieldResolver: GetFieldResolver = (schema: AnyObjectSchema) => {\n return {\n getFields() {\n return getFieldsFromSchema(schema)\n },\n getNestedFields(name: string) {\n return getFieldsFromSchema(getNestedSchema(schema, name))\n },\n }\n}\n","import {\n createForm,\n CreateFormProps,\n FormProps,\n WithFields,\n FieldValues,\n} from '@saas-ui/forms'\nimport { yupFieldResolver, yupResolver } from './yup-resolver'\nimport { InferType } from 'yup'\nimport React from 'react'\nimport { AnyObjectSchema } from './types'\n\ntype ResolverArgs = Parameters<typeof yupResolver>\n\nexport interface CreateYupFormProps<FieldDefs>\n extends CreateFormProps<FieldDefs> {\n schemaOptions?: ResolverArgs[1]\n resolverOptions?: ResolverArgs[2]\n}\n\nexport type YupFormType<\n FieldDefs,\n ExtraProps = object,\n ExtraOverrides = object,\n Type extends 'yup' = 'yup'\n> = (<\n TSchema extends AnyObjectSchema = AnyObjectSchema,\n TFieldValues extends InferType<TSchema> = InferType<TSchema>, // placeholder\n TContext extends object = object\n>(\n props: WithFields<\n FormProps<TFieldValues, TContext, TSchema>,\n FieldDefs,\n ExtraOverrides\n > & {\n ref?: React.ForwardedRef<HTMLFormElement>\n } & ExtraProps\n) => React.ReactElement) & {\n displayName?: string\n id?: 'YupForm'\n}\n\nexport const createYupForm = <FieldDefs>(\n options?: CreateYupFormProps<FieldDefs>\n) => {\n const YupForm = createForm({\n resolver: (schema: any) =>\n yupResolver(schema, options?.schemaOptions, options?.resolverOptions),\n fieldResolver: yupFieldResolver,\n ...options,\n })\n\n YupForm.displayName = 'YupForm'\n YupForm.id = 'YupForm'\n\n return YupForm as YupFormType<FieldDefs>\n}\n","import {\n createForm,\n CreateFormProps,\n FormProps,\n WithFields,\n FieldValues,\n createStepForm,\n StepsOptions,\n UseStepFormProps,\n Form,\n} from '@saas-ui/forms'\nimport { yupFieldResolver, yupResolver } from './yup-resolver'\nimport { InferType, object, string } from 'yup'\nimport React from 'react'\nimport { AnyObjectSchema } from './types'\n\ntype ResolverArgs = Parameters<typeof yupResolver>\n\nexport interface CreateYupFormProps<FieldDefs>\n extends CreateFormProps<FieldDefs> {\n schemaOptions?: ResolverArgs[1]\n resolverOptions?: ResolverArgs[2]\n}\n\ntype InferStepType<T extends Required<StepsOptions<AnyObjectSchema>>> =\n T extends [infer Step, ...infer Rest]\n ? Step extends { schema: AnyObjectSchema }\n ? InferType<Step['schema']> &\n (Rest extends Required<StepsOptions<AnyObjectSchema>>\n ? InferStepType<Rest>\n : object)\n : object\n : object\n\ntype YupStepFormType<\n FieldDefs,\n ExtraProps = object,\n ExtraOverrides = object\n> = (<\n TSteps extends Required<StepsOptions<AnyObjectSchema>> = Required<\n StepsOptions<AnyObjectSchema>\n >,\n TFieldValues extends InferStepType<TSteps> = InferStepType<TSteps>,\n TContext extends object = object\n>(\n props: UseStepFormProps<TSteps, TFieldValues, TContext> & {\n steps: TSteps\n ref?: React.ForwardedRef<HTMLFormElement>\n }\n) => React.ReactElement) & {\n displayName?: string\n id?: string\n}\n\nexport const createYupStepForm = <FieldDefs>(\n options?: CreateYupFormProps<FieldDefs>\n) => {\n const YupStepForm = createStepForm<any, any, any, any>({\n resolver: (schema: any) =>\n yupResolver(schema, options?.schemaOptions, options?.resolverOptions),\n fieldResolver: yupFieldResolver,\n ...options,\n })\n\n YupStepForm.displayName = 'YupStepForm'\n YupStepForm.id = 'YupStepForm'\n\n return YupStepForm as YupStepFormType<FieldDefs>\n}\n","export * from './yup-resolver'\nexport { createYupForm, type YupFormType } from './create-yup-form'\nexport type { AnyObjectSchema } from './types'\nimport { createYupForm } from './create-yup-form'\nimport { createYupStepForm } from './create-yup-step-form'\n\nexport const Form = createYupForm()\n\nexport const StepForm = createYupStepForm()\n"]}
@@ -1,9 +1,392 @@
1
- import { FieldProps, WithFields, FormProps, CreateFormProps } from '@saas-ui/forms';
2
- import * as z from 'zod';
3
- import { z as z$1 } from 'zod';
1
+ import * as React$1 from 'react';
2
+ import React$1__default from 'react';
3
+ import * as react_hook_form from 'react-hook-form';
4
+ import { FieldValues, FieldPath, UseFormProps, SubmitHandler, RegisterOptions, UseFormReturn, UseFieldArrayReturn, WatchObserver, SubmitErrorHandler } from 'react-hook-form';
5
+ import { MaybeRenderProp } from '@chakra-ui/react-utils';
6
+ import { UseStepperProps, UseStepperReturn } from '@saas-ui/core';
7
+ import { HTMLChakraProps, TextareaProps, SwitchProps, CheckboxProps, FormControlProps, ResponsiveValue, InputProps, RadioGroupProps, MenuProps, SelectProps as SelectProps$1, UsePinInputProps, SystemProps, NumberInputProps as NumberInputProps$1, StackDirection, MenuItemOptionProps, ButtonProps, RadioProps } from '@chakra-ui/react';
8
+ import { FocusableElement } from '@chakra-ui/utils';
9
+ import * as _chakra_ui_system_dist_system_types from '@chakra-ui/system/dist/system.types';
10
+ import * as zod from 'zod';
11
+ import { z } from 'zod';
12
+ import { FieldProps as FieldProps$1, WithFields, FormProps as FormProps$1, CreateFormProps } from '@saas-ui/forms';
4
13
  import { zodResolver } from '@hookform/resolvers/zod';
5
14
  export { zodResolver } from '@hookform/resolvers/zod';
6
15
 
16
+ interface NumberInputOptions {
17
+ /**
18
+ * Hide the stepper.
19
+ */
20
+ hideStepper?: boolean;
21
+ /**
22
+ * Render a custom increment icon.
23
+ */
24
+ incrementIcon?: React$1.ReactNode;
25
+ /**
26
+ * Render a custom decrement icon.
27
+ */
28
+ decrementIcon?: React$1.ReactNode;
29
+ }
30
+ interface NumberInputProps extends NumberInputProps$1, NumberInputOptions {
31
+ }
32
+
33
+ interface PasswordOptions {
34
+ viewIcon?: React$1__default.ReactNode;
35
+ viewOffIcon?: React$1__default.ReactNode;
36
+ leftAddon?: React$1__default.ReactNode;
37
+ }
38
+ interface PasswordInputProps extends InputProps, PasswordOptions {
39
+ }
40
+
41
+ interface RadioOption extends Omit<RadioProps, 'value' | 'label'>, FieldOption {
42
+ }
43
+ type RadioOptions = FieldOptions<RadioOption>;
44
+ interface RadioInputOptions {
45
+ options: RadioOptions;
46
+ spacing?: SystemProps['margin'];
47
+ direction?: StackDirection;
48
+ }
49
+ interface RadioInputProps extends Omit<RadioGroupProps, 'children'>, RadioInputOptions {
50
+ }
51
+
52
+ interface SelectOptions {
53
+ /**
54
+ * The name of the input field in a native form.
55
+ */
56
+ name: string;
57
+ /**
58
+ * The value of the select field.
59
+ */
60
+ value?: string | string[];
61
+ /**
62
+ * The initial value of the select field.
63
+ */
64
+ defaultValue?: string | string[];
65
+ /**
66
+ * The callback invoked when the value of the select field changes.
67
+ * @param value The value of the select field.
68
+ */
69
+ onChange?: (value: string | string[]) => void;
70
+ /**
71
+ * The placeholder text when there's no value.
72
+ */
73
+ placeholder?: string;
74
+ /**
75
+ * If `true`, the select will be disabled.
76
+ */
77
+ isDisabled?: boolean;
78
+ /**
79
+ * An array of options
80
+ * If you leave this empty the children prop will be rendered.
81
+ */
82
+ options?: FieldOptions<SelectOption>;
83
+ /**
84
+ * Enable multiple select.
85
+ */
86
+ multiple?: boolean;
87
+ /**
88
+ * The function used to render the value of the select field.
89
+ * @param value The value of the select field.
90
+ * @returns The rendered value.
91
+ */
92
+ renderValue?: (value: string | string[]) => React$1__default.ReactNode;
93
+ }
94
+
95
+ interface SelectProps extends Omit<MenuProps, 'children'>, SelectOptions {
96
+ }
97
+ interface SelectOption extends Omit<MenuItemOptionProps, 'value'>, FieldOption {
98
+ }
99
+ /**
100
+ * An option in a select list
101
+ *
102
+ * @see https://saas-ui.dev/docs/components/forms/select
103
+ */
104
+ declare const SelectOption: _chakra_ui_system_dist_system_types.ComponentWithAs<"button", MenuItemOptionProps>;
105
+
106
+ interface Option$1 {
107
+ value: string;
108
+ label?: string;
109
+ }
110
+ interface NativeSelectOptions {
111
+ options?: Option$1[];
112
+ }
113
+ interface NativeSelectProps extends SelectProps$1, NativeSelectOptions {
114
+ }
115
+
116
+ interface InputFieldProps extends InputProps {
117
+ type?: string;
118
+ leftAddon?: React$1.ReactNode;
119
+ rightAddon?: React$1.ReactNode;
120
+ }
121
+ interface NumberInputFieldProps extends NumberInputProps {
122
+ type: 'number';
123
+ }
124
+ interface PinFieldProps extends Omit<UsePinInputProps, 'type'> {
125
+ pinLength?: number;
126
+ pinType?: 'alphanumeric' | 'number';
127
+ spacing?: SystemProps['margin'];
128
+ }
129
+ declare const defaultFieldTypes: {
130
+ text: React$1.FC<InputFieldProps & BaseFieldProps<react_hook_form.FieldValues, string>>;
131
+ email: React$1.FC<InputFieldProps & BaseFieldProps<react_hook_form.FieldValues, string>>;
132
+ url: React$1.FC<InputFieldProps & BaseFieldProps<react_hook_form.FieldValues, string>>;
133
+ phone: React$1.FC<InputFieldProps & BaseFieldProps<react_hook_form.FieldValues, string>>;
134
+ number: React$1.FC<NumberInputFieldProps & BaseFieldProps<react_hook_form.FieldValues, string>>;
135
+ password: React$1.FC<PasswordInputProps & BaseFieldProps<react_hook_form.FieldValues, string>>;
136
+ textarea: React$1.FC<TextareaProps & BaseFieldProps<react_hook_form.FieldValues, string>>;
137
+ switch: React$1.FC<SwitchProps & BaseFieldProps<react_hook_form.FieldValues, string>>;
138
+ select: React$1.FC<SelectProps & BaseFieldProps<react_hook_form.FieldValues, string>>;
139
+ checkbox: React$1.FC<CheckboxProps & BaseFieldProps<react_hook_form.FieldValues, string>>;
140
+ radio: React$1.FC<RadioInputProps & BaseFieldProps<react_hook_form.FieldValues, string>>;
141
+ pin: React$1.FC<PinFieldProps & BaseFieldProps<react_hook_form.FieldValues, string>>;
142
+ 'native-select': React$1.FC<NativeSelectProps & BaseFieldProps<react_hook_form.FieldValues, string>>;
143
+ };
144
+ type DefaultFields = typeof defaultFieldTypes;
145
+
146
+ interface SubmitButtonProps extends ButtonProps {
147
+ /**
148
+ * Disable the submit button if the form is untouched.
149
+ *
150
+ * Change the default behavior by updating
151
+ * `SubmitButton.defaultProps.disableIfUntouched`
152
+ */
153
+ disableIfUntouched?: boolean;
154
+ /**
155
+ * Disable the submit button if the form is invalid.
156
+ *
157
+ * Change the default behavior by updating
158
+ * `SubmitButton.defaultProps.disableIfInvalid`
159
+ */
160
+ disableIfInvalid?: boolean;
161
+ }
162
+
163
+ type FieldOption = {
164
+ label?: string;
165
+ value: string;
166
+ };
167
+ type FieldOptions<TOption extends FieldOption = FieldOption> = Array<string> | Array<TOption>;
168
+ type ValueOf<T> = T[keyof T];
169
+ type ShallowMerge<A, B> = Omit<A, keyof B> & B;
170
+ type Split<S extends string, D extends string> = string extends S ? string[] : S extends '' ? [] : S extends `${infer T}${D}${infer U}` ? [T, ...Split<U, D>] : [S];
171
+ type MapPath<T extends string[]> = T extends [infer U, ...infer R] ? U extends string ? `${U extends `${number}` ? '$' : U}${R[0] extends string ? '.' : ''}${R extends string[] ? MapPath<R> : ''}` : '' : '';
172
+ type TransformPath<T extends string> = MapPath<Split<T, '.'>>;
173
+ type ArrayFieldPath<Name extends string> = Name extends string ? TransformPath<Name> : never;
174
+ interface BaseFieldProps<TFieldValues extends FieldValues = FieldValues, TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>> extends Omit<FormControlProps, 'label' | 'type'> {
175
+ /**
176
+ * The field name
177
+ */
178
+ name: TName | ArrayFieldPath<TName>;
179
+ /**
180
+ * The field label
181
+ */
182
+ label?: string;
183
+ /**
184
+ * Hide the field label
185
+ */
186
+ hideLabel?: boolean;
187
+ /**
188
+ * Field help text
189
+ */
190
+ help?: string;
191
+ /**
192
+ * React hook form rules
193
+ */
194
+ rules?: Omit<RegisterOptions<TFieldValues, TName>, 'valueAsNumber' | 'valueAsDate' | 'setValueAs' | 'disabled'>;
195
+ /**
196
+ * Build-in types:
197
+ * text, number, password, textarea, select, native-select, checkbox, radio, switch, pin
198
+ *
199
+ * Will default to a text field if there is no matching type.
200
+ */
201
+ type?: string;
202
+ /**
203
+ * The input placeholder
204
+ */
205
+ placeholder?: string;
206
+ }
207
+ type MergeFieldProps<FieldDefs, TFieldValues extends FieldValues = FieldValues, TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>> = ValueOf<{
208
+ [K in keyof FieldDefs]: FieldDefs[K] extends React.FC<infer Props> ? {
209
+ type?: K;
210
+ } & ShallowMerge<Props, BaseFieldProps<TFieldValues, TName>> : never;
211
+ }>;
212
+ type FieldProps<TFieldValues extends FieldValues = FieldValues> = MergeFieldProps<DefaultFields, TFieldValues>;
213
+ type DefaultFieldOverrides = {
214
+ submit?: SubmitButtonProps;
215
+ [key: string]: any;
216
+ };
217
+
218
+ type FieldResolver = {
219
+ getFields(): BaseFieldProps[];
220
+ getNestedFields(name: string): BaseFieldProps[];
221
+ };
222
+
223
+ interface DisplayIfProps<TFieldValues extends FieldValues = FieldValues, TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>> {
224
+ children: React$1.ReactElement;
225
+ name: TName;
226
+ defaultValue?: unknown;
227
+ isDisabled?: boolean;
228
+ isExact?: boolean;
229
+ condition?: (value: unknown, context: UseFormReturn<TFieldValues>) => boolean;
230
+ }
231
+
232
+ interface UseArrayFieldReturn extends UseFieldArrayReturn {
233
+ /**
234
+ * The array field name
235
+ */
236
+ name: string;
237
+ /**
238
+ * The default value for new items
239
+ */
240
+ defaultValue: Record<string, any>;
241
+ /**
242
+ * Min amount of items
243
+ */
244
+ min?: number;
245
+ /**
246
+ * Max amount of items
247
+ */
248
+ max?: number;
249
+ }
250
+ interface ArrayFieldOptions<TFieldValues extends FieldValues = FieldValues, TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>> {
251
+ /**
252
+ * The field name
253
+ */
254
+ name: TName;
255
+ /**
256
+ * Default value for new values in the array
257
+ */
258
+ defaultValue?: Record<string, any>;
259
+ /**
260
+ * Default key name for rows, change this if your data uses a different 'id' field
261
+ * @default "id"
262
+ */
263
+ keyName?: string;
264
+ min?: number;
265
+ max?: number;
266
+ }
267
+ interface ArrayFieldProps<TFieldValues extends FieldValues = FieldValues, TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>> extends ArrayFieldOptions<TFieldValues, TName>, Omit<BaseFieldProps<TFieldValues, TName>, 'name' | 'defaultValue' | 'children'> {
268
+ children: MaybeRenderProp<ArrayField[]>;
269
+ }
270
+ interface ArrayField {
271
+ id: string;
272
+ [key: string]: unknown;
273
+ }
274
+ /**
275
+ * The wrapper component that composes the default ArrayField functionality.
276
+ *
277
+ * @see Docs https://saas-ui.dev/docs/components/forms/array-field
278
+ */
279
+ declare const ArrayField: ((props: ArrayFieldProps & {
280
+ ref?: React$1.ForwardedRef<UseArrayFieldReturn>;
281
+ }) => React$1.ReactElement) & {
282
+ displayName: string;
283
+ };
284
+
285
+ interface ObjectFieldProps<TFieldValues extends FieldValues = FieldValues, TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>> extends BaseFieldProps {
286
+ name: TName;
287
+ children: React$1.ReactNode;
288
+ columns?: ResponsiveValue<number>;
289
+ spacing?: ResponsiveValue<string | number>;
290
+ }
291
+
292
+ interface FormRenderContext<TFieldValues extends FieldValues = FieldValues, TContext extends object = object, TFieldTypes = FieldProps<TFieldValues>> extends UseFormReturn<TFieldValues, TContext> {
293
+ Field: React$1.FC<TFieldTypes & React$1.RefAttributes<FocusableElement>>;
294
+ DisplayIf: React$1.FC<DisplayIfProps<TFieldValues>>;
295
+ ArrayField: React$1.FC<ArrayFieldProps<TFieldValues> & React$1.RefAttributes<UseArrayFieldReturn>>;
296
+ ObjectField: React$1.FC<ObjectFieldProps<TFieldValues>>;
297
+ }
298
+ interface FormOptions<TSchema = unknown, TFieldValues extends FieldValues = FieldValues, TContext extends object = object, TFieldTypes = FieldProps<TFieldValues>> {
299
+ /**
300
+ * The form schema.
301
+ */
302
+ schema?: TSchema;
303
+ /**
304
+ * Triggers when any of the field change.
305
+ */
306
+ onChange?: WatchObserver<TFieldValues>;
307
+ /**
308
+ * The submit handler.
309
+ */
310
+ onSubmit: SubmitHandler<TFieldValues>;
311
+ /**
312
+ * Triggers when there are validation errors.
313
+ */
314
+ onError?: SubmitErrorHandler<TFieldValues>;
315
+ /**
316
+ * The Hook Form state ref.
317
+ */
318
+ formRef?: React$1.RefObject<UseFormReturn<TFieldValues, TContext>>;
319
+ /**
320
+ * The form children, can be a render prop or a ReactNode.
321
+ */
322
+ children?: MaybeRenderProp<FormRenderContext<TFieldValues, TContext, TFieldTypes>>;
323
+ /**
324
+ * The field resolver, used to resolve the fields from schemas.
325
+ */
326
+ fieldResolver?: FieldResolver;
327
+ /**
328
+ * Field overrides
329
+ */
330
+ fields?: DefaultFieldOverrides;
331
+ }
332
+ interface FormProps<TSchema = unknown, TFieldValues extends FieldValues = FieldValues, TContext extends object = object, TFieldTypes = FieldProps<TFieldValues>> extends UseFormProps<TFieldValues, TContext>, Omit<HTMLChakraProps<'form'>, 'children' | 'onChange' | 'onSubmit' | 'onError'>, FormOptions<TSchema, TFieldValues, TContext, TFieldTypes> {
333
+ }
334
+
335
+ type StepsOptions<TSchema, TName extends string = string> = {
336
+ /**
337
+ * The step name
338
+ */
339
+ name: TName;
340
+ /**
341
+ * Schema
342
+ */
343
+ schema?: TSchema;
344
+ }[];
345
+ interface FormStepOptions<TName extends string = string> {
346
+ /**
347
+ * The step name
348
+ */
349
+ name: TName;
350
+ /**
351
+ * Schema
352
+ */
353
+ schema?: any;
354
+ /**
355
+ * Hook Form Resolver
356
+ */
357
+ resolver?: any;
358
+ }
359
+ interface FormStepProps<TName extends string = string> extends FormStepOptions<TName>, Omit<HTMLChakraProps<'div'>, 'onSubmit'> {
360
+ onSubmit?: FormStepSubmitHandler;
361
+ }
362
+ type FormStepSubmitHandler<TFieldValues extends FieldValues = FieldValues> = (data: TFieldValues, stepper: UseStepperReturn) => Promise<void>;
363
+
364
+ type StepName<T extends {
365
+ [k: number]: {
366
+ readonly name: string;
367
+ };
368
+ }> = T[number]['name'];
369
+ interface StepFormRenderContext<TSteps extends StepsOptions<any> = StepsOptions<any>, TFieldValues extends FieldValues = FieldValues, TContext extends object = object, TFieldTypes = FieldProps<TFieldValues>> extends UseStepFormReturn<TFieldValues> {
370
+ Field: React$1.FC<TFieldTypes & React$1.RefAttributes<FocusableElement>>;
371
+ FormStep: React$1.FC<FormStepProps<StepName<TSteps>>>;
372
+ DisplayIf: React$1.FC<DisplayIfProps<TFieldValues>>;
373
+ ArrayField: React$1.FC<ArrayFieldProps<TFieldValues> & React$1.RefAttributes<UseArrayFieldReturn>>;
374
+ ObjectField: React$1.FC<ObjectFieldProps<TFieldValues>>;
375
+ }
376
+ interface UseStepFormProps<TSteps extends StepsOptions<any> = StepsOptions<any>, TFieldValues extends FieldValues = FieldValues, TContext extends object = object, TFieldTypes = FieldProps<TFieldValues>> extends Omit<UseStepperProps, 'onChange'>, Omit<FormProps<any, TFieldValues, TContext, TFieldTypes>, 'children'> {
377
+ steps?: TSteps;
378
+ children: MaybeRenderProp<StepFormRenderContext<TSteps, TFieldValues, TContext, TFieldTypes>>;
379
+ }
380
+ interface UseStepFormReturn<TFieldValues extends FieldValues = FieldValues> extends UseStepperReturn {
381
+ getFormProps(): {
382
+ onSubmit: SubmitHandler<TFieldValues>;
383
+ schema?: any;
384
+ resolver?: any;
385
+ };
386
+ updateStep(step: any): void;
387
+ steps: Record<string, any>;
388
+ }
389
+
7
390
  type Options = {
8
391
  min?: number;
9
392
  max?: number;
@@ -14,11 +397,11 @@ type Options = {
14
397
  * @param schema The Yup schema
15
398
  * @returns {FieldProps[]}
16
399
  */
17
- declare const getFieldsFromSchema: (schema: z.ZodTypeAny) => FieldProps[];
18
- declare const getNestedSchema: (schema: z.ZodTypeAny, path: string) => any;
19
- declare const zodFieldResolver: <T extends z.ZodTypeAny>(schema: T) => {
20
- getFields(): FieldProps[];
21
- getNestedFields(name: string): FieldProps[];
400
+ declare const getFieldsFromSchema: (schema: zod.ZodTypeAny) => FieldProps$1[];
401
+ declare const getNestedSchema: (schema: zod.ZodTypeAny, path: string) => any;
402
+ declare const zodFieldResolver: <T extends zod.ZodTypeAny>(schema: T) => {
403
+ getFields(): FieldProps$1[];
404
+ getNestedFields(name: string): FieldProps$1[];
22
405
  };
23
406
  interface ZodMeta {
24
407
  label: string;
@@ -32,7 +415,7 @@ interface CreateZodFormProps<FieldDefs> extends CreateFormProps<FieldDefs> {
32
415
  schemaOptions?: ResolverArgs[1];
33
416
  resolverOptions?: ResolverArgs[2];
34
417
  }
35
- type ZodFormType<FieldDefs, ExtraProps = object, ExtraOverrides = object, Type extends 'zod' = 'zod'> = (<TSchema extends z$1.AnyZodObject = z$1.AnyZodObject, TFieldValues extends z$1.infer<TSchema> = z$1.infer<TSchema>, TContext extends object = object>(props: WithFields<FormProps<TSchema, TFieldValues, TContext>, FieldDefs, ExtraOverrides> & {
418
+ type ZodFormType<FieldDefs, ExtraProps = object, ExtraOverrides = object, Type extends 'zod' = 'zod'> = (<TSchema extends z.AnyZodObject = z.AnyZodObject, TFieldValues extends z.infer<TSchema> = z.infer<TSchema>, TContext extends object = object>(props: WithFields<FormProps$1<TSchema, TFieldValues, TContext>, FieldDefs, ExtraOverrides> & {
36
419
  ref?: React.ForwardedRef<HTMLFormElement>;
37
420
  } & ExtraProps) => React.ReactElement) & {
38
421
  displayName?: string;
@@ -41,5 +424,128 @@ type ZodFormType<FieldDefs, ExtraProps = object, ExtraOverrides = object, Type e
41
424
  declare const createZodForm: <FieldDefs>(options?: CreateZodFormProps<FieldDefs> | undefined) => ZodFormType<FieldDefs, object, object, "zod">;
42
425
 
43
426
  declare const Form: ZodFormType<unknown, object, object, "zod">;
427
+ declare const StepForm: (<TSteps extends {
428
+ name: string;
429
+ schema?: zod.AnyZodObject | undefined;
430
+ }[] = {
431
+ name: string;
432
+ schema?: zod.AnyZodObject | undefined;
433
+ }[], TFieldValues extends TSteps extends [infer Step, ...infer Rest] ? Step extends {
434
+ schema: zod.AnyZodObject;
435
+ } ? zod.TypeOf<Step["schema"]> & (Rest extends {
436
+ name: string;
437
+ schema?: zod.AnyZodObject | undefined;
438
+ }[] ? Rest extends infer T ? T extends Rest ? T extends [infer Step, ...infer Rest] ? Step extends {
439
+ schema: zod.AnyZodObject;
440
+ } ? zod.TypeOf<Step["schema"]> & (Rest extends {
441
+ name: string;
442
+ schema?: zod.AnyZodObject | undefined;
443
+ }[] ? Rest extends infer T_1 ? T_1 extends Rest ? T_1 extends [infer Step, ...infer Rest] ? Step extends {
444
+ schema: zod.AnyZodObject;
445
+ } ? zod.TypeOf<Step["schema"]> & (Rest extends {
446
+ name: string;
447
+ schema?: zod.AnyZodObject | undefined;
448
+ }[] ? Rest extends infer T_2 ? T_2 extends Rest ? T_2 extends [infer Step, ...infer Rest] ? Step extends {
449
+ schema: zod.AnyZodObject;
450
+ } ? zod.TypeOf<Step["schema"]> & (Rest extends {
451
+ name: string;
452
+ schema?: zod.AnyZodObject | undefined;
453
+ }[] ? Rest extends infer T_3 ? T_3 extends Rest ? T_3 extends [infer Step, ...infer Rest] ? Step extends {
454
+ schema: zod.AnyZodObject;
455
+ } ? zod.TypeOf<Step["schema"]> & (Rest extends {
456
+ name: string;
457
+ schema?: zod.AnyZodObject | undefined;
458
+ }[] ? Rest extends infer T_4 ? T_4 extends Rest ? T_4 extends [infer Step, ...infer Rest] ? Step extends {
459
+ schema: zod.AnyZodObject;
460
+ } ? zod.TypeOf<Step["schema"]> & (Rest extends {
461
+ name: string;
462
+ schema?: zod.AnyZodObject | undefined;
463
+ }[] ? Rest extends infer T_5 ? T_5 extends Rest ? T_5 extends [infer Step, ...infer Rest] ? Step extends {
464
+ schema: zod.AnyZodObject;
465
+ } ? zod.TypeOf<Step["schema"]> & (Rest extends {
466
+ name: string;
467
+ schema?: zod.AnyZodObject | undefined;
468
+ }[] ? Rest extends infer T_6 ? T_6 extends Rest ? T_6 extends [infer Step, ...infer Rest] ? Step extends {
469
+ schema: zod.AnyZodObject;
470
+ } ? zod.TypeOf<Step["schema"]> & (Rest extends {
471
+ name: string;
472
+ schema?: zod.AnyZodObject | undefined;
473
+ }[] ? Rest extends infer T_7 ? T_7 extends Rest ? T_7 extends [infer Step, ...infer Rest] ? Step extends {
474
+ schema: zod.AnyZodObject;
475
+ } ? zod.TypeOf<Step["schema"]> & (Rest extends {
476
+ name: string;
477
+ schema?: zod.AnyZodObject | undefined;
478
+ }[] ? Rest extends infer T_8 ? T_8 extends Rest ? T_8 extends [infer Step, ...infer Rest] ? Step extends {
479
+ schema: zod.AnyZodObject;
480
+ } ? zod.TypeOf<Step["schema"]> & (Rest extends {
481
+ name: string;
482
+ schema?: zod.AnyZodObject | undefined;
483
+ }[] ? Rest extends infer T_9 ? T_9 extends Rest ? T_9 extends [infer Step, ...infer Rest] ? Step extends {
484
+ schema: zod.AnyZodObject;
485
+ } ? zod.TypeOf<Step["schema"]> & (Rest extends {
486
+ name: string;
487
+ schema?: zod.AnyZodObject | undefined;
488
+ }[] ? any : object) : object : object : never : never : object) : object : object : never : never : object) : object : object : never : never : object) : object : object : never : never : object) : object : object : never : never : object) : object : object : never : never : object) : object : object : never : never : object) : object : object : never : never : object) : object : object : never : never : object) : object : object : never : never : object) : object : object = TSteps extends [infer Step, ...infer Rest] ? Step extends {
489
+ schema: zod.AnyZodObject;
490
+ } ? zod.TypeOf<Step["schema"]> & (Rest extends {
491
+ name: string;
492
+ schema?: zod.AnyZodObject | undefined;
493
+ }[] ? Rest extends infer T_10 ? T_10 extends Rest ? T_10 extends [infer Step, ...infer Rest] ? Step extends {
494
+ schema: zod.AnyZodObject;
495
+ } ? zod.TypeOf<Step["schema"]> & (Rest extends {
496
+ name: string;
497
+ schema?: zod.AnyZodObject | undefined;
498
+ }[] ? Rest extends infer T_11 ? T_11 extends Rest ? T_11 extends [infer Step, ...infer Rest] ? Step extends {
499
+ schema: zod.AnyZodObject;
500
+ } ? zod.TypeOf<Step["schema"]> & (Rest extends {
501
+ name: string;
502
+ schema?: zod.AnyZodObject | undefined;
503
+ }[] ? Rest extends infer T_12 ? T_12 extends Rest ? T_12 extends [infer Step, ...infer Rest] ? Step extends {
504
+ schema: zod.AnyZodObject;
505
+ } ? zod.TypeOf<Step["schema"]> & (Rest extends {
506
+ name: string;
507
+ schema?: zod.AnyZodObject | undefined;
508
+ }[] ? Rest extends infer T_13 ? T_13 extends Rest ? T_13 extends [infer Step, ...infer Rest] ? Step extends {
509
+ schema: zod.AnyZodObject;
510
+ } ? zod.TypeOf<Step["schema"]> & (Rest extends {
511
+ name: string;
512
+ schema?: zod.AnyZodObject | undefined;
513
+ }[] ? Rest extends infer T_14 ? T_14 extends Rest ? T_14 extends [infer Step, ...infer Rest] ? Step extends {
514
+ schema: zod.AnyZodObject;
515
+ } ? zod.TypeOf<Step["schema"]> & (Rest extends {
516
+ name: string;
517
+ schema?: zod.AnyZodObject | undefined;
518
+ }[] ? Rest extends infer T_15 ? T_15 extends Rest ? T_15 extends [infer Step, ...infer Rest] ? Step extends {
519
+ schema: zod.AnyZodObject;
520
+ } ? zod.TypeOf<Step["schema"]> & (Rest extends {
521
+ name: string;
522
+ schema?: zod.AnyZodObject | undefined;
523
+ }[] ? Rest extends infer T_16 ? T_16 extends Rest ? T_16 extends [infer Step, ...infer Rest] ? Step extends {
524
+ schema: zod.AnyZodObject;
525
+ } ? zod.TypeOf<Step["schema"]> & (Rest extends {
526
+ name: string;
527
+ schema?: zod.AnyZodObject | undefined;
528
+ }[] ? Rest extends infer T_17 ? T_17 extends Rest ? T_17 extends [infer Step, ...infer Rest] ? Step extends {
529
+ schema: zod.AnyZodObject;
530
+ } ? zod.TypeOf<Step["schema"]> & (Rest extends {
531
+ name: string;
532
+ schema?: zod.AnyZodObject | undefined;
533
+ }[] ? Rest extends infer T_18 ? T_18 extends Rest ? T_18 extends [infer Step, ...infer Rest] ? Step extends {
534
+ schema: zod.AnyZodObject;
535
+ } ? zod.TypeOf<Step["schema"]> & (Rest extends {
536
+ name: string;
537
+ schema?: zod.AnyZodObject | undefined;
538
+ }[] ? Rest extends infer T_19 ? T_19 extends Rest ? T_19 extends [infer Step, ...infer Rest] ? Step extends {
539
+ schema: zod.AnyZodObject;
540
+ } ? zod.TypeOf<Step["schema"]> & (Rest extends {
541
+ name: string;
542
+ schema?: zod.AnyZodObject | undefined;
543
+ }[] ? any : object) : object : object : never : never : object) : object : object : never : never : object) : object : object : never : never : object) : object : object : never : never : object) : object : object : never : never : object) : object : object : never : never : object) : object : object : never : never : object) : object : object : never : never : object) : object : object : never : never : object) : object : object : never : never : object) : object : object, TContext extends object = object>(props: UseStepFormProps<TSteps, TFieldValues, TContext, FieldProps<TFieldValues>> & {
544
+ steps: TSteps;
545
+ ref?: React$1.ForwardedRef<HTMLFormElement> | undefined;
546
+ }) => React$1.ReactElement<any, string | React$1.JSXElementConstructor<any>>) & {
547
+ displayName?: string | undefined;
548
+ id?: string | undefined;
549
+ };
44
550
 
45
- export { Form, Options, ZodFormType, ZodMeta, createZodForm, getFieldsFromSchema, getNestedSchema, zodFieldResolver, zodMeta, zodParseMeta };
551
+ export { Form, Options, StepForm, ZodFormType, ZodMeta, createZodForm, getFieldsFromSchema, getNestedSchema, zodFieldResolver, zodMeta, zodParseMeta };
package/dist/zod/index.js CHANGED
@@ -84,15 +84,27 @@ var createZodForm = (options) => {
84
84
  ZodForm.id = "ZodForm";
85
85
  return ZodForm;
86
86
  };
87
+ var createZodStepForm = (options) => {
88
+ const ZodStepForm = forms.createStepForm({
89
+ resolver: (schema) => zod.zodResolver(schema, options == null ? void 0 : options.schemaOptions, options == null ? void 0 : options.resolverOptions),
90
+ fieldResolver: zodFieldResolver,
91
+ ...options
92
+ });
93
+ ZodStepForm.displayName = "ZodStepForm";
94
+ ZodStepForm.id = "ZodStepForm";
95
+ return ZodStepForm;
96
+ };
87
97
 
88
98
  // zod/src/index.ts
89
99
  var Form = createZodForm();
100
+ var StepForm = createZodStepForm();
90
101
 
91
102
  Object.defineProperty(exports, 'zodResolver', {
92
103
  enumerable: true,
93
104
  get: function () { return zod.zodResolver; }
94
105
  });
95
106
  exports.Form = Form;
107
+ exports.StepForm = StepForm;
96
108
  exports.createZodForm = createZodForm;
97
109
  exports.getFieldsFromSchema = getFieldsFromSchema;
98
110
  exports.getNestedSchema = getNestedSchema;
@@ -1 +1 @@
1
- {"version":3,"sources":["../../zod/src/zod-resolver.ts","../../zod/src/create-zod-form.ts","../../zod/src/index.ts"],"names":[],"mappings":";AACA,SAAS,mBAAmB;AAC5B,SAAS,WAAW;AAUpB,IAAM,UAAU,CAAC,UAAwB;AACvC,UAAQ,MAAM,KAAK,UAAU;AAAA,IAC3B,KAAK;AACH,aAAO;AAAA,IACT,KAAK;AACH,aAAO;AAAA,IACT,KAAK;AACH,aAAO;AAAA,IACT,KAAK;AACH,aAAO;AAAA,IACT,KAAK;AAAA,IACL;AACE,aAAO;AAAA,EACX;AACF;AAEA,IAAM,iBAAiB,CAAC,OAAY,SAAiB;AA5BrD;AA6BE,UAAO,WAAM,KAAK,IAAI,MAAf,mBAAkB;AAC3B;AAQO,IAAM,sBAAsB,CAAC,WAAuC;AACzE,QAAM,SAAuB,CAAC;AAE9B,MAAI,eAAoC,CAAC;AACzC,MAAI,OAAO,KAAK,aAAa,YAAY;AACvC,mBAAe,OAAO,KAAK,KAAK;AAAA,EAClC,WAAW,OAAO,KAAK,aAAa,aAAa;AAC/C,mBAAe,OAAO,KAAK,MAAM;AAAA,EACnC,OAAO;AACL,WAAO;AAAA,EACT;AAEA,aAAW,QAAQ,cAAc;AAC/B,UAAM,QAAQ,aAAa,IAAI;AAE/B,UAAM,UAAmB,CAAC;AAC1B,QAAI,MAAM,KAAK,aAAa,YAAY;AACtC,cAAQ,MAAM,eAAe,OAAO,WAAW;AAC/C,cAAQ,MAAM,eAAe,OAAO,WAAW;AAAA,IACjD;AAEA,UAAM,OAAO,MAAM,eAAe,aAAa,MAAM,WAAW;AAEhE,WAAO,KAAK;AAAA,MACV;AAAA,MACA,QAAO,6BAAM,UAAS,MAAM,eAAe;AAAA,MAC3C,OAAM,6BAAM,SAAQ,QAAQ,KAAK;AAAA,MACjC,GAAG;AAAA,IACL,CAAC;AAAA,EACH;AACA,SAAO;AACT;AAEO,IAAM,kBAAkB,CAAC,QAAsB,SAAiB;AACrE,SAAO,IAAI,OAAO,KAAK,MAAM,GAAG,IAAI;AACtC;AAEO,IAAM,mBAAmB,CAAyB,WAAc;AACrE,SAAO;AAAA,IACL,YAAY;AACV,aAAO,oBAAoB,MAAM;AAAA,IACnC;AAAA,IACA,gBAAgB,MAAc;AAC5B,aAAO,oBAAoB,gBAAgB,QAAQ,IAAI,CAAC;AAAA,IAC1D;AAAA,EACF;AACF;AAOO,IAAM,UAAU,CAAC,SAAkB;AACxC,SAAO,KAAK,UAAU,IAAI;AAC5B;AAEO,IAAM,eAAe,CAAC,SAAiB;AAC5C,MAAI;AACF,WAAO,KAAK,MAAM,IAAI;AAAA,EACxB,SAAS,GAAP;AACA,WAAO;AAAA,EACT;AACF;;;ACrGA;AAAA,EACE;AAAA,OAKK;AAkCA,IAAM,gBAAgB,CAC3B,YACG;AACH,QAAM,UAAU,WAAW;AAAA,IACzB,UAAU,CAAC,WACT,YAAY,QAAQ,mCAAS,eAAe,mCAAS,eAAe;AAAA,IACtE,eAAe;AAAA,IACf,GAAG;AAAA,EACL,CAAC;AAED,UAAQ,cAAc;AACtB,UAAQ,KAAK;AAEb,SAAO;AACT;;;ACzCO,IAAM,OAAO,cAAc","sourcesContent":["import * as z from 'zod'\nimport { zodResolver } from '@hookform/resolvers/zod'\nimport { get } from '@chakra-ui/utils'\nimport { FieldProps } from '@saas-ui/forms'\n\nexport { zodResolver }\n\nexport type Options = {\n min?: number\n max?: number\n}\n\nconst getType = (field: z.ZodTypeAny) => {\n switch (field._def.typeName) {\n case 'ZodArray':\n return 'array'\n case 'ZodObject':\n return 'object'\n case 'ZodNumber':\n return 'number'\n case 'ZodDate':\n return 'date'\n case 'ZodString':\n default:\n return 'text'\n }\n}\n\nconst getArrayOption = (field: any, name: string) => {\n return field._def[name]?.value\n}\n\n/**\n * A helper function to render forms automatically based on a Yup schema\n *\n * @param schema The Yup schema\n * @returns {FieldProps[]}\n */\nexport const getFieldsFromSchema = (schema: z.ZodTypeAny): FieldProps[] => {\n const fields: FieldProps[] = []\n\n let schemaFields: Record<string, any> = {}\n if (schema._def.typeName === 'ZodArray') {\n schemaFields = schema._def.type.shape\n } else if (schema._def.typeName === 'ZodObject') {\n schemaFields = schema._def.shape()\n } else {\n return fields\n }\n\n for (const name in schemaFields) {\n const field = schemaFields[name]\n\n const options: Options = {}\n if (field._def.typeName === 'ZodArray') {\n options.min = getArrayOption(field, 'minLength')\n options.max = getArrayOption(field, 'maxLength')\n }\n\n const meta = field.description && zodParseMeta(field.description)\n\n fields.push({\n name,\n label: meta?.label || field.description || name,\n type: meta?.type || getType(field),\n ...options,\n })\n }\n return fields\n}\n\nexport const getNestedSchema = (schema: z.ZodTypeAny, path: string) => {\n return get(schema._def.shape(), path)\n}\n\nexport const zodFieldResolver = <T extends z.ZodTypeAny>(schema: T) => {\n return {\n getFields() {\n return getFieldsFromSchema(schema)\n },\n getNestedFields(name: string) {\n return getFieldsFromSchema(getNestedSchema(schema, name))\n },\n }\n}\n\nexport interface ZodMeta {\n label: string\n type?: string\n}\n\nexport const zodMeta = (meta: ZodMeta) => {\n return JSON.stringify(meta)\n}\n\nexport const zodParseMeta = (meta: string) => {\n try {\n return JSON.parse(meta)\n } catch (e) {\n return meta\n }\n}\n","import {\n createForm,\n CreateFormProps,\n WithFields,\n FormProps,\n FieldValues,\n} from '@saas-ui/forms'\nimport { zodFieldResolver, zodResolver } from './zod-resolver'\nimport { z } from 'zod'\n\ntype ResolverArgs = Parameters<typeof zodResolver>\n\nexport interface CreateZodFormProps<FieldDefs>\n extends CreateFormProps<FieldDefs> {\n schemaOptions?: ResolverArgs[1]\n resolverOptions?: ResolverArgs[2]\n}\n\nexport type ZodFormType<\n FieldDefs,\n ExtraProps = object,\n ExtraOverrides = object,\n Type extends 'zod' = 'zod'\n> = (<\n TSchema extends z.AnyZodObject = z.AnyZodObject,\n TFieldValues extends z.infer<TSchema> = z.infer<TSchema>,\n TContext extends object = object\n>(\n props: WithFields<\n FormProps<TSchema, TFieldValues, TContext>,\n FieldDefs,\n ExtraOverrides\n > & {\n ref?: React.ForwardedRef<HTMLFormElement>\n } & ExtraProps\n) => React.ReactElement) & {\n displayName?: string\n id?: string\n}\n\nexport const createZodForm = <FieldDefs>(\n options?: CreateZodFormProps<FieldDefs>\n) => {\n const ZodForm = createForm({\n resolver: (schema: any) =>\n zodResolver(schema, options?.schemaOptions, options?.resolverOptions),\n fieldResolver: zodFieldResolver,\n ...options,\n })\n\n ZodForm.displayName = 'ZodForm'\n ZodForm.id = 'ZodForm'\n\n return ZodForm as ZodFormType<FieldDefs>\n}\n","export {\n zodResolver,\n getFieldsFromSchema,\n getNestedSchema,\n zodFieldResolver,\n zodMeta,\n zodParseMeta,\n} from './zod-resolver'\nexport type { Options, ZodMeta } from './zod-resolver'\nexport { createZodForm, type ZodFormType } from './create-zod-form'\n\nimport { createZodForm } from './create-zod-form'\n\nexport const Form = createZodForm()\n"]}
1
+ {"version":3,"sources":["../../zod/src/zod-resolver.ts","../../zod/src/create-zod-form.ts","../../zod/src/create-zod-step-form.ts","../../zod/src/index.ts"],"names":[],"mappings":";AACA,SAAS,mBAAmB;AAC5B,SAAS,WAAW;AAUpB,IAAM,UAAU,CAAC,UAAwB;AACvC,UAAQ,MAAM,KAAK,UAAU;AAAA,IAC3B,KAAK;AACH,aAAO;AAAA,IACT,KAAK;AACH,aAAO;AAAA,IACT,KAAK;AACH,aAAO;AAAA,IACT,KAAK;AACH,aAAO;AAAA,IACT,KAAK;AAAA,IACL;AACE,aAAO;AAAA,EACX;AACF;AAEA,IAAM,iBAAiB,CAAC,OAAY,SAAiB;AA5BrD;AA6BE,UAAO,WAAM,KAAK,IAAI,MAAf,mBAAkB;AAC3B;AAQO,IAAM,sBAAsB,CAAC,WAAuC;AACzE,QAAM,SAAuB,CAAC;AAE9B,MAAI,eAAoC,CAAC;AACzC,MAAI,OAAO,KAAK,aAAa,YAAY;AACvC,mBAAe,OAAO,KAAK,KAAK;AAAA,EAClC,WAAW,OAAO,KAAK,aAAa,aAAa;AAC/C,mBAAe,OAAO,KAAK,MAAM;AAAA,EACnC,OAAO;AACL,WAAO;AAAA,EACT;AAEA,aAAW,QAAQ,cAAc;AAC/B,UAAM,QAAQ,aAAa,IAAI;AAE/B,UAAM,UAAmB,CAAC;AAC1B,QAAI,MAAM,KAAK,aAAa,YAAY;AACtC,cAAQ,MAAM,eAAe,OAAO,WAAW;AAC/C,cAAQ,MAAM,eAAe,OAAO,WAAW;AAAA,IACjD;AAEA,UAAM,OAAO,MAAM,eAAe,aAAa,MAAM,WAAW;AAEhE,WAAO,KAAK;AAAA,MACV;AAAA,MACA,QAAO,6BAAM,UAAS,MAAM,eAAe;AAAA,MAC3C,OAAM,6BAAM,SAAQ,QAAQ,KAAK;AAAA,MACjC,GAAG;AAAA,IACL,CAAC;AAAA,EACH;AACA,SAAO;AACT;AAEO,IAAM,kBAAkB,CAAC,QAAsB,SAAiB;AACrE,SAAO,IAAI,OAAO,KAAK,MAAM,GAAG,IAAI;AACtC;AAEO,IAAM,mBAAmB,CAAyB,WAAc;AACrE,SAAO;AAAA,IACL,YAAY;AACV,aAAO,oBAAoB,MAAM;AAAA,IACnC;AAAA,IACA,gBAAgB,MAAc;AAC5B,aAAO,oBAAoB,gBAAgB,QAAQ,IAAI,CAAC;AAAA,IAC1D;AAAA,EACF;AACF;AAOO,IAAM,UAAU,CAAC,SAAkB;AACxC,SAAO,KAAK,UAAU,IAAI;AAC5B;AAEO,IAAM,eAAe,CAAC,SAAiB;AAC5C,MAAI;AACF,WAAO,KAAK,MAAM,IAAI;AAAA,EACxB,SAAS,GAAP;AACA,WAAO;AAAA,EACT;AACF;;;ACrGA;AAAA,EACE;AAAA,OAKK;AAkCA,IAAM,gBAAgB,CAC3B,YACG;AACH,QAAM,UAAU,WAAW;AAAA,IACzB,UAAU,CAAC,WACT,YAAY,QAAQ,mCAAS,eAAe,mCAAS,eAAe;AAAA,IACtE,eAAe;AAAA,IACf,GAAG;AAAA,EACL,CAAC;AAED,UAAQ,cAAc;AACtB,UAAQ,KAAK;AAEb,SAAO;AACT;;;ACtDA;AAAA,EAEE;AAAA,OAGK;AA6CA,IAAM,oBAAoB,CAC/B,YACG;AACH,QAAM,cAAc,eAAmC;AAAA,IACrD,UAAU,CAAC,WACT,YAAY,QAAQ,mCAAS,eAAe,mCAAS,eAAe;AAAA,IACtE,eAAe;AAAA,IACf,GAAG;AAAA,EACL,CAAC;AAED,cAAY,cAAc;AAC1B,cAAY,KAAK;AAEjB,SAAO;AACT;;;AClDO,IAAM,OAAO,cAAc;AAE3B,IAAM,WAAW,kBAAkB","sourcesContent":["import * as z from 'zod'\nimport { zodResolver } from '@hookform/resolvers/zod'\nimport { get } from '@chakra-ui/utils'\nimport { FieldProps } from '@saas-ui/forms'\n\nexport { zodResolver }\n\nexport type Options = {\n min?: number\n max?: number\n}\n\nconst getType = (field: z.ZodTypeAny) => {\n switch (field._def.typeName) {\n case 'ZodArray':\n return 'array'\n case 'ZodObject':\n return 'object'\n case 'ZodNumber':\n return 'number'\n case 'ZodDate':\n return 'date'\n case 'ZodString':\n default:\n return 'text'\n }\n}\n\nconst getArrayOption = (field: any, name: string) => {\n return field._def[name]?.value\n}\n\n/**\n * A helper function to render forms automatically based on a Yup schema\n *\n * @param schema The Yup schema\n * @returns {FieldProps[]}\n */\nexport const getFieldsFromSchema = (schema: z.ZodTypeAny): FieldProps[] => {\n const fields: FieldProps[] = []\n\n let schemaFields: Record<string, any> = {}\n if (schema._def.typeName === 'ZodArray') {\n schemaFields = schema._def.type.shape\n } else if (schema._def.typeName === 'ZodObject') {\n schemaFields = schema._def.shape()\n } else {\n return fields\n }\n\n for (const name in schemaFields) {\n const field = schemaFields[name]\n\n const options: Options = {}\n if (field._def.typeName === 'ZodArray') {\n options.min = getArrayOption(field, 'minLength')\n options.max = getArrayOption(field, 'maxLength')\n }\n\n const meta = field.description && zodParseMeta(field.description)\n\n fields.push({\n name,\n label: meta?.label || field.description || name,\n type: meta?.type || getType(field),\n ...options,\n })\n }\n return fields\n}\n\nexport const getNestedSchema = (schema: z.ZodTypeAny, path: string) => {\n return get(schema._def.shape(), path)\n}\n\nexport const zodFieldResolver = <T extends z.ZodTypeAny>(schema: T) => {\n return {\n getFields() {\n return getFieldsFromSchema(schema)\n },\n getNestedFields(name: string) {\n return getFieldsFromSchema(getNestedSchema(schema, name))\n },\n }\n}\n\nexport interface ZodMeta {\n label: string\n type?: string\n}\n\nexport const zodMeta = (meta: ZodMeta) => {\n return JSON.stringify(meta)\n}\n\nexport const zodParseMeta = (meta: string) => {\n try {\n return JSON.parse(meta)\n } catch (e) {\n return meta\n }\n}\n","import {\n createForm,\n CreateFormProps,\n WithFields,\n FormProps,\n FieldValues,\n} from '@saas-ui/forms'\nimport { zodFieldResolver, zodResolver } from './zod-resolver'\nimport { z } from 'zod'\n\ntype ResolverArgs = Parameters<typeof zodResolver>\n\nexport interface CreateZodFormProps<FieldDefs>\n extends CreateFormProps<FieldDefs> {\n schemaOptions?: ResolverArgs[1]\n resolverOptions?: ResolverArgs[2]\n}\n\nexport type ZodFormType<\n FieldDefs,\n ExtraProps = object,\n ExtraOverrides = object,\n Type extends 'zod' = 'zod'\n> = (<\n TSchema extends z.AnyZodObject = z.AnyZodObject,\n TFieldValues extends z.infer<TSchema> = z.infer<TSchema>,\n TContext extends object = object\n>(\n props: WithFields<\n FormProps<TSchema, TFieldValues, TContext>,\n FieldDefs,\n ExtraOverrides\n > & {\n ref?: React.ForwardedRef<HTMLFormElement>\n } & ExtraProps\n) => React.ReactElement) & {\n displayName?: string\n id?: string\n}\n\nexport const createZodForm = <FieldDefs>(\n options?: CreateZodFormProps<FieldDefs>\n) => {\n const ZodForm = createForm({\n resolver: (schema: any) =>\n zodResolver(schema, options?.schemaOptions, options?.resolverOptions),\n fieldResolver: zodFieldResolver,\n ...options,\n })\n\n ZodForm.displayName = 'ZodForm'\n ZodForm.id = 'ZodForm'\n\n return ZodForm as ZodFormType<FieldDefs>\n}\n","import {\n CreateFormProps,\n createStepForm,\n StepsOptions,\n UseStepFormProps,\n} from '@saas-ui/forms'\nimport { zodFieldResolver, zodResolver } from './zod-resolver'\nimport { AnyZodObject, z } from 'zod'\nimport React from 'react'\n\ntype ResolverArgs = Parameters<typeof zodResolver>\n\nexport interface CreateZodFormProps<FieldDefs>\n extends CreateFormProps<FieldDefs> {\n schemaOptions?: ResolverArgs[1]\n resolverOptions?: ResolverArgs[2]\n}\n\ntype InferStepType<T extends Required<StepsOptions<AnyZodObject>>> = T extends [\n infer Step,\n ...infer Rest\n]\n ? Step extends { schema: AnyZodObject }\n ? z.infer<Step['schema']> &\n (Rest extends Required<StepsOptions<AnyZodObject>>\n ? InferStepType<Rest>\n : object)\n : object\n : object\n\ntype ZodStepFormType<\n FieldDefs,\n ExtraProps = object,\n ExtraOverrides = object\n> = (<\n TSteps extends Required<StepsOptions<AnyZodObject>> = Required<\n StepsOptions<AnyZodObject>\n >,\n TFieldValues extends InferStepType<TSteps> = InferStepType<TSteps>,\n TContext extends object = object\n>(\n props: UseStepFormProps<TSteps, TFieldValues, TContext> & {\n steps: TSteps\n ref?: React.ForwardedRef<HTMLFormElement>\n }\n) => React.ReactElement) & {\n displayName?: string\n id?: string\n}\n\nexport const createZodStepForm = <FieldDefs>(\n options?: CreateZodFormProps<FieldDefs>\n) => {\n const ZodStepForm = createStepForm<any, any, any, any>({\n resolver: (schema: any) =>\n zodResolver(schema, options?.schemaOptions, options?.resolverOptions),\n fieldResolver: zodFieldResolver,\n ...options,\n })\n\n ZodStepForm.displayName = 'ZodStepForm'\n ZodStepForm.id = 'ZodStepForm'\n\n return ZodStepForm as ZodStepFormType<FieldDefs>\n}\n","export {\n zodResolver,\n getFieldsFromSchema,\n getNestedSchema,\n zodFieldResolver,\n zodMeta,\n zodParseMeta,\n} from './zod-resolver'\nexport type { Options, ZodMeta } from './zod-resolver'\nexport { createZodForm, type ZodFormType } from './create-zod-form'\n\nimport { createZodForm } from './create-zod-form'\nimport { createZodStepForm } from './create-zod-step-form'\n\nexport const Form = createZodForm()\n\nexport const StepForm = createZodStepForm()\n"]}