fosrl.pangolin/src/components/ui/form.tsx

179 lines
4.4 KiB
TypeScript
Raw Normal View History

2024-10-06 09:55:45 -04:00
"use client"
import * as React from "react"
import * as LabelPrimitive from "@radix-ui/react-label"
import { Slot } from "@radix-ui/react-slot"
import {
2024-10-06 18:05:20 -04:00
Controller,
ControllerProps,
FieldPath,
FieldValues,
FormProvider,
useFormContext,
2024-10-06 09:55:45 -04:00
} from "react-hook-form"
2025-01-01 21:41:31 -05:00
import { cn } from "@app/lib/cn"
2024-10-06 09:55:45 -04:00
import { Label } from "@/components/ui/label"
const Form = FormProvider
type FormFieldContextValue<
2024-10-06 18:05:20 -04:00
TFieldValues extends FieldValues = FieldValues,
TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>
2024-10-06 09:55:45 -04:00
> = {
2024-10-06 18:05:20 -04:00
name: TName
2024-10-06 09:55:45 -04:00
}
const FormFieldContext = React.createContext<FormFieldContextValue>(
2024-10-06 18:05:20 -04:00
{} as FormFieldContextValue
2024-10-06 09:55:45 -04:00
)
const FormField = <
2024-10-06 18:05:20 -04:00
TFieldValues extends FieldValues = FieldValues,
TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>
2024-10-06 09:55:45 -04:00
>({
2024-10-06 18:05:20 -04:00
...props
2024-10-06 09:55:45 -04:00
}: ControllerProps<TFieldValues, TName>) => {
2024-10-06 18:05:20 -04:00
return (
<FormFieldContext.Provider value={{ name: props.name }}>
<Controller {...props} />
</FormFieldContext.Provider>
)
2024-10-06 09:55:45 -04:00
}
const useFormField = () => {
2024-10-06 18:05:20 -04:00
const fieldContext = React.useContext(FormFieldContext)
const itemContext = React.useContext(FormItemContext)
const { getFieldState, formState } = useFormContext()
const fieldState = getFieldState(fieldContext.name, formState)
if (!fieldContext) {
throw new Error("useFormField should be used within <FormField>")
}
const { id } = itemContext
return {
id,
name: fieldContext.name,
formItemId: `${id}-form-item`,
formDescriptionId: `${id}-form-item-description`,
formMessageId: `${id}-form-item-message`,
...fieldState,
}
2024-10-06 09:55:45 -04:00
}
type FormItemContextValue = {
2024-10-06 18:05:20 -04:00
id: string
2024-10-06 09:55:45 -04:00
}
const FormItemContext = React.createContext<FormItemContextValue>(
2024-10-06 18:05:20 -04:00
{} as FormItemContextValue
2024-10-06 09:55:45 -04:00
)
const FormItem = React.forwardRef<
2024-10-06 18:05:20 -04:00
HTMLDivElement,
React.HTMLAttributes<HTMLDivElement>
2024-10-06 09:55:45 -04:00
>(({ className, ...props }, ref) => {
2024-10-06 18:05:20 -04:00
const id = React.useId()
2024-10-06 09:55:45 -04:00
2024-10-06 18:05:20 -04:00
return (
<FormItemContext.Provider value={{ id }}>
<div ref={ref} className={cn("space-y-2", className)} {...props} />
</FormItemContext.Provider>
)
2024-10-06 09:55:45 -04:00
})
FormItem.displayName = "FormItem"
const FormLabel = React.forwardRef<
2024-10-06 18:05:20 -04:00
React.ElementRef<typeof LabelPrimitive.Root>,
React.ComponentPropsWithoutRef<typeof LabelPrimitive.Root>
2024-10-06 09:55:45 -04:00
>(({ className, ...props }, ref) => {
2024-10-06 18:05:20 -04:00
const { error, formItemId } = useFormField()
return (
<Label
ref={ref}
className={cn(error && "text-destructive", className)}
htmlFor={formItemId}
{...props}
/>
)
2024-10-06 09:55:45 -04:00
})
FormLabel.displayName = "FormLabel"
const FormControl = React.forwardRef<
2024-10-06 18:05:20 -04:00
React.ElementRef<typeof Slot>,
React.ComponentPropsWithoutRef<typeof Slot>
2024-10-06 09:55:45 -04:00
>(({ ...props }, ref) => {
2024-10-06 18:05:20 -04:00
const { error, formItemId, formDescriptionId, formMessageId } = useFormField()
return (
<Slot
ref={ref}
id={formItemId}
aria-describedby={
!error
? `${formDescriptionId}`
: `${formDescriptionId} ${formMessageId}`
}
aria-invalid={!!error}
{...props}
/>
)
2024-10-06 09:55:45 -04:00
})
FormControl.displayName = "FormControl"
const FormDescription = React.forwardRef<
2024-10-06 18:05:20 -04:00
HTMLParagraphElement,
React.HTMLAttributes<HTMLParagraphElement>
2024-10-06 09:55:45 -04:00
>(({ className, ...props }, ref) => {
2024-10-06 18:05:20 -04:00
const { formDescriptionId } = useFormField()
return (
<p
ref={ref}
id={formDescriptionId}
className={cn("text-sm text-muted-foreground", className)}
{...props}
/>
)
2024-10-06 09:55:45 -04:00
})
FormDescription.displayName = "FormDescription"
const FormMessage = React.forwardRef<
2024-10-06 18:05:20 -04:00
HTMLParagraphElement,
React.HTMLAttributes<HTMLParagraphElement>
2024-10-06 09:55:45 -04:00
>(({ className, children, ...props }, ref) => {
2024-10-06 18:05:20 -04:00
const { error, formMessageId } = useFormField()
const body = error ? String(error?.message) : children
if (!body) {
return null
}
return (
<p
ref={ref}
id={formMessageId}
className={cn("text-sm font-medium text-destructive", className)}
{...props}
>
{body}
</p>
)
2024-10-06 09:55:45 -04:00
})
FormMessage.displayName = "FormMessage"
export {
2024-10-06 18:05:20 -04:00
useFormField,
Form,
FormItem,
FormLabel,
FormControl,
FormDescription,
FormMessage,
FormField,
2024-10-06 09:55:45 -04:00
}