form
shadcn/formFreeComponent
shadcn publishes no description for this item.
Live preview
live · rendered by the registry
Rendered by shadcn on their own page — this is the component running, not a screenshot of it.Install it
npx shadcn@latest add formSource
1"use client"23import * as React from "react"4import * as LabelPrimitive from "@radix-ui/react-label"5import { Slot } from "@radix-ui/react-slot"6import {7 Controller,8 FormProvider,9 useFormContext,10 type ControllerProps,11 type FieldPath,12 type FieldValues,13} from "react-hook-form"1415import { cn } from "@/lib/utils"16import { Label } from "@/registry/new-york/ui/label"1718const Form = FormProvider1920type FormFieldContextValue<21 TFieldValues extends FieldValues = FieldValues,22 TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>23> = {24 name: TName25}2627const FormFieldContext = React.createContext<FormFieldContextValue | null>(null)2829const FormField = <30 TFieldValues extends FieldValues = FieldValues,31 TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>32>({33 ...props34}: ControllerProps<TFieldValues, TName>) => {35 return (36 <FormFieldContext.Provider value={{ name: props.name }}>37 <Controller {...props} />38 </FormFieldContext.Provider>39 )40}4142const useFormField = () => {43 const fieldContext = React.useContext(FormFieldContext)44 const itemContext = React.useContext(FormItemContext)45 const { getFieldState, formState } = useFormContext()4647 if (!fieldContext) {48 throw new Error("useFormField should be used within <FormField>")49 }5051 if (!itemContext) {52 throw new Error("useFormField should be used within <FormItem>")53 }5455 const fieldState = getFieldState(fieldContext.name, formState)5657 const { id } = itemContext5859 return {60 id,61 name: fieldContext.name,62 formItemId: `${id}-form-item`,63 formDescriptionId: `${id}-form-item-description`,64 formMessageId: `${id}-form-item-message`,65 ...fieldState,66 }67}6869type FormItemContextValue = {70 id: string71}7273const FormItemContext = React.createContext<FormItemContextValue | null>(null)7475const FormItem = React.forwardRef<76 HTMLDivElement,77 React.HTMLAttributes<HTMLDivElement>78>(({ className, ...props }, ref) => {79 const id = React.useId()8081 return (82 <FormItemContext.Provider value={{ id }}>83 <div ref={ref} className={cn("space-y-2", className)} {...props} />84 </FormItemContext.Provider>85 )86})87FormItem.displayName = "FormItem"8889const FormLabel = React.forwardRef<90 React.ElementRef<typeof LabelPrimitive.Root>,91 React.ComponentPropsWithoutRef<typeof LabelPrimitive.Root>92>(({ className, ...props }, ref) => {93 const { error, formItemId } = useFormField()9495 return (96 <Label97 ref={ref}98// … truncated
More from shadcn
All 62 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| accordionaccordion | shadcn | Components | Free | no deps | |
| alertalert | shadcn | Components | Free | no deps | |
| alert-dialogalert-dialog | shadcn | Components | Free | no deps | |
| aspect-ratioaspect-ratio | shadcn | Components | Free | no deps | |
| attachmentattachment | shadcn | Components | Free | no deps | |
| avataravatar | shadcn | Components | Free | no deps | |
| badgebadge | shadcn | Components | Free | no deps | |
| breadcrumbbreadcrumb | shadcn | Components | Free | no deps |
