form
nrjdalal/formFreeComponent
acme publishes no description for this item.
Install it
npx shadcn@latest add https://nrjdalal.dev/r/form.jsonSource
1"use client"23import { Label } from "@/components/ui/label"4import { cn } from "@/lib/utils"5import * as LabelPrimitive from "@radix-ui/react-label"6import { Slot } from "@radix-ui/react-slot"7import * as React from "react"8import {9 Controller,10 ControllerProps,11 FieldPath,12 FieldValues,13 FormProvider,14 useFormContext,15 useFormState,16} from "react-hook-form"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 FormFieldContextValue,29)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 } = useFormContext()48 const formState = useFormState({ name: fieldContext.name })49 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 FormItemContextValue,73)7475function FormItem({ className, ...props }: React.ComponentProps<"div">) {76 const id = React.useId()7778 return (79 <FormItemContext.Provider value={{ id }}>80 <div81 data-slot="form-item"82 className={cn("grid gap-2", className)}83 {...props}84 />85 </FormItemContext.Provider>86 )87}8889function FormLabel({90 className,91 ...props92}: React.ComponentProps<typeof LabelPrimitive.Root>) {93 const { error, formItemId } = useFormField()9495 return (96 <Label97 data-slot="form-label"98 data-error={!!error}99 className={cn("data-[error=true]:text-destructive-foreground", className)}100 htmlFor={formItemId}101 {...props}102 />103 )104}105106// … truncated
What it pulls in
npm packages
Files it writes
More from acme
All 49 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| accordionaccordion | acme | Components | Free | 5 deps · 2 files | |
| alertalert | acme | Components | Free | 4 deps · 2 files | |
| alert-dialogalert-dialog | acme | Components | Free | 6 deps · 3 files | |
| aspect-ratioaspect-ratio | acme | Components | Free | 1 dep | |
| avataravatar | acme | Components | Free | 4 deps · 2 files | |
| badgebadge | acme | Components | Free | 5 deps · 2 files | |
| breadcrumbbreadcrumb | acme | Components | Free | 5 deps · 2 files | |
| buttonbutton | acme | Components | Free | 5 deps · 2 files |
