how to properly divide form into components in shadcnui
Unanswered
American Chinchilla posted this in #help-forum
American ChinchillaOP
this is the example form using shadcn and react form hook
would i take out the whole
"use client"
import { zodResolver } from "@hookform/resolvers/zod"
import { useForm } from "react-hook-form"
import { z } from "zod"
import { Button } from "@/components/ui/button"
import {
Form,
FormControl,
FormDescription,
FormField,
FormItem,
FormLabel,
FormMessage,
} from "@/components/ui/form"
import {
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue,
} from "@/components/ui/select"
import { Textarea } from "./ui/textarea"
const FormSchema = z.object({
welcomeChannel: z.string(),
welcomeMessage: z.string(),
})
export default function WelcomeForm() {
const form = useForm<z.infer<typeof FormSchema>>({
resolver: zodResolver(FormSchema),
})
function onSubmit(data: z.infer<typeof FormSchema>) {
console.log(data)
}
return (
<Form {...form}>
<form onSubmit={form.handleSubmit(onSubmit)}>
<FormField
control={form.control}
name="welcomeChannel"
render={({ field }) => (
<FormItem>
<FormLabel>Welcome channel</FormLabel>
<Select onValueChange={field.onChange} defaultValue={field.value}>
<FormControl>
<SelectTrigger>
<SelectValue placeholder="Select a channel" />
</SelectTrigger>
</FormControl>
<SelectContent>
<SelectItem value="123">Channel 1</SelectItem>
<SelectItem value="234">Channel 2</SelectItem>
<SelectItem value="345">Channel 3</SelectItem>
</SelectContent>
</Select>
<FormDescription>
The channel where the welcome message will be sent
</FormDescription>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="welcomeMessage"
render={({ field }) => (
<FormItem>
<FormLabel>Welcome message</FormLabel>
<FormControl>
<Textarea
placeholder="Message"
className="resize-none"
{...field}
/>
</FormControl>
<FormDescription>
The message that will be sent to the welcome channel
</FormDescription>
<FormMessage />
</FormItem>
)}
/>
<Button type="submit">Save</Button>
</form>
</Form>
)
} and for example i would like to take out the Select part to another file so i can reuse itwould i take out the whole
FormField tag and pass form as props so i can set the control={form.control} or would i rather take out the FormItem and pass in as props the field given in render1 Reply
American ChinchillaOP
"use client"
import {
FormControl,
FormDescription,
FormItem,
FormLabel,
FormMessage,
} from "@/components/ui/form"
import {
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue,
} from "@/components/ui/select"
export default function ChannelSelect({
field,
}: {
field: any
}) {
return (
<FormItem>
<FormLabel>Welcome channel</FormLabel>
<Select onValueChange={field.onChange} defaultValue={field.value}>
<FormControl>
<SelectTrigger>
<SelectValue placeholder="Select a channel" />
</SelectTrigger>
</FormControl>
<SelectContent>
<SelectItem value="123">Channel 1</SelectItem>
<SelectItem value="234">Channel 2</SelectItem>
<SelectItem value="345">Channel 3</SelectItem>
</SelectContent>
</Select>
<FormDescription>
The channel where the welcome message will be sent
</FormDescription>
<FormMessage />
</FormItem>
)
} so i tried this but how would i go about type annotating that?