Make useForm compatible with shadcn Form component out of the box #2046
RiadhKHEDHIRI
started this conversation in
Ideas
Replies: 2 comments
-
Would be fantastic, currently I'm struggling with the same |
Beta Was this translation helpful? Give feedback.
0 replies
-
Use import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage } from '@/Components/ui/form';
import { zodResolver } from "@hookform/resolvers/zod";
import { useForm } from "react-hook-form"
import { z } from "zod"
import { router } from '@inertiajs/react'
const formSchema = z.object({
title: z.string().min(5).max(50),
description: z.string().min(5).max(500),
})
export function YourForm() {
const form = useForm<z.infer<typeof formSchema>>({
resolver: zodResolver(formSchema),
defaultValues: {
title: "",
description: "",
},
});
function onSubmit(values: z.infer<typeof formSchema>) {
router.post('/posts', values);
}
return (
<Form {...form}>
<form onSubmit={form.handleSubmit(onSubmit)} >
// your shadcn FormFields
</form>
</Form>
);
} you can also pass the edit: add formater typescript to the block code, mb |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
shadcn/ui
is the most popular UI components collection for React. Its counterpart for Vue and Svelte are:shadcn-vue
shadcn-svelte
They all have the
Form
component to build well-designed HTML forms. Typically, they are wrappers around these libraries to provide composable components for building forms with validation using libraries suchZod
:They expect
useForm
to return some controls and attributes.For more details, here are the direct links to the documentation:
It would be nice to enhance
useForm
to useshadcn
forms out of the box. Thank you 🙏Beta Was this translation helpful? Give feedback.
All reactions