formaze 1.0.4 → 1.0.6

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 (3) hide show
  1. package/README.md +130 -103
  2. package/dist/index.d.ts +61 -0
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -19,7 +19,7 @@ npm install formaze
19
19
  ```tsx
20
20
  // if you are using next.js (app router) then activate the below line "use client" directive otherwise remove it
21
21
 
22
- // "use client"
22
+ // "use client"
23
23
  import { z } from "zod";
24
24
  import { useFormSchema, createFormValidator } from "formaze";
25
25
  /* for pre-styled css (check the styling guide below) */
@@ -27,49 +27,51 @@ import "formaze/dist/style.css";
27
27
 
28
28
  // create the validation schema
29
29
  const formSchema = useFormSchema({
30
- email: {
31
- type: "email",
32
- },
33
- password: {
34
- type: "password",
35
- minLength: {
36
- value: 8, // by default 6
37
- },
30
+ email: {
31
+ type: "email",
32
+ },
33
+ password: {
34
+ type: "password",
35
+ minLength: {
36
+ value: 8, // by default 6
38
37
  },
38
+ },
39
39
  });
40
40
 
41
41
  // create the form
42
42
  const Form = createFormValidator<typeof formSchema>();
43
43
 
44
44
  export function RegistrationForm() {
45
- function handleSubmit(data: z.infer<typeof formSchema>) {
46
- const result = formSchema.safeParse(data);
47
-
48
- if (!result.success) throw new Error("Invalid inputs");
49
-
50
- console.log(data);
51
- }
52
-
53
- return (
54
- <Form schema={formSchema} onSubmit={handleSubmit}>
55
- <Form.Input
56
- type="email"
57
- name="email"
58
- placeholder="Enter your email"
59
- />
60
- <Form.Input
61
- type="password"
62
- name="password"
63
- placeholder="Enter your password"
64
- />
65
- <button
66
- className="rounded-md bg-blue-500 py-1 px-3 text-white hover:bg-blue-600"
67
- type="submit"
68
- >
69
- Submit
70
- </button>
71
- </Form>
72
- );
45
+ function handleSubmit(data: z.infer<typeof formSchema>) {
46
+ const result = formSchema.safeParse(data);
47
+
48
+ if (!result.success) throw new Error("Invalid inputs");
49
+
50
+ console.log(data);
51
+ }
52
+
53
+ return (
54
+ <Form schema={formSchema} onSubmit={handleSubmit}>
55
+ <Form.Input
56
+ label="Email"
57
+ type="email"
58
+ name="email"
59
+ placeholder="Enter your email"
60
+ />
61
+ <Form.Input
62
+ label="Password"
63
+ type="password"
64
+ name="password"
65
+ placeholder="Enter your password"
66
+ />
67
+ <button
68
+ className="rounded-md bg-blue-500 py-1 px-3 text-white hover:bg-blue-600"
69
+ type="submit"
70
+ >
71
+ Submit
72
+ </button>
73
+ </Form>
74
+ );
73
75
  }
74
76
  ```
75
77
 
@@ -79,15 +81,16 @@ The useFormSchema hook allows you to define validation rules for each field in t
79
81
 
80
82
  ```js
81
83
  const formSchema = useFormSchema({
82
- email: {
83
- type: "email", customMessage: "A valid email is required",
84
+ email: {
85
+ type: "email",
86
+ customMessage: "A valid email is required",
84
87
  },
85
88
  password: {
86
89
  type: "password",
87
- minLength: {
88
- value: 8,
90
+ minLength: {
91
+ value: 8,
89
92
  message: "Password must be at least 8 characters",
90
- },
93
+ },
91
94
  maxLength: {
92
95
  value: 16,
93
96
  message: "Password must be less than 16 characters",
@@ -114,8 +117,8 @@ Though, you can directly use `zod` to define schema as well and pass it to the F
114
117
  import { z } from "zod";
115
118
 
116
119
  const formSchema = z.object({
117
- email: z.string().email(),
118
- name: z.string().min(3, {message: "Required"})
120
+ email: z.string().email(),
121
+ name: z.string().min(3, { message: "Required" }),
119
122
  });
120
123
  ```
121
124
 
@@ -132,7 +135,7 @@ You can specify the following validation options for each field:
132
135
 
133
136
  ## Styling
134
137
 
135
- - For Tailwind (pre-styled)
138
+ - For Tailwind (pre-styled)
136
139
 
137
140
  Follow the [Official Tailwind Docs](https://tailwindcss.com/docs/installation/framework-guides) for initializing project with vite or next.js
138
141
 
@@ -142,43 +145,44 @@ You can add your own styles or import the below css (styled with tailwind css) f
142
145
  import "formaze/dist/style.css";
143
146
  ```
144
147
 
145
- - For Pure CSS
146
- Here are the css classes with default styles, add it into main css file
148
+ - For Pure CSS
149
+ Here are the css classes with default styles, add it into main css file
150
+
147
151
  ```css
148
152
  .form-control {
149
- display: flex;
150
- flex-direction: column;
151
- justify-content: center;
152
- gap: 1rem;
153
+ display: flex;
154
+ flex-direction: column;
155
+ justify-content: center;
156
+ gap: 1rem;
153
157
  }
154
158
 
155
159
  .form-control .form-field {
156
- display: flex;
157
- flex-direction: column;
158
- justify-content: center;
160
+ display: flex;
161
+ flex-direction: column;
162
+ justify-content: center;
159
163
  }
160
164
 
161
165
  .form-control .form-field .form-input {
162
- padding: 10px 1rem;
163
- border-radius: 10px;
164
- border-width: 1px;
165
- border-color: rgb(209 213 219);
166
+ padding: 10px 1rem;
167
+ border-radius: 10px;
168
+ border-width: 1px;
169
+ border-color: rgb(209 213 219);
166
170
  }
167
171
 
168
172
  .form-control .form-field .form-input:focus {
169
- border-color: rgb(59 130 246);
170
- outline: none;
173
+ border-color: rgb(59 130 246);
174
+ outline: none;
171
175
  }
172
176
 
173
- .form-control .form-field-error-text {
174
- color: rgb(220, 38, 38);
175
- font-size: 0.875rem;
177
+ .form-control .form-field-error-text {
178
+ color: rgb(220, 38, 38);
179
+ font-size: 0.875rem;
176
180
  }
177
181
 
178
182
  /* or */
179
183
 
180
- .form-control .form-field-error {
181
- color: rgb(220, 38, 38);
184
+ .form-control .form-field-error {
185
+ color: rgb(220, 38, 38);
182
186
  }
183
187
  ```
184
188
 
@@ -187,7 +191,7 @@ Here are the css classes with default styles, add it into main css file
187
191
  You just have to add `"use client"` directive at the top of your file where you are using this form and its related methods
188
192
 
189
193
  ```tsx
190
- "use client"
194
+ "use client";
191
195
  // your code
192
196
  ```
193
197
 
@@ -201,85 +205,105 @@ The createFormValidator function returns a Form component with built-in form val
201
205
 
202
206
  **schema: `T`**
203
207
 
204
- - Type: `T` (A Zod schema)
208
+ - Type: `T` (A Zod schema)
205
209
 
206
- - Description:
207
- The schema prop is a schema used to define the structure and validation rules for the form. This schema determines the expected fields, their types, and any validation logic, such as required fields, minimum/maximum values, regex patterns, etc.
210
+ - Description:
211
+ The schema prop is a schema used to define the structure and validation rules for the form. This schema determines the expected fields, their types, and any validation logic, such as required fields, minimum/maximum values, regex patterns, etc.
208
212
 
209
- - Example:
213
+ - Example:
210
214
 
211
215
  ```tsx
212
216
  const schema = useFormSchema({
213
- email: { type: "email" },
214
- password: { type: "string", minLength: { value: 8 } },
217
+ email: { type: "email" },
218
+ password: { type: "string", minLength: { value: 8 } },
215
219
  });
216
220
 
217
221
  const Form = createFormValidator<typeof schema>();
218
222
 
219
223
  <Form schema={schema} onSubmit={handleSubmit} />;
220
224
  ```
225
+
221
226
  **onSubmit: `SubmitHandler<z.infer<T>>`**
222
227
 
223
- - Type: `SubmitHandler<z.infer<T>>`
228
+ - Type: `SubmitHandler<z.infer<T>>`
224
229
 
225
- - Description:
226
- The onSubmit prop accepts a function that is called when the form is successfully submitted. The function receives the form's validated data as its argument, which is inferred from the useFormSchema (using Zod) schema (z.infer<T>).
230
+ - Description:
231
+ The onSubmit prop accepts a function that is called when the form is successfully submitted. The function receives the form's validated data as its argument, which is inferred from the useFormSchema (using Zod) schema (z.infer<T>).
227
232
 
228
- - Example:
233
+ - Example:
229
234
 
230
235
  ```tsx
231
236
  const schema = useFormSchema({
232
- email: { type: "email" },
233
- password: { type: "string" },
237
+ email: { type: "email" },
238
+ password: { type: "string" },
234
239
  });
235
240
 
236
241
  const onSubmit = (data: z.infer<typeof schema>) => {
237
- console.log(data); // { email: "test@example.com", password: "securePassword" }
242
+ console.log(data); // { email: "test@example.com", password: "securePassword" }
238
243
  };
239
244
 
240
245
  <Form schema={schema} onSubmit={onSubmit} />;
241
246
  ```
242
247
 
243
248
  **defaultValues?: `DefaultValues<z.infer<T>>`**
244
- - Type: `DefaultValues<z.infer<T>>`
245
249
 
246
- - Description:
247
- This optional prop allows you to specify default values for the form fields. The keys should exactly match the fields defined in the useFormSchema/Zod schema, and the values should be of the appropriate type.
250
+ - Type: `DefaultValues<z.infer<T>>`
251
+
252
+ - Description:
253
+ This optional prop allows you to specify default values for the form fields. The keys should exactly match the fields defined in the useFormSchema/Zod schema, and the values should be of the appropriate type.
248
254
 
249
- - Example:
255
+ - Example:
250
256
 
251
257
  ```tsx
252
258
  const schema = useFormSchema({
253
- email: { type: "email" },
254
- password: { type: "string" },
259
+ email: { type: "email" },
260
+ password: { type: "string" },
255
261
  });
256
262
 
257
263
  const defaultValues = {
258
- email: "example@example.com",
259
- password: "",
264
+ email: "example@example.com",
265
+ password: "",
260
266
  };
261
267
 
262
268
  <Form schema={schema} defaultValues={defaultValues} onSubmit={onSubmit} />;
263
269
  ```
264
270
 
265
271
  **children: React.ReactNode**
266
- - Type: `React.ReactNode`
267
272
 
268
- - Description:
269
- The children prop accepts form inputs and other JSX elements to be rendered inside the Form component. It specifically expects components like Form.Input, which is designed to integrate with the form's validation logic. The Form.Input component handles binding form fields to the Zod schema, ensuring that validation rules are properly applied.
273
+ - Type: `React.ReactNode`
274
+
275
+ - Description:
276
+ The children prop accepts form inputs and other JSX elements to be rendered inside the Form component. It specifically expects components like Form.Input, which is designed to integrate with the form's validation logic. The Form.Input component handles binding form fields to the Zod schema, ensuring that validation rules are properly applied.
270
277
 
271
278
  You can include Form.Input for each form field, along with any other elements like buttons or additional form elements. The Form.Input automatically manages validation states based on the schema.
272
279
 
273
- - Example:
280
+ - Example:
274
281
 
275
282
  ```tsx
276
283
  <Form schema={schema} onSubmit={onSubmit} defaultValues={defaultValues}>
277
- <Form.Input name="email" placeholder="Email" />
278
- <Form.Input type="password" name="password" placeholder="Password" />
279
- <button type="submit">Submit</button>
284
+ <Form.Input name="email" placeholder="Email" />
285
+ <Form.Input type="password" name="password" placeholder="Password" />
286
+ <button type="submit">Submit</button>
280
287
  </Form>
281
288
  ```
282
289
 
290
+ **Input component of the generated Form**
291
+
292
+ **Form.Input**
293
+
294
+ The Form.Input component is a form input field that is connected to a Zod-based form schema. It ensures that the field is validated according to the rules defined in the schema.
295
+
296
+ **Props:**
297
+
298
+ - name: `keyof z.infer<T>`
299
+ The name of the form field, which should correspond to one of the keys in the Zod schema used in the form. This connects the input to the form's validation logic.
300
+
301
+ - label: `string` (optional)
302
+ A label for the form field. This is used for displaying a descriptive text for the input field.
303
+
304
+ - ...props: `React.HTMLAttributes<HTMLInputElement>`
305
+ Any additional props that can be passed to an HTML input element. These props allow you to customize the input field, such as adding a placeholder, className, type, etc.
306
+
283
307
  ### `useFormSchema`
284
308
 
285
309
  useFormSchema is a utility function that generates a Zod schema based on the provided configuration. It supports various field types such as string, email, password, number, date, and boolean, along with validation rules like minLength, maxLength, regex, etc.
@@ -288,20 +312,23 @@ useFormSchema is a utility function that generates a Zod schema based on the pro
288
312
 
289
313
  **schemaConfig: `SchemaConfig<T>`**
290
314
 
291
- - Type: `SchemaConfig<T>`
315
+ - Type: `SchemaConfig<T>`
292
316
 
293
- - Description:
294
- The schemaConfig defines the fields and validation rules for the schema. Each field in the config should specify its type (string, email, password, etc.), whether it is optional, and any additional validation logic, such as minLength, maxLength, or regex.
317
+ - Description:
318
+ The schemaConfig defines the fields and validation rules for the schema. Each field in the config should specify its type (string, email, password, etc.), whether it is optional, and any additional validation logic, such as minLength, maxLength, or regex.
295
319
 
296
- - Example:
320
+ - Example:
297
321
 
298
322
  ```tsx
299
323
  const schema = useFormSchema({
300
- email: { type: "email" },
301
- password: {
302
- type: "string",
303
- minLength: { value: 8, message: "Password must be at least 8 characters long" },
304
- },
324
+ email: { type: "email" },
325
+ password: {
326
+ type: "string",
327
+ minLength: {
328
+ value: 8,
329
+ message: "Password must be at least 8 characters long",
330
+ },
331
+ },
305
332
  });
306
333
  ```
307
334
 
package/dist/index.d.ts CHANGED
@@ -98,11 +98,72 @@ type SchemaKeyValuePair<T extends SchemaConfig<Record<string, FieldConfig>>> = {
98
98
  : z.ZodTypeAny;
99
99
  };
100
100
 
101
+ /**
102
+ * Generates a Form component with built-in validation using Zod and React Hook Form.
103
+ * The returned Form component also includes an Input component for form fields with automatic schema-based validation.
104
+ *
105
+ * @template T - The Zod schema used to define the structure and validation rules for the form.
106
+ *
107
+ * @returns {{
108
+ * ({schema, children, className, onSubmit, ...props}: FormProps<T>): JSX.Element;
109
+ * Input: ({label, name, className, ...props}: InputProps<T>) => JSX.Element;
110
+ * }} - The generated Form component that supports schema-based form handling and an Input component for each form field.
111
+ *
112
+ * @component Form
113
+ * @param {T} schema - Zod schema defining the form fields and validation rules.
114
+ * @param {SubmitHandler<z.infer<T>>} onSubmit - Function to handle form submission after successful validation.
115
+ * @param {DefaultValues<z.infer<T>>} [defaultValues] - Optional default values to pre-populate the form fields.
116
+ * @param {React.ReactNode} children - Form fields and other JSX components, including Form.Input components.
117
+ * @param {string} [className] - Optional class for the form element.
118
+ *
119
+ * @component Input
120
+ * @param {keyof z.infer<T>} name - The name of the form field, matching the key of the schema.
121
+ * @param {string} label - Label for the form input.
122
+ * @param {string} [className] - Optional class for the input element.
123
+ *
124
+ * @example
125
+ * // Create a form schema
126
+ * const schema = useFormSchema({
127
+ * email: { type: "email" },
128
+ * password: { type: "string", minLength: { value: 8 } },
129
+ * });
130
+ *
131
+ * // Generate the Form component
132
+ * const MyForm = createFormValidator<typeof schema>();
133
+ *
134
+ * <MyForm schema={schema} onSubmit={(data) => {}}>
135
+ * <MyForm.Input name="email" label="Email" type="email" />
136
+ * <MyForm.Input name="password" label="Password" type="password" />
137
+ * <button type="submit">Submit</button>
138
+ * </MyForm>;
139
+ */
140
+
101
141
  declare const createFormValidator: <T extends z.ZodSchema>() => {
102
142
  ({ schema, onSubmit, children, className }: FormProps<T>): JSX.Element;
103
143
  Input({ name, label, className, ...props }: InputProps<T>): JSX.Element;
104
144
  };
105
145
 
146
+ /**
147
+ * Generates a Zod schema based on the provided configuration.
148
+ * This utility function is useful for creating dynamic form schemas with validation rules.
149
+ *
150
+ * @template T - The shape of the schema config, where T extends a record of field configurations.
151
+ *
152
+ * @param {SchemaConfig<T>} schemaConfig - An object that defines the fields and validation rules for the form.
153
+ * Each key in the object represents a form field, and its value specifies the field's type, optional status, and any additional validation logic.
154
+ *
155
+ * @returns {z.ZodObject<T>} - Returns a Zod schema that can be used for form validation.
156
+ *
157
+ * @example
158
+ * const schema = useFormSchema({
159
+ * email: { type: "email" },
160
+ * password: {
161
+ * type: "string",
162
+ * minLength: { value: 8 },
163
+ * },
164
+ * });
165
+ */
166
+
106
167
  declare const useFormSchema: <
107
168
  T extends SchemaConfig<Record<string, FieldConfig>>
108
169
  >(
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "description": "An easy form validation package for react, built by using React hook form and zod library",
4
4
  "main": "dist/formaze.js",
5
5
  "types": "dist/index.d.ts",
6
- "version": "1.0.4",
6
+ "version": "1.0.6",
7
7
  "type": "module",
8
8
  "keywords": [
9
9
  "react",