formaze 1.0.5 → 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.
- package/README.md +100 -100
- package/dist/index.d.ts +5 -6
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -27,51 +27,51 @@ import "formaze/dist/style.css";
|
|
|
27
27
|
|
|
28
28
|
// create the validation schema
|
|
29
29
|
const formSchema = useFormSchema({
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
30
|
+
email: {
|
|
31
|
+
type: "email",
|
|
32
|
+
},
|
|
33
|
+
password: {
|
|
34
|
+
type: "password",
|
|
35
|
+
minLength: {
|
|
36
|
+
value: 8, // by default 6
|
|
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
|
-
|
|
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
|
-
|
|
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
|
+
);
|
|
75
75
|
}
|
|
76
76
|
```
|
|
77
77
|
|
|
@@ -81,33 +81,33 @@ The useFormSchema hook allows you to define validation rules for each field in t
|
|
|
81
81
|
|
|
82
82
|
```js
|
|
83
83
|
const formSchema = useFormSchema({
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
},
|
|
94
|
-
maxLength: {
|
|
95
|
-
value: 16,
|
|
96
|
-
message: "Password must be less than 16 characters",
|
|
97
|
-
},
|
|
98
|
-
},
|
|
99
|
-
username: {
|
|
100
|
-
type: "string",
|
|
101
|
-
maxLength: {
|
|
102
|
-
value: 12,
|
|
103
|
-
message: "Username must be less than 12 characters",
|
|
104
|
-
},
|
|
105
|
-
customMessage: "Username must not be empty",
|
|
106
|
-
},
|
|
107
|
-
terms: {
|
|
108
|
-
type: "boolean",
|
|
109
|
-
customMessage: "You must accept the terms to continue",
|
|
84
|
+
email: {
|
|
85
|
+
type: "email",
|
|
86
|
+
customMessage: "A valid email is required",
|
|
87
|
+
},
|
|
88
|
+
password: {
|
|
89
|
+
type: "password",
|
|
90
|
+
minLength: {
|
|
91
|
+
value: 8,
|
|
92
|
+
message: "Password must be at least 8 characters",
|
|
110
93
|
},
|
|
94
|
+
maxLength: {
|
|
95
|
+
value: 16,
|
|
96
|
+
message: "Password must be less than 16 characters",
|
|
97
|
+
},
|
|
98
|
+
},
|
|
99
|
+
username: {
|
|
100
|
+
type: "string",
|
|
101
|
+
maxLength: {
|
|
102
|
+
value: 12,
|
|
103
|
+
message: "Username must be less than 12 characters",
|
|
104
|
+
},
|
|
105
|
+
customMessage: "Username must not be empty",
|
|
106
|
+
},
|
|
107
|
+
terms: {
|
|
108
|
+
type: "boolean",
|
|
109
|
+
customMessage: "You must accept the terms to continue",
|
|
110
|
+
},
|
|
111
111
|
});
|
|
112
112
|
```
|
|
113
113
|
|
|
@@ -150,39 +150,39 @@ import "formaze/dist/style.css";
|
|
|
150
150
|
|
|
151
151
|
```css
|
|
152
152
|
.form-control {
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
153
|
+
display: flex;
|
|
154
|
+
flex-direction: column;
|
|
155
|
+
justify-content: center;
|
|
156
|
+
gap: 1rem;
|
|
157
157
|
}
|
|
158
158
|
|
|
159
159
|
.form-control .form-field {
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
160
|
+
display: flex;
|
|
161
|
+
flex-direction: column;
|
|
162
|
+
justify-content: center;
|
|
163
163
|
}
|
|
164
164
|
|
|
165
165
|
.form-control .form-field .form-input {
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
166
|
+
padding: 10px 1rem;
|
|
167
|
+
border-radius: 10px;
|
|
168
|
+
border-width: 1px;
|
|
169
|
+
border-color: rgb(209 213 219);
|
|
170
170
|
}
|
|
171
171
|
|
|
172
172
|
.form-control .form-field .form-input:focus {
|
|
173
|
-
|
|
174
|
-
|
|
173
|
+
border-color: rgb(59 130 246);
|
|
174
|
+
outline: none;
|
|
175
175
|
}
|
|
176
176
|
|
|
177
177
|
.form-control .form-field-error-text {
|
|
178
|
-
|
|
179
|
-
|
|
178
|
+
color: rgb(220, 38, 38);
|
|
179
|
+
font-size: 0.875rem;
|
|
180
180
|
}
|
|
181
181
|
|
|
182
182
|
/* or */
|
|
183
183
|
|
|
184
184
|
.form-control .form-field-error {
|
|
185
|
-
|
|
185
|
+
color: rgb(220, 38, 38);
|
|
186
186
|
}
|
|
187
187
|
```
|
|
188
188
|
|
|
@@ -214,8 +214,8 @@ The createFormValidator function returns a Form component with built-in form val
|
|
|
214
214
|
|
|
215
215
|
```tsx
|
|
216
216
|
const schema = useFormSchema({
|
|
217
|
-
|
|
218
|
-
|
|
217
|
+
email: { type: "email" },
|
|
218
|
+
password: { type: "string", minLength: { value: 8 } },
|
|
219
219
|
});
|
|
220
220
|
|
|
221
221
|
const Form = createFormValidator<typeof schema>();
|
|
@@ -234,12 +234,12 @@ const Form = createFormValidator<typeof schema>();
|
|
|
234
234
|
|
|
235
235
|
```tsx
|
|
236
236
|
const schema = useFormSchema({
|
|
237
|
-
|
|
238
|
-
|
|
237
|
+
email: { type: "email" },
|
|
238
|
+
password: { type: "string" },
|
|
239
239
|
});
|
|
240
240
|
|
|
241
241
|
const onSubmit = (data: z.infer<typeof schema>) => {
|
|
242
|
-
|
|
242
|
+
console.log(data); // { email: "test@example.com", password: "securePassword" }
|
|
243
243
|
};
|
|
244
244
|
|
|
245
245
|
<Form schema={schema} onSubmit={onSubmit} />;
|
|
@@ -256,13 +256,13 @@ const onSubmit = (data: z.infer<typeof schema>) => {
|
|
|
256
256
|
|
|
257
257
|
```tsx
|
|
258
258
|
const schema = useFormSchema({
|
|
259
|
-
|
|
260
|
-
|
|
259
|
+
email: { type: "email" },
|
|
260
|
+
password: { type: "string" },
|
|
261
261
|
});
|
|
262
262
|
|
|
263
263
|
const defaultValues = {
|
|
264
|
-
|
|
265
|
-
|
|
264
|
+
email: "example@example.com",
|
|
265
|
+
password: "",
|
|
266
266
|
};
|
|
267
267
|
|
|
268
268
|
<Form schema={schema} defaultValues={defaultValues} onSubmit={onSubmit} />;
|
|
@@ -281,9 +281,9 @@ You can include Form.Input for each form field, along with any other elements li
|
|
|
281
281
|
|
|
282
282
|
```tsx
|
|
283
283
|
<Form schema={schema} onSubmit={onSubmit} defaultValues={defaultValues}>
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
284
|
+
<Form.Input name="email" placeholder="Email" />
|
|
285
|
+
<Form.Input type="password" name="password" placeholder="Password" />
|
|
286
|
+
<button type="submit">Submit</button>
|
|
287
287
|
</Form>
|
|
288
288
|
```
|
|
289
289
|
|
|
@@ -321,14 +321,14 @@ useFormSchema is a utility function that generates a Zod schema based on the pro
|
|
|
321
321
|
|
|
322
322
|
```tsx
|
|
323
323
|
const schema = useFormSchema({
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
},
|
|
324
|
+
email: { type: "email" },
|
|
325
|
+
password: {
|
|
326
|
+
type: "string",
|
|
327
|
+
minLength: {
|
|
328
|
+
value: 8,
|
|
329
|
+
message: "Password must be at least 8 characters long",
|
|
331
330
|
},
|
|
331
|
+
},
|
|
332
332
|
});
|
|
333
333
|
```
|
|
334
334
|
|
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.InputHTMLAttributes<HTMLInputElement> {
|
|
14
14
|
name: keyof z.infer<T>;
|
|
15
15
|
label?: string;
|
|
16
16
|
}
|
|
@@ -120,20 +120,19 @@ type SchemaKeyValuePair<T extends SchemaConfig<Record<string, FieldConfig>>> = {
|
|
|
120
120
|
* @param {keyof z.infer<T>} name - The name of the form field, matching the key of the schema.
|
|
121
121
|
* @param {string} label - Label for the form input.
|
|
122
122
|
* @param {string} [className] - Optional class for the input element.
|
|
123
|
-
* @param {...any} props - Additional input attributes like `type`, `placeholder`, etc.
|
|
124
123
|
*
|
|
125
124
|
* @example
|
|
126
|
-
* // Create a
|
|
125
|
+
* // Create a form schema
|
|
127
126
|
* const schema = useFormSchema({
|
|
128
127
|
* email: { type: "email" },
|
|
129
128
|
* password: { type: "string", minLength: { value: 8 } },
|
|
130
129
|
* });
|
|
131
130
|
*
|
|
132
|
-
* //
|
|
131
|
+
* // Generate the Form component
|
|
133
132
|
* const MyForm = createFormValidator<typeof schema>();
|
|
134
133
|
*
|
|
135
|
-
* <MyForm schema={schema} onSubmit={
|
|
136
|
-
* <MyForm.Input name="email" label="Email" />
|
|
134
|
+
* <MyForm schema={schema} onSubmit={(data) => {}}>
|
|
135
|
+
* <MyForm.Input name="email" label="Email" type="email" />
|
|
137
136
|
* <MyForm.Input name="password" label="Password" type="password" />
|
|
138
137
|
* <button type="submit">Submit</button>
|
|
139
138
|
* </MyForm>;
|
package/package.json
CHANGED