Created
September 11, 2023 01:12
-
-
Save shansmith01/c770f78adefe43401f1696e0b3ac023c to your computer and use it in GitHub Desktop.
Ideal Form
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Form = ({ onSubmit, formSchema, defaultValues, children }) => { | |
const methods = useForm({ defaultValues, resolver: zodResolver(formSchema) }); | |
const { handleSubmit } = methods; | |
return ( | |
<form onSubmit={handleSubmit(onSubmit)}> | |
{children} | |
<input type="submit" /> | |
</form> | |
); | |
}; | |
export const HomePage = () => { | |
const data = { | |
userName: "Shannon", | |
age: 40, | |
} | |
const onSubmit = (data) => { | |
console.log(data); | |
} | |
const formSchema = { | |
username: z.string().min(2, { | |
message: "Username must be at least 2 characters.", | |
}), | |
age: z.number().min(18, { | |
message: "You must be at least 18 years old.", | |
}), | |
} | |
return ( | |
<> | |
<Form onSubmit={onSubmit} formSchema={formSchema} defaultValues={data}> | |
<FormField name="username"> | |
<FormLabel>User Name</FormLabel> | |
<ShortText placeHolder="e.g ShannonSmith42" /> | |
<FormDescription>Enter the users name</FormDescription> | |
<FormMessage/> | |
</FormField> | |
<FormField name="age"> | |
<FormLabel>Age</FormLabel> | |
<Number placeHolder="eg. 3" /> | |
<FormDescription>Your age to the nearest year</FormDescription> | |
<FormMessage/> | |
</FormField> | |
</Form> | |
</> | |
) | |
}; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment