formaze 2.0.3 → 2.1.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/README.md +115 -113
- package/dist/formaze.js +1851 -1572
- package/dist/formaze.umd.cjs +22 -1
- package/dist/style.css +1 -1
- package/dist/vite.svg +1 -0
- package/package.json +5 -3
- package/dist/index.d.ts +0 -201
package/README.md
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
**Latest version has been released.**
|
|
2
|
+
|
|
1
3
|
# Formaze: An Easy Form Builder for React
|
|
2
4
|
|
|
3
5
|
Formaze is a flexible and customizable form builder for React built with `React Hook Form`, `Zod`, and `TailwindCSS`. It provides an easy way to build forms with custom validation schemas and handles complex form validation logic efficiently with proper type-safety.
|
|
@@ -28,55 +30,55 @@ import "formaze/dist/style.css";
|
|
|
28
30
|
|
|
29
31
|
// create the validation schema
|
|
30
32
|
const formSchema = makeFormSchema({
|
|
31
|
-
|
|
32
|
-
|
|
33
|
+
email: { type: "email" },
|
|
34
|
+
password: { type: "password", minLength: { value: 8 } },
|
|
33
35
|
});
|
|
34
36
|
|
|
35
37
|
// create the form component
|
|
36
38
|
const Form = createFormValidator(formSchema);
|
|
37
39
|
|
|
38
40
|
export function MyForm() {
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
41
|
+
// for TypeScript
|
|
42
|
+
function handleSubmit(data: z.infer<typeof formSchema>) {
|
|
43
|
+
const result = formSchema.safeParse(data);
|
|
44
|
+
|
|
45
|
+
if (!result.success) throw new Error("Invalid inputs");
|
|
46
|
+
|
|
47
|
+
console.log(data);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// for JavaScript
|
|
51
|
+
// /**@param {import("zod").infer<typeof formSchema>} data */
|
|
52
|
+
// function handleSubmit(data) {
|
|
53
|
+
// const result = formSchema.safeParse(data);
|
|
54
|
+
|
|
55
|
+
// if (!result.success) throw new Error("Invalid inputs");
|
|
56
|
+
|
|
57
|
+
// console.log(data);
|
|
58
|
+
// }
|
|
59
|
+
|
|
60
|
+
return (
|
|
61
|
+
<Form onSubmit={handleSubmit}>
|
|
62
|
+
<Form.Input
|
|
63
|
+
label="Email"
|
|
64
|
+
type="email"
|
|
65
|
+
name="email"
|
|
66
|
+
placeholder="Enter your email"
|
|
67
|
+
/>
|
|
68
|
+
<Form.Input
|
|
69
|
+
label="Password"
|
|
70
|
+
type="password"
|
|
71
|
+
name="password"
|
|
72
|
+
placeholder="Enter your password"
|
|
73
|
+
/>
|
|
74
|
+
<button
|
|
75
|
+
className="rounded-md bg-blue-500 py-1 px-3 text-white hover:bg-blue-600"
|
|
76
|
+
type="submit"
|
|
77
|
+
>
|
|
78
|
+
Submit
|
|
79
|
+
</button>
|
|
80
|
+
</Form>
|
|
81
|
+
);
|
|
80
82
|
}
|
|
81
83
|
```
|
|
82
84
|
|
|
@@ -86,33 +88,33 @@ The makeFormSchema method allows you to define validation rules for each field i
|
|
|
86
88
|
|
|
87
89
|
```js
|
|
88
90
|
const formSchema = makeFormSchema({
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
91
|
+
email: {
|
|
92
|
+
type: "email",
|
|
93
|
+
customMessage: "A valid email is required",
|
|
94
|
+
},
|
|
95
|
+
password: {
|
|
96
|
+
type: "password",
|
|
97
|
+
minLength: {
|
|
98
|
+
value: 8,
|
|
99
|
+
message: "Password must be at least 8 characters",
|
|
100
|
+
},
|
|
101
|
+
maxLength: {
|
|
102
|
+
value: 16,
|
|
103
|
+
message: "Password must be less than 16 characters",
|
|
104
|
+
},
|
|
105
|
+
},
|
|
106
|
+
username: {
|
|
107
|
+
type: "string",
|
|
108
|
+
regex: {
|
|
109
|
+
value: /^[a-z0-9]{3,}$/ /* only lowercase letters and numbers */,
|
|
110
|
+
message: "Username must contains lowercase letters and numbers",
|
|
111
|
+
},
|
|
112
|
+
customMessage: "Username must not be empty",
|
|
113
|
+
},
|
|
114
|
+
terms: {
|
|
115
|
+
type: "boolean",
|
|
116
|
+
customMessage: "You must accept the terms to continue",
|
|
117
|
+
},
|
|
116
118
|
});
|
|
117
119
|
```
|
|
118
120
|
|
|
@@ -123,8 +125,8 @@ import { z } from "zod";
|
|
|
123
125
|
import { createFormValidator } from "formaze";
|
|
124
126
|
|
|
125
127
|
const formSchema = z.object({
|
|
126
|
-
|
|
127
|
-
|
|
128
|
+
email: z.string().email(),
|
|
129
|
+
name: z.string().min(3, { message: "Required" }),
|
|
128
130
|
});
|
|
129
131
|
|
|
130
132
|
const Form = createFormValidator(formSchema);
|
|
@@ -157,39 +159,37 @@ import "formaze/dist/style.css";
|
|
|
157
159
|
|
|
158
160
|
```css
|
|
159
161
|
.form-control {
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
162
|
+
display: flex;
|
|
163
|
+
flex-direction: column;
|
|
164
|
+
justify-content: center;
|
|
165
|
+
gap: 1rem;
|
|
164
166
|
}
|
|
165
167
|
|
|
166
168
|
.form-control .form-field {
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
169
|
+
display: flex;
|
|
170
|
+
flex-direction: column;
|
|
171
|
+
justify-content: center;
|
|
170
172
|
}
|
|
171
173
|
|
|
172
174
|
.form-control .form-field .form-input {
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
175
|
+
padding: 10px 1rem;
|
|
176
|
+
border-radius: 10px;
|
|
177
|
+
border-width: 1px;
|
|
178
|
+
border-color: rgb(209 213 219);
|
|
177
179
|
}
|
|
178
180
|
|
|
179
181
|
.form-control .form-field .form-input:focus {
|
|
180
|
-
|
|
181
|
-
|
|
182
|
+
border-color: rgb(59 130 246);
|
|
183
|
+
outline: none;
|
|
182
184
|
}
|
|
183
185
|
|
|
184
186
|
.form-control .form-field-error-text {
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
}
|
|
188
|
-
|
|
187
|
+
color: rgb(220, 38, 38);
|
|
188
|
+
font-size: 0.875rem;
|
|
189
189
|
/* or */
|
|
190
190
|
|
|
191
191
|
.form-control .form-field-error {
|
|
192
|
-
|
|
192
|
+
color: rgb(220, 38, 38);
|
|
193
193
|
}
|
|
194
194
|
```
|
|
195
195
|
|
|
@@ -227,8 +227,8 @@ The createFormValidator function accepts an argument which is a type of zod sche
|
|
|
227
227
|
|
|
228
228
|
```tsx
|
|
229
229
|
const schema = makeFormSchema({
|
|
230
|
-
|
|
231
|
-
|
|
230
|
+
email: { type: "email" },
|
|
231
|
+
password: { type: "string", minLength: { value: 8 } },
|
|
232
232
|
});
|
|
233
233
|
|
|
234
234
|
const Form = createFormValidator(schema);
|
|
@@ -247,14 +247,14 @@ const Form = createFormValidator(schema);
|
|
|
247
247
|
|
|
248
248
|
```tsx
|
|
249
249
|
const schema = makeFormSchema({
|
|
250
|
-
|
|
251
|
-
|
|
250
|
+
email: { type: "email" },
|
|
251
|
+
password: { type: "string" },
|
|
252
252
|
});
|
|
253
253
|
|
|
254
254
|
const Form = createFormValidator(schema);
|
|
255
255
|
|
|
256
256
|
const handleSubmit = (data: z.infer<typeof schema>) => {
|
|
257
|
-
|
|
257
|
+
console.log(data); // { email: "test@example.com", password: "securePassword" }
|
|
258
258
|
};
|
|
259
259
|
|
|
260
260
|
<Form onSubmit={handleSubmit} />;
|
|
@@ -271,15 +271,15 @@ const handleSubmit = (data: z.infer<typeof schema>) => {
|
|
|
271
271
|
|
|
272
272
|
```tsx
|
|
273
273
|
const schema = makeFormSchema({
|
|
274
|
-
|
|
275
|
-
|
|
274
|
+
email: { type: "email" },
|
|
275
|
+
password: { type: "string" },
|
|
276
276
|
});
|
|
277
277
|
|
|
278
278
|
const Form = createFormValidator(schema);
|
|
279
279
|
|
|
280
280
|
const defaultValues = {
|
|
281
|
-
|
|
282
|
-
|
|
281
|
+
email: "example@example.com",
|
|
282
|
+
password: "",
|
|
283
283
|
};
|
|
284
284
|
|
|
285
285
|
<Form defaultValues={defaultValues} />;
|
|
@@ -298,9 +298,9 @@ You can include `Form.Input` for each form field, along with any other elements
|
|
|
298
298
|
|
|
299
299
|
```tsx
|
|
300
300
|
<Form onSubmit={onSubmit} defaultValues={defaultValues}>
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
301
|
+
<Form.Input type="email" name="email" placeholder="Email" />
|
|
302
|
+
<Form.Input type="password" name="password" placeholder="Password" />
|
|
303
|
+
<button type="submit">Submit</button>
|
|
304
304
|
</Form>
|
|
305
305
|
```
|
|
306
306
|
|
|
@@ -357,14 +357,14 @@ The makeFormSchema is a function that generates a Zod schema based on the provid
|
|
|
357
357
|
|
|
358
358
|
```tsx
|
|
359
359
|
const schema = makeFormSchema({
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
360
|
+
email: { type: "email" },
|
|
361
|
+
password: {
|
|
362
|
+
type: "string",
|
|
363
|
+
minLength: {
|
|
364
|
+
value: 8,
|
|
365
|
+
message: "Password must be at least 8 characters long",
|
|
366
|
+
},
|
|
367
|
+
},
|
|
368
368
|
});
|
|
369
369
|
```
|
|
370
370
|
|
|
@@ -372,7 +372,9 @@ This schema can then be passed to the Form component returned by `createFormVali
|
|
|
372
372
|
|
|
373
373
|
## Changes
|
|
374
374
|
|
|
375
|
-
1.
|
|
375
|
+
1. **(Latest) So after a long time i updated this package, as React upgraded its version, i was aware that the previous version of this package would give version compatibility errors. But now it is fixed**.
|
|
376
|
+
|
|
377
|
+
2. The `createFormValidator` has been changed and now it is accepting an argument which is a type of zod schema generated through `makeFormSchema` or directly from `Zod` and the rest of the logic will be the same as before.
|
|
376
378
|
|
|
377
379
|
In order to give proper type support for JavaScript projects and to simplify the defining process this change has been made.
|
|
378
380
|
|
|
@@ -388,8 +390,8 @@ const Form = createFormValidator<typeof formSchema>(); ❌
|
|
|
388
390
|
const Form = createFormValidator(formSchema); ✔
|
|
389
391
|
```
|
|
390
392
|
|
|
391
|
-
|
|
393
|
+
3. The Form component generated through `createFormValidator` method is now accepting an optional `mode` prop which you can read [about](#props-of-the-returned-form-component) here, and the `schema` prop of this Form component is now optional (**NOTE: In future version this prop will be deprecated. As of now this prop is no longer needed.**).
|
|
392
394
|
|
|
393
|
-
|
|
395
|
+
4. The name of the `useFormSchema` has been changed to `makeFormSchema`, as per React rules we cannot use hooks outside of a React Component and this naming convention was preventing it from being used outside of a React Component, but now it's been taken into consideration. You can now use it anywhere you want.
|
|
394
396
|
|
|
395
397
|
## (That's it!)
|