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 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
- email: { type: "email" },
32
- password: { type: "password", minLength: { value: 8 } },
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
- // for TypeScript
40
- function handleSubmit(data: z.infer<typeof formSchema>) {
41
- const result = formSchema.safeParse(data);
42
-
43
- if (!result.success) throw new Error("Invalid inputs");
44
-
45
- console.log(data);
46
- }
47
-
48
- // for JavaScript
49
- // /**@param {import("zod").infer<typeof formSchema>} data */
50
- // function handleSubmit(data) {
51
- // const result = formSchema.safeParse(data);
52
-
53
- // if (!result.success) throw new Error("Invalid inputs");
54
-
55
- // console.log(data);
56
- // }
57
-
58
- return (
59
- <Form onSubmit={handleSubmit}>
60
- <Form.Input
61
- label="Email"
62
- type="email"
63
- name="email"
64
- placeholder="Enter your email"
65
- />
66
- <Form.Input
67
- label="Password"
68
- type="password"
69
- name="password"
70
- placeholder="Enter your password"
71
- />
72
- <button
73
- className="rounded-md bg-blue-500 py-1 px-3 text-white hover:bg-blue-600"
74
- type="submit"
75
- >
76
- Submit
77
- </button>
78
- </Form>
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
- email: {
90
- type: "email",
91
- customMessage: "A valid email is required",
92
- },
93
- password: {
94
- type: "password",
95
- minLength: {
96
- value: 8,
97
- message: "Password must be at least 8 characters",
98
- },
99
- maxLength: {
100
- value: 16,
101
- message: "Password must be less than 16 characters",
102
- },
103
- },
104
- username: {
105
- type: "string",
106
- regex: {
107
- value: /^[a-z0-9]{3,}$/ /* only lowercase letters and numbers */,
108
- message: "Username must contains lowercase letters and numbers",
109
- },
110
- customMessage: "Username must not be empty",
111
- },
112
- terms: {
113
- type: "boolean",
114
- customMessage: "You must accept the terms to continue",
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
- email: z.string().email(),
127
- name: z.string().min(3, { message: "Required" }),
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
- display: flex;
161
- flex-direction: column;
162
- justify-content: center;
163
- gap: 1rem;
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
- display: flex;
168
- flex-direction: column;
169
- justify-content: center;
169
+ display: flex;
170
+ flex-direction: column;
171
+ justify-content: center;
170
172
  }
171
173
 
172
174
  .form-control .form-field .form-input {
173
- padding: 10px 1rem;
174
- border-radius: 10px;
175
- border-width: 1px;
176
- border-color: rgb(209 213 219);
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
- border-color: rgb(59 130 246);
181
- outline: none;
182
+ border-color: rgb(59 130 246);
183
+ outline: none;
182
184
  }
183
185
 
184
186
  .form-control .form-field-error-text {
185
- color: rgb(220, 38, 38);
186
- font-size: 0.875rem;
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
- color: rgb(220, 38, 38);
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
- email: { type: "email" },
231
- password: { type: "string", minLength: { value: 8 } },
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
- email: { type: "email" },
251
- password: { type: "string" },
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
- console.log(data); // { email: "test@example.com", password: "securePassword" }
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
- email: { type: "email" },
275
- password: { type: "string" },
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
- email: "example@example.com",
282
- password: "",
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
- <Form.Input type="email" name="email" placeholder="Email" />
302
- <Form.Input type="password" name="password" placeholder="Password" />
303
- <button type="submit">Submit</button>
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
- 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
- },
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. 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.
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
- 2. 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.**).
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
- 3. 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.
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!)