formaze 2.1.0 → 2.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/dist/index.d.ts +201 -0
  2. package/package.json +1 -1
@@ -0,0 +1,201 @@
1
+ import { z } from "zod";
2
+ import { DefaultValues, SubmitHandler } from "react-hook-form";
3
+
4
+ export interface FormProps<T extends z.ZodSchema>
5
+ extends React.FormHTMLAttributes<HTMLFormElement> {
6
+ schema?: T;
7
+ onSubmit: SubmitHandler<z.infer<T>>;
8
+ defaultValues?: DefaultValues<z.infer<T>>;
9
+ children: React.ReactNode;
10
+ /** default is onSubmit */
11
+ mode?: "onBlur" | "onChange" | "onSubmit" | "onTouched" | "all";
12
+ }
13
+
14
+ // @ts-ignore
15
+ interface InputProps<T extends z.ZodSchema>
16
+ extends React.InputHTMLAttributes<HTMLInputElement> {
17
+ name: keyof z.infer<T>;
18
+ label?: string;
19
+ }
20
+
21
+ interface FormFieldProps extends React.HTMLAttributes<HTMLDivElement> {
22
+ name: string;
23
+ children: React.ReactNode;
24
+ }
25
+
26
+ type OptionalAndCustomMessageConfig = {
27
+ optional?: boolean;
28
+ customMessage?: string;
29
+ };
30
+
31
+ type StringFieldConfig = {
32
+ type: "string";
33
+ minLength?: { value: number; message?: string };
34
+ maxLength?: { value: number; message?: string };
35
+ regex?: { value: RegExp; message?: string };
36
+ };
37
+
38
+ type EmailFieldConfig = {
39
+ type: "email";
40
+ regex?: { value: RegExp; message?: string };
41
+ };
42
+
43
+ type PasswordFieldConfig = {
44
+ type: "password";
45
+ minLength?: { value: number; message?: string };
46
+ maxLength?: { value: number; message?: string };
47
+ regex?: { value: RegExp; message?: string };
48
+ };
49
+
50
+ type NumberFieldConfig = {
51
+ type: "number";
52
+ min?: { value: number | string | Date; message?: string };
53
+ max?: { value: number | string | Date; message?: string };
54
+ };
55
+
56
+ type DateFieldConfig = {
57
+ type: "date";
58
+ min?: { value: string; message?: string };
59
+ max?: { value: string; message?: string };
60
+ };
61
+
62
+ type BooleanFieldConfig = {
63
+ type: "boolean";
64
+ };
65
+
66
+ type FieldConfig = OptionalAndCustomMessageConfig &
67
+ (
68
+ | StringFieldConfig
69
+ | EmailFieldConfig
70
+ | PasswordFieldConfig
71
+ | NumberFieldConfig
72
+ | DateFieldConfig
73
+ | BooleanFieldConfig
74
+ );
75
+
76
+ type SchemaConfig<T extends Record<string, FieldConfig>> = {
77
+ [K in keyof T]: T[K];
78
+ };
79
+
80
+ type SchemaKeyValuePair<T extends SchemaConfig<Record<string, FieldConfig>>> = {
81
+ [K in keyof T]: T[K]["optional"] extends true
82
+ ? z.ZodOptional<
83
+ T[K]["type"] extends "email" | "password" | "string"
84
+ ? z.ZodString
85
+ : T[K]["type"] extends "number"
86
+ ? z.ZodNumber
87
+ : T[K]["type"] extends "boolean"
88
+ ? z.ZodBoolean
89
+ : T[K]["type"] extends "date"
90
+ ? z.ZodDate
91
+ : z.ZodTypeAny
92
+ >
93
+ : T[K]["type"] extends "string" | "email" | "password"
94
+ ? z.ZodString
95
+ : T[K]["type"] extends "number"
96
+ ? z.ZodNumber
97
+ : T[K]["type"] extends "boolean"
98
+ ? z.ZodBoolean
99
+ : T[K]["type"] extends "date"
100
+ ? z.ZodDate
101
+ : z.ZodTypeAny;
102
+ };
103
+
104
+ /**
105
+ * Generates a Form component with built-in validation using Zod and React Hook Form.
106
+ * The returned Form component also includes an Input component for form fields with automatic schema-based validation.
107
+ *
108
+ * @template T - The Zod schema used to define the structure and validation rules for the form.
109
+ * @param {T} formSchema - Zod schema generated through makeFormSchema or directly from Zod.
110
+ * @returns {{
111
+ * ({schema, children, className, onSubmit, mode, ...props}: FormProps<T>): JSX.Element;
112
+ * Input: ({label, name, className, ...props}: InputProps<T>) => JSX.Element;
113
+ * }} - The generated Form component that supports schema-based form handling and an Input component for each form field.
114
+ *
115
+ * @component Form
116
+ * @param {T} schema - Optional Zod schema defining the form fields and validation rules.
117
+ * @param {SubmitHandler<z.infer<T>>} onSubmit - Function to handle form submission after successful validation.
118
+ * @param {DefaultValues<z.infer<T>>} [defaultValues] - Optional default values to pre-populate the form fields.
119
+ * @param {React.ReactNode} children - Form fields and other JSX components, including Form.Input components.
120
+ * @param {string} [className] - Optional class for the form element.
121
+ * @param {string} mode - Optional mode for when to trigger validation error. Default is onSubmit.
122
+ *
123
+ * @component Input
124
+ * @param {keyof z.infer<T>} name - The name of the form field, matching the key of the schema.
125
+ * @param {string} label - Label for the form input.
126
+ * @param {string} [className] - Optional class for the input element.
127
+ *
128
+ * @example
129
+ * // Create a form schema
130
+ * const schema = makeFormSchema({
131
+ * email: { type: "email" },
132
+ * password: { type: "string", minLength: { value: 8 } },
133
+ * });
134
+ *
135
+ * // Generate the Form component
136
+ * const MyForm = createFormValidator(schema);
137
+ *
138
+ * <MyForm onSubmit={(data) => {}}>
139
+ * <MyForm.Input name="email" label="Email" type="email" />
140
+ * <MyForm.Input name="password" label="Password" type="password" />
141
+ * <button type="submit">Submit</button>
142
+ * </MyForm>;
143
+ */
144
+
145
+ declare const createFormValidator: <T extends z.ZodSchema>(
146
+ formSchema: T
147
+ ) => {
148
+ ({
149
+ schema,
150
+ onSubmit,
151
+ children,
152
+ className,
153
+ ...props
154
+ }: FormProps<T>): JSX.Element;
155
+ Input({ name, label, className, ...props }: InputProps<T>): JSX.Element;
156
+ };
157
+
158
+ /**
159
+ * Generates a Zod schema based on the provided configuration.
160
+ * This utility function is useful for creating dynamic form schemas with validation rules.
161
+ *
162
+ * @template T - The shape of the schema config, where T extends a record of field configurations.
163
+ *
164
+ * @param {SchemaConfig<T>} schemaConfig - An object that defines the fields and validation rules for the form.
165
+ * Each key in the object represents a form field, and its value specifies the field's type, optional status, and any additional validation logic.
166
+ *
167
+ * @returns {z.ZodObject<T>} - Returns a Zod schema that can be used for form validation.
168
+ *
169
+ * @example
170
+ * const schema = makeFormSchema({
171
+ * email: { type: "email" },
172
+ * password: {
173
+ * type: "string",
174
+ * minLength: { value: 8 },
175
+ * },
176
+ * });
177
+ */
178
+
179
+ declare const makeFormSchema: <
180
+ T extends SchemaConfig<Record<string, FieldConfig>>
181
+ >(
182
+ schemaConfig: SchemaConfig<T>
183
+ ) => z.ZodObject<
184
+ SchemaKeyValuePair<T>,
185
+ "strip",
186
+ z.ZodTypeAny,
187
+ {
188
+ [k in keyof z.objectUtil.addQuestionMarks<
189
+ z.baseObjectOutputType<SchemaKeyValuePair<T>>,
190
+ any
191
+ >]: z.objectUtil.addQuestionMarks<
192
+ z.baseObjectOutputType<SchemaKeyValuePair<T>>,
193
+ any
194
+ >[k];
195
+ },
196
+ {
197
+ [k_1 in keyof z.baseObjectInputType<
198
+ SchemaKeyValuePair<T>
199
+ >]: z.baseObjectInputType<SchemaKeyValuePair<T>>[k_1];
200
+ }
201
+ >;
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "description": "Easily build forms with custom validation logic for React",
4
4
  "main": "dist/formaze.js",
5
5
  "types": "dist/index.d.ts",
6
- "version": "2.1.0",
6
+ "version": "2.3.0",
7
7
  "type": "module",
8
8
  "keywords": [
9
9
  "react",