mirror of
https://github.com/shadcn-ui/ui.git
synced 2026-06-29 23:55:02 +00:00
* refactor: remove usage of uncontrolled input apis within controlled inputs Closes: #384 * docs: display FormField controlled input usage example --------- Co-authored-by: shadcn <m@shadcn.com>
79 lines
2.1 KiB
TypeScript
79 lines
2.1 KiB
TypeScript
"use client"
|
|
|
|
import Link from "next/link"
|
|
import { zodResolver } from "@hookform/resolvers/zod"
|
|
import { useForm } from "react-hook-form"
|
|
import * as z from "zod"
|
|
|
|
import { Button } from "@/components/ui/button"
|
|
import { Checkbox } from "@/components/ui/checkbox"
|
|
import { RadioGroup, RadioGroupItem } from "@/components/ui/radio-group"
|
|
import { Switch } from "@/components/ui/switch"
|
|
import { Textarea } from "@/components/ui/textarea"
|
|
import { toast } from "@/components/ui/use-toast"
|
|
import {
|
|
Form,
|
|
FormControl,
|
|
FormDescription,
|
|
FormField,
|
|
FormItem,
|
|
FormLabel,
|
|
FormMessage,
|
|
} from "@/components/react-hook-form/form"
|
|
|
|
const FormSchema = z.object({
|
|
bio: z
|
|
.string()
|
|
.min(10, {
|
|
message: "Bio must be at least 10 characters.",
|
|
})
|
|
.max(160, {
|
|
message: "Bio must not be longer than 30 characters.",
|
|
}),
|
|
})
|
|
|
|
export function TextareaReactHookForm() {
|
|
const form = useForm<z.infer<typeof FormSchema>>({
|
|
resolver: zodResolver(FormSchema),
|
|
})
|
|
|
|
function onSubmit(data: z.infer<typeof FormSchema>) {
|
|
toast({
|
|
title: "You submitted the following values:",
|
|
description: (
|
|
<pre className="mt-2 w-[340px] rounded-md bg-slate-950 p-4">
|
|
<code className="text-white">{JSON.stringify(data, null, 2)}</code>
|
|
</pre>
|
|
),
|
|
})
|
|
}
|
|
|
|
return (
|
|
<Form {...form}>
|
|
<form onSubmit={form.handleSubmit(onSubmit)} className="w-2/3 space-y-6">
|
|
<FormField
|
|
control={form.control}
|
|
name="bio"
|
|
render={({ field }) => (
|
|
<FormItem>
|
|
<FormLabel>Bio</FormLabel>
|
|
<FormControl>
|
|
<Textarea
|
|
placeholder="Tell us a little bit about yourself"
|
|
className="resize-none"
|
|
{...field}
|
|
/>
|
|
</FormControl>
|
|
<FormDescription>
|
|
You can <span>@mention</span> other users and organizations.
|
|
</FormDescription>
|
|
<FormMessage />
|
|
</FormItem>
|
|
)}
|
|
/>
|
|
<Button type="submit">Submit</Button>
|
|
</form>
|
|
</Form>
|
|
)
|
|
}
|