form
9ui/formFreeComponent
9ui publishes no description for this item.
Live preview
live · rendered by the registry
Rendered by 9ui on their own page — this is the component running, not a screenshot of it.Install it
npx shadcn@latest add https://9ui.dev/r/form.jsonSource
1"use client"23import * as React from "react"4import {5 Controller,6 FormProvider,7 useFormContext,8 useFormState,9 type ControllerProps,10 type FieldPath,11 type FieldValues,12} from "react-hook-form"1314import { Label } from "@/components/ui/label"1516import { cn } from "@/lib/utils"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 } = 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 FormItemContextValue73)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 Label>) {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", className)}100 htmlFor={formItemId}101 {...props}102 />103 )104}105106function FormControl({ ...props }: React.ComponentProps<"div">) {107 const { error, formItemId, formDescriptionId, formMessageId } = useFormField()108109 return (110 <div111 data-slot="form-control"112 id={formItemId}113// … truncated
What it pulls in
npm packages
Other registry items
Files it writes
More from 9ui
All 56 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| accordionaccordion | 9ui | Components | Free | no deps | |
| alertalert | 9ui | Components | Free | no deps | |
| alert-dialogalert-dialog | 9ui | Components | Free | no deps | |
| aspect-ratioaspect-ratio | 9ui | Components | Free | no deps | |
| autocompleteautocomplete | 9ui | Components | Free | no deps | |
| avataravatar | 9ui | Components | Free | no deps | |
| badgebadge | 9ui | Components | Free | no deps | |
| breadcrumbsbreadcrumbs | 9ui | Components | Free | no deps |
