form
showcase-elevenlabs/formFreeComponent
shadcn/ui publishes no description for this item.
Install it
npx shadcn@latest add https://raw.githubusercontent.com/elevenlabs/showcase/main/apps/www/public/r/styles/new-york/form.jsonSource
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>(28 {} as FormFieldContextValue29)3031const FormField = <32 TFieldValues extends FieldValues = FieldValues,33 TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>34>({35 ...props36}: ControllerProps<TFieldValues, TName>) => {37 return (38 <FormFieldContext.Provider value={{ name: props.name }}>39 <Controller {...props} />40 </FormFieldContext.Provider>41 )42}4344const useFormField = () => {45 const fieldContext = React.useContext(FormFieldContext)46 const itemContext = React.useContext(FormItemContext)47 const { getFieldState, formState } = useFormContext()4849 const fieldState = getFieldState(fieldContext.name, formState)5051 if (!fieldContext) {52 throw new Error("useFormField should be used within <FormField>")53 }5455 const { id } = itemContext5657 return {58 id,59 name: fieldContext.name,60 formItemId: `${id}-form-item`,61 formDescriptionId: `${id}-form-item-description`,62 formMessageId: `${id}-form-item-message`,63 ...fieldState,64 }65}6667type FormItemContextValue = {68 id: string69}7071const FormItemContext = React.createContext<FormItemContextValue>(72 {} as FormItemContextValue73)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 className={cn(error && "text-destructive", className)}99 htmlFor={formItemId}100 {...props}101// … truncated
What it pulls in
npm packages
Other registry items
Files it writes
More from shadcn/ui
All 357 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| accordionaccordion | shadcn/ui | Components | Free | 1 dep | |
| alertalert | shadcn/ui | Components | Free | no deps | |
| alert-dialogalert-dialog | shadcn/ui | Components | Free | 1 dep | |
| aspect-ratioaspect-ratio | shadcn/ui | Components | Free | 1 dep | |
| avataravatar | shadcn/ui | Components | Free | 1 dep | |
| badgebadge | shadcn/ui | Components | Free | 1 dep | |
| breadcrumbbreadcrumb | shadcn/ui | Components | Free | 1 dep | |
| buttonbutton | shadcn/ui | Components | Free | 1 dep |
