formaze 2.2.0 → 2.4.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.
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { z } from "zod";
2
- import { SubmitHandler, DefaultValues } from "react-hook-form";
2
+ import { DefaultValues, SubmitHandler } from "react-hook-form";
3
3
 
4
4
  export interface FormProps<T extends z.ZodSchema>
5
5
  extends React.FormHTMLAttributes<HTMLFormElement> {
@@ -12,58 +12,58 @@ export interface FormProps<T extends z.ZodSchema>
12
12
  }
13
13
 
14
14
  // @ts-ignore
15
- export interface InputProps<T extends z.ZodSchema>
15
+ interface InputProps<T extends z.ZodSchema>
16
16
  extends React.InputHTMLAttributes<HTMLInputElement> {
17
17
  name: keyof z.infer<T>;
18
- label: string;
18
+ label?: string;
19
19
  }
20
20
 
21
- export interface FormFieldProps extends React.HTMLAttributes<HTMLDivElement> {
21
+ interface FormFieldProps extends React.HTMLAttributes<HTMLDivElement> {
22
22
  name: string;
23
23
  children: React.ReactNode;
24
24
  }
25
25
 
26
- export type OptionalAndCustomMessageConfig = {
26
+ type OptionalAndCustomMessageConfig = {
27
27
  optional?: boolean;
28
28
  customMessage?: string;
29
29
  };
30
30
 
31
- export type StringFieldConfig = {
31
+ type StringFieldConfig = {
32
32
  type: "string";
33
33
  minLength?: { value: number; message?: string };
34
34
  maxLength?: { value: number; message?: string };
35
35
  regex?: { value: RegExp; message?: string };
36
36
  };
37
37
 
38
- export type EmailFieldConfig = {
38
+ type EmailFieldConfig = {
39
39
  type: "email";
40
40
  regex?: { value: RegExp; message?: string };
41
41
  };
42
42
 
43
- export type PasswordFieldConfig = {
43
+ type PasswordFieldConfig = {
44
44
  type: "password";
45
45
  minLength?: { value: number; message?: string };
46
46
  maxLength?: { value: number; message?: string };
47
47
  regex?: { value: RegExp; message?: string };
48
48
  };
49
49
 
50
- export type NumberFieldConfig = {
50
+ type NumberFieldConfig = {
51
51
  type: "number";
52
52
  min?: { value: number | string | Date; message?: string };
53
53
  max?: { value: number | string | Date; message?: string };
54
54
  };
55
55
 
56
- export type DateFieldConfig = {
56
+ type DateFieldConfig = {
57
57
  type: "date";
58
58
  min?: { value: string; message?: string };
59
59
  max?: { value: string; message?: string };
60
60
  };
61
61
 
62
- export type BooleanFieldConfig = {
62
+ type BooleanFieldConfig = {
63
63
  type: "boolean";
64
64
  };
65
65
 
66
- export type FieldConfig = OptionalAndCustomMessageConfig &
66
+ type FieldConfig = OptionalAndCustomMessageConfig &
67
67
  (
68
68
  | StringFieldConfig
69
69
  | EmailFieldConfig
@@ -73,13 +73,11 @@ export type FieldConfig = OptionalAndCustomMessageConfig &
73
73
  | BooleanFieldConfig
74
74
  );
75
75
 
76
- export type SchemaConfig<T extends Record<string, FieldConfig>> = {
76
+ type SchemaConfig<T extends Record<string, FieldConfig>> = {
77
77
  [K in keyof T]: T[K];
78
78
  };
79
79
 
80
- export type SchemaKeyValuePair<
81
- T extends SchemaConfig<Record<string, FieldConfig>>
82
- > = {
80
+ type SchemaKeyValuePair<T extends SchemaConfig<Record<string, FieldConfig>>> = {
83
81
  [K in keyof T]: T[K]["optional"] extends true
84
82
  ? z.ZodOptional<
85
83
  T[K]["type"] extends "email" | "password" | "string"
@@ -102,3 +100,102 @@ export type SchemaKeyValuePair<
102
100
  ? z.ZodDate
103
101
  : z.ZodTypeAny;
104
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.2.0",
6
+ "version": "2.4.0",
7
7
  "type": "module",
8
8
  "keywords": [
9
9
  "react",
package/dist/vite.svg DELETED
@@ -1 +0,0 @@
1
- <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="iconify iconify--logos" width="31.88" height="32" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 257"><defs><linearGradient id="IconifyId1813088fe1fbc01fb466" x1="-.828%" x2="57.636%" y1="7.652%" y2="78.411%"><stop offset="0%" stop-color="#41D1FF"></stop><stop offset="100%" stop-color="#BD34FE"></stop></linearGradient><linearGradient id="IconifyId1813088fe1fbc01fb467" x1="43.376%" x2="50.316%" y1="2.242%" y2="89.03%"><stop offset="0%" stop-color="#FFEA83"></stop><stop offset="8.333%" stop-color="#FFDD35"></stop><stop offset="100%" stop-color="#FFA800"></stop></linearGradient></defs><path fill="url(#IconifyId1813088fe1fbc01fb466)" d="M255.153 37.938L134.897 252.976c-2.483 4.44-8.862 4.466-11.382.048L.875 37.958c-2.746-4.814 1.371-10.646 6.827-9.67l120.385 21.517a6.537 6.537 0 0 0 2.322-.004l117.867-21.483c5.438-.991 9.574 4.796 6.877 9.62Z"></path><path fill="url(#IconifyId1813088fe1fbc01fb467)" d="M185.432.063L96.44 17.501a3.268 3.268 0 0 0-2.634 3.014l-5.474 92.456a3.268 3.268 0 0 0 3.997 3.378l24.777-5.718c2.318-.535 4.413 1.507 3.936 3.838l-7.361 36.047c-.495 2.426 1.782 4.5 4.151 3.78l15.304-4.649c2.372-.72 4.652 1.36 4.15 3.788l-11.698 56.621c-.732 3.542 3.979 5.473 5.943 2.437l1.313-2.028l72.516-144.72c1.215-2.423-.88-5.186-3.54-4.672l-25.505 4.922c-2.396.462-4.435-1.77-3.759-4.114l16.646-57.705c.677-2.35-1.37-4.583-3.769-4.113Z"></path></svg>