@@ -5,38 +5,38 @@ description: Component creates form based on Zod schemas & react-hook-form with
55
66## Preview
77
8- <Preview name = " auto-form" />
8+ <Preview className = " bg-card " name = " auto-form" />
99
1010## Usage
1111
1212``` tsx
13- import { AutoForm } from ' @vitnode/core/components/form/auto-form' ;
14- import { AutoFormCheckbox } from ' @vitnode/core/components/form/fields/checkbox' ;
15- import { AutoFormInput } from ' @vitnode/core/components/form/fields/input' ;
16- import { AutoFormSelect } from ' @vitnode/core/components/form/fields/select' ;
17- import { AutoFormTextarea } from ' @vitnode/core/components/form/fields/textarea' ;
18- import { z } from ' zod' ;
13+ import { AutoForm } from " @vitnode/core/components/form/auto-form" ;
14+ import { AutoFormCheckbox } from " @vitnode/core/components/form/fields/checkbox" ;
15+ import { AutoFormInput } from " @vitnode/core/components/form/fields/input" ;
16+ import { AutoFormSelect } from " @vitnode/core/components/form/fields/select" ;
17+ import { AutoFormTextarea } from " @vitnode/core/components/form/fields/textarea" ;
18+ import { z } from " zod" ;
1919```
2020
2121``` ts
2222const formSchema = z .object ({
23- username: z .string ().min (3 , ' Username must be at least 3 characters' ),
23+ username: z .string ().min (3 , " Username must be at least 3 characters" ),
2424 email: z
25- .email (' Please enter a valid email address' )
25+ .email (" Please enter a valid email address" )
2626 .describe (" We'll use this email to contact you. (from zod schema)" ),
27- user_type: z .enum ([' admin' , ' editor' , ' viewer' ]),
27+ user_type: z .enum ([" admin" , " editor" , " viewer" ]),
2828 accept_terms: z .boolean ().refine (val => val , {
29- message: ' You must accept the terms and conditions' ,
29+ message: " You must accept the terms and conditions" ,
3030 }),
31- description: z .string ().min (10 , ' Description must be at least 10 characters' ),
31+ description: z .string ().min (10 , " Description must be at least 10 characters" ),
3232});
3333```
3434
3535``` tsx
3636<AutoForm
3737 fields = { [
3838 {
39- id: ' username' ,
39+ id: " username" ,
4040 component : props => (
4141 <AutoFormInput
4242 { ... props }
@@ -46,26 +46,26 @@ const formSchema = z.object({
4646 ),
4747 },
4848 {
49- id: ' email' ,
49+ id: " email" ,
5050 component : props => <AutoFormInput { ... props } label = " Email Address" />,
5151 },
5252 {
53- id: ' user_type' ,
53+ id: " user_type" ,
5454 component : props => (
5555 <AutoFormSelect
5656 { ... props }
5757 description = " Select the type of user."
5858 label = " User Type"
5959 labels = { [
60- { value: ' admin' , label: ' Admin' },
61- { value: ' editor' , label: ' Editor' },
62- { value: ' viewer' , label: ' Viewer' },
60+ { value: " admin" , label: " Admin" },
61+ { value: " editor" , label: " Editor" },
62+ { value: " viewer" , label: " Viewer" },
6363 ]}
6464 />
6565 ),
6666 },
6767 {
68- id: ' accept_terms' ,
68+ id: " accept_terms" ,
6969 component : props => (
7070 <AutoFormCheckbox
7171 { ... props }
@@ -74,7 +74,7 @@ const formSchema = z.object({
7474 ),
7575 },
7676 {
77- id: ' description' ,
77+ id: " description" ,
7878 component : props => (
7979 <AutoFormTextarea
8080 { ... props }
@@ -151,7 +151,7 @@ or using the `describe` method in Zod:
151151
152152``` ts
153153const formSchema = z .object ({
154- username: z .string ().describe (' This is the username for your application.' ),
154+ username: z .string ().describe (" This is the username for your application." ),
155155});
156156```
157157
@@ -161,8 +161,8 @@ Set default values for fields using the `default` method:
161161
162162``` ts
163163const formSchema = z .object ({
164- username: z .string ().default (' user123' ),
165- role: z .enum ([' user' , ' admin' ]).default (' user' ),
164+ username: z .string ().default (" user123" ),
165+ role: z .enum ([" user" , " admin" ]).default (" user" ),
166166});
167167```
168168
@@ -179,7 +179,7 @@ const formSchema = z.object({
179179 .string ()
180180 .min (8 )
181181 .refine (val => / [A-Z ] / .test (val ), {
182- message: ' Password must contain at least one uppercase letter' ,
182+ message: " Password must contain at least one uppercase letter" ,
183183 }),
184184});
185185```
@@ -198,7 +198,7 @@ To activate submit button and handle form submission with the `onSubmit` callbac
198198<AutoForm
199199 fields = { [
200200 {
201- id: ' username' ,
201+ id: " username" ,
202202 component : props => (
203203 <AutoFormInput
204204 { ... props }
@@ -232,9 +232,9 @@ The `onSubmit` callback provides access to the React Hook Form instance as a sec
232232 formSchema = { formSchema }
233233 onSubmit = { (values , form ) => {
234234 // Access form methods
235- form .setError (' username' , {
236- type: ' manual' ,
237- message: ' Username already taken' ,
235+ form .setError (" username" , {
236+ type: " manual" ,
237+ message: " Username already taken" ,
238238 });
239239 }}
240240/>
@@ -243,18 +243,18 @@ The `onSubmit` callback provides access to the React Hook Form instance as a sec
243243You can also define the submission handler separately:
244244
245245``` ts
246- import type { AutoFormOnSubmit } from ' @vitnode/core/components/form/auto-form' ;
246+ import type { AutoFormOnSubmit } from " @vitnode/core/components/form/auto-form" ;
247247```
248248
249249``` tsx
250250const onSubmit: AutoFormOnSubmit <typeof formSchema > = async (values , form ) => {
251251 try {
252252 await saveData (values );
253- toast .success (' Form submitted successfully' );
253+ toast .success (" Form submitted successfully" );
254254 } catch (error ) {
255- form .setError (' root' , {
256- type: ' manual' ,
257- message: ' Failed to submit form' ,
255+ form .setError (" root" , {
256+ type: " manual" ,
257+ message: " Failed to submit form" ,
258258 });
259259 }
260260};
@@ -286,17 +286,17 @@ Customize the submit button using the `submitButtonProps`:
286286 // [!code ++]
287287 submitButtonProps = { {
288288 // [!code ++]
289- variant: ' outline' ,
289+ variant: " outline" ,
290290 // [!code ++]
291- size: ' lg ' ,
291+ size: " lg " ,
292292 // [!code ++]
293- children: ' Save Changes' ,
293+ children: " Save Changes" ,
294294 // [!code ++]
295- className: ' w-full mt-4' ,
295+ className: " w-full mt-4" ,
296296 // [!code ++]
297297 }}
298298 onSubmit = { values => {
299- console .log (' Form submitted' , values );
299+ console .log (" Form submitted" , values );
300300 }}
301301/>
302302```
0 commit comments