formaze 1.0.3 → 1.0.5
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/README.md +167 -25
- package/dist/index.d.ts +63 -1
- package/package.json +2 -2
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) */
|
|
@@ -53,11 +53,13 @@ export function RegistrationForm() {
|
|
|
53
53
|
return (
|
|
54
54
|
<Form schema={formSchema} onSubmit={handleSubmit}>
|
|
55
55
|
<Form.Input
|
|
56
|
-
|
|
57
|
-
|
|
56
|
+
label="Email"
|
|
57
|
+
type="email"
|
|
58
|
+
name="email"
|
|
58
59
|
placeholder="Enter your email"
|
|
59
60
|
/>
|
|
60
61
|
<Form.Input
|
|
62
|
+
label="Password"
|
|
61
63
|
type="password"
|
|
62
64
|
name="password"
|
|
63
65
|
placeholder="Enter your password"
|
|
@@ -116,7 +118,7 @@ import { z } from "zod";
|
|
|
116
118
|
|
|
117
119
|
const formSchema = z.object({
|
|
118
120
|
email: z.string().email(),
|
|
119
|
-
name: z.string().min(3, {message: "Required"})
|
|
121
|
+
name: z.string().min(3, { message: "Required" }),
|
|
120
122
|
});
|
|
121
123
|
```
|
|
122
124
|
|
|
@@ -133,7 +135,7 @@ You can specify the following validation options for each field:
|
|
|
133
135
|
|
|
134
136
|
## Styling
|
|
135
137
|
|
|
136
|
-
-
|
|
138
|
+
- For Tailwind (pre-styled)
|
|
137
139
|
|
|
138
140
|
Follow the [Official Tailwind Docs](https://tailwindcss.com/docs/installation/framework-guides) for initializing project with vite or next.js
|
|
139
141
|
|
|
@@ -143,43 +145,44 @@ You can add your own styles or import the below css (styled with tailwind css) f
|
|
|
143
145
|
import "formaze/dist/style.css";
|
|
144
146
|
```
|
|
145
147
|
|
|
146
|
-
-
|
|
147
|
-
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
|
+
|
|
148
151
|
```css
|
|
149
152
|
.form-control {
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
153
|
+
display: flex;
|
|
154
|
+
flex-direction: column;
|
|
155
|
+
justify-content: center;
|
|
156
|
+
gap: 1rem;
|
|
154
157
|
}
|
|
155
158
|
|
|
156
159
|
.form-control .form-field {
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
+
display: flex;
|
|
161
|
+
flex-direction: column;
|
|
162
|
+
justify-content: center;
|
|
160
163
|
}
|
|
161
164
|
|
|
162
165
|
.form-control .form-field .form-input {
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
166
|
+
padding: 10px 1rem;
|
|
167
|
+
border-radius: 10px;
|
|
168
|
+
border-width: 1px;
|
|
169
|
+
border-color: rgb(209 213 219);
|
|
167
170
|
}
|
|
168
171
|
|
|
169
172
|
.form-control .form-field .form-input:focus {
|
|
170
|
-
|
|
171
|
-
|
|
173
|
+
border-color: rgb(59 130 246);
|
|
174
|
+
outline: none;
|
|
172
175
|
}
|
|
173
176
|
|
|
174
|
-
.form-control .form-field-error-text {
|
|
175
|
-
|
|
177
|
+
.form-control .form-field-error-text {
|
|
178
|
+
color: rgb(220, 38, 38);
|
|
176
179
|
font-size: 0.875rem;
|
|
177
180
|
}
|
|
178
181
|
|
|
179
182
|
/* or */
|
|
180
183
|
|
|
181
|
-
.form-control .form-field-error {
|
|
182
|
-
|
|
184
|
+
.form-control .form-field-error {
|
|
185
|
+
color: rgb(220, 38, 38);
|
|
183
186
|
}
|
|
184
187
|
```
|
|
185
188
|
|
|
@@ -188,8 +191,147 @@ Here are the css classes with default styles, add it into main css file
|
|
|
188
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
|
|
189
192
|
|
|
190
193
|
```tsx
|
|
191
|
-
"use client"
|
|
194
|
+
"use client";
|
|
192
195
|
// your code
|
|
193
196
|
```
|
|
194
197
|
|
|
198
|
+
## Components and Hooks
|
|
199
|
+
|
|
200
|
+
### `createFormValidator`
|
|
201
|
+
|
|
202
|
+
The createFormValidator function returns a Form component with built-in form validation using Zod and React Hook Form. It simplifies the creation of forms that adhere to a Zod schema for type-safe validation.
|
|
203
|
+
|
|
204
|
+
**Returned Form Component Props**:
|
|
205
|
+
|
|
206
|
+
**schema: `T`**
|
|
207
|
+
|
|
208
|
+
- Type: `T` (A Zod schema)
|
|
209
|
+
|
|
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.
|
|
212
|
+
|
|
213
|
+
- Example:
|
|
214
|
+
|
|
215
|
+
```tsx
|
|
216
|
+
const schema = useFormSchema({
|
|
217
|
+
email: { type: "email" },
|
|
218
|
+
password: { type: "string", minLength: { value: 8 } },
|
|
219
|
+
});
|
|
220
|
+
|
|
221
|
+
const Form = createFormValidator<typeof schema>();
|
|
222
|
+
|
|
223
|
+
<Form schema={schema} onSubmit={handleSubmit} />;
|
|
224
|
+
```
|
|
225
|
+
|
|
226
|
+
**onSubmit: `SubmitHandler<z.infer<T>>`**
|
|
227
|
+
|
|
228
|
+
- Type: `SubmitHandler<z.infer<T>>`
|
|
229
|
+
|
|
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>).
|
|
232
|
+
|
|
233
|
+
- Example:
|
|
234
|
+
|
|
235
|
+
```tsx
|
|
236
|
+
const schema = useFormSchema({
|
|
237
|
+
email: { type: "email" },
|
|
238
|
+
password: { type: "string" },
|
|
239
|
+
});
|
|
240
|
+
|
|
241
|
+
const onSubmit = (data: z.infer<typeof schema>) => {
|
|
242
|
+
console.log(data); // { email: "test@example.com", password: "securePassword" }
|
|
243
|
+
};
|
|
244
|
+
|
|
245
|
+
<Form schema={schema} onSubmit={onSubmit} />;
|
|
246
|
+
```
|
|
247
|
+
|
|
248
|
+
**defaultValues?: `DefaultValues<z.infer<T>>`**
|
|
249
|
+
|
|
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.
|
|
254
|
+
|
|
255
|
+
- Example:
|
|
256
|
+
|
|
257
|
+
```tsx
|
|
258
|
+
const schema = useFormSchema({
|
|
259
|
+
email: { type: "email" },
|
|
260
|
+
password: { type: "string" },
|
|
261
|
+
});
|
|
262
|
+
|
|
263
|
+
const defaultValues = {
|
|
264
|
+
email: "example@example.com",
|
|
265
|
+
password: "",
|
|
266
|
+
};
|
|
267
|
+
|
|
268
|
+
<Form schema={schema} defaultValues={defaultValues} onSubmit={onSubmit} />;
|
|
269
|
+
```
|
|
270
|
+
|
|
271
|
+
**children: React.ReactNode**
|
|
272
|
+
|
|
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.
|
|
277
|
+
|
|
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.
|
|
279
|
+
|
|
280
|
+
- Example:
|
|
281
|
+
|
|
282
|
+
```tsx
|
|
283
|
+
<Form schema={schema} onSubmit={onSubmit} defaultValues={defaultValues}>
|
|
284
|
+
<Form.Input name="email" placeholder="Email" />
|
|
285
|
+
<Form.Input type="password" name="password" placeholder="Password" />
|
|
286
|
+
<button type="submit">Submit</button>
|
|
287
|
+
</Form>
|
|
288
|
+
```
|
|
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
|
+
|
|
307
|
+
### `useFormSchema`
|
|
308
|
+
|
|
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.
|
|
310
|
+
|
|
311
|
+
#### Arguments
|
|
312
|
+
|
|
313
|
+
**schemaConfig: `SchemaConfig<T>`**
|
|
314
|
+
|
|
315
|
+
- Type: `SchemaConfig<T>`
|
|
316
|
+
|
|
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.
|
|
319
|
+
|
|
320
|
+
- Example:
|
|
321
|
+
|
|
322
|
+
```tsx
|
|
323
|
+
const schema = useFormSchema({
|
|
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
|
+
},
|
|
332
|
+
});
|
|
333
|
+
```
|
|
334
|
+
|
|
335
|
+
This schema can then be passed to the Form component returned by `createFormValidator`.
|
|
336
|
+
|
|
195
337
|
## (That's it!)
|
package/dist/index.d.ts
CHANGED
|
@@ -10,7 +10,7 @@ interface FormProps<T extends z.ZodSchema>
|
|
|
10
10
|
}
|
|
11
11
|
|
|
12
12
|
interface InputProps<T extends z.ZodSchema>
|
|
13
|
-
extends React.
|
|
13
|
+
extends React.HTMLAttributes<HTMLInputElement> {
|
|
14
14
|
name: keyof z.infer<T>;
|
|
15
15
|
label?: string;
|
|
16
16
|
}
|
|
@@ -98,11 +98,73 @@ 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
|
+
* @param {...any} props - Additional input attributes like `type`, `placeholder`, etc.
|
|
124
|
+
*
|
|
125
|
+
* @example
|
|
126
|
+
* // Create a Zod schema
|
|
127
|
+
* const schema = useFormSchema({
|
|
128
|
+
* email: { type: "email" },
|
|
129
|
+
* password: { type: "string", minLength: { value: 8 } },
|
|
130
|
+
* });
|
|
131
|
+
*
|
|
132
|
+
* // Use the generated Form component
|
|
133
|
+
* const MyForm = createFormValidator<typeof schema>();
|
|
134
|
+
*
|
|
135
|
+
* <MyForm schema={schema} onSubmit={handleSubmit}>
|
|
136
|
+
* <MyForm.Input name="email" label="Email" />
|
|
137
|
+
* <MyForm.Input name="password" label="Password" type="password" />
|
|
138
|
+
* <button type="submit">Submit</button>
|
|
139
|
+
* </MyForm>;
|
|
140
|
+
*/
|
|
141
|
+
|
|
101
142
|
declare const createFormValidator: <T extends z.ZodSchema>() => {
|
|
102
143
|
({ schema, onSubmit, children, className }: FormProps<T>): JSX.Element;
|
|
103
144
|
Input({ name, label, className, ...props }: InputProps<T>): JSX.Element;
|
|
104
145
|
};
|
|
105
146
|
|
|
147
|
+
/**
|
|
148
|
+
* Generates a Zod schema based on the provided configuration.
|
|
149
|
+
* This utility function is useful for creating dynamic form schemas with validation rules.
|
|
150
|
+
*
|
|
151
|
+
* @template T - The shape of the schema config, where T extends a record of field configurations.
|
|
152
|
+
*
|
|
153
|
+
* @param {SchemaConfig<T>} schemaConfig - An object that defines the fields and validation rules for the form.
|
|
154
|
+
* Each key in the object represents a form field, and its value specifies the field's type, optional status, and any additional validation logic.
|
|
155
|
+
*
|
|
156
|
+
* @returns {z.ZodObject<T>} - Returns a Zod schema that can be used for form validation.
|
|
157
|
+
*
|
|
158
|
+
* @example
|
|
159
|
+
* const schema = useFormSchema({
|
|
160
|
+
* email: { type: "email" },
|
|
161
|
+
* password: {
|
|
162
|
+
* type: "string",
|
|
163
|
+
* minLength: { value: 8 },
|
|
164
|
+
* },
|
|
165
|
+
* });
|
|
166
|
+
*/
|
|
167
|
+
|
|
106
168
|
declare const useFormSchema: <
|
|
107
169
|
T extends SchemaConfig<Record<string, FieldConfig>>
|
|
108
170
|
>(
|
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.
|
|
6
|
+
"version": "1.0.5",
|
|
7
7
|
"type": "module",
|
|
8
8
|
"keywords": [
|
|
9
9
|
"react",
|
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
],
|
|
15
15
|
"repository": {
|
|
16
16
|
"type": "git",
|
|
17
|
-
"url": "https://github.com/Its-Samir/formaze"
|
|
17
|
+
"url": "git+https://github.com/Its-Samir/formaze.git"
|
|
18
18
|
},
|
|
19
19
|
"license": "MIT",
|
|
20
20
|
"scripts": {
|