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

204 lines
4.6 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
)
2025-04-12 12:40:52 -04:00
const FormItem = (
{
ref,
className,
...props
}: React.HTMLAttributes<HTMLDivElement> & {
ref: React.RefObject<HTMLDivElement>;
}
) => {
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>
)
2025-04-12 12:40:52 -04:00
}
2024-10-06 09:55:45 -04:00
FormItem.displayName = "FormItem"
2025-04-12 12:40:52 -04:00
const FormLabel = (
{
ref,
className,
...props
}: React.ComponentPropsWithoutRef<typeof LabelPrimitive.Root> & {
ref: React.RefObject<React.ElementRef<typeof LabelPrimitive.Root>>;
}
) => {
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}
/>
)
2025-04-12 12:40:52 -04:00
}
2024-10-06 09:55:45 -04:00
FormLabel.displayName = "FormLabel"
2025-04-12 12:40:52 -04:00
const FormControl = (
{
ref,
...props
}: React.ComponentPropsWithoutRef<typeof Slot> & {
ref: React.RefObject<React.ElementRef<typeof Slot>>;
}
) => {
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}
/>
)
2025-04-12 12:40:52 -04:00
}
2024-10-06 09:55:45 -04:00
FormControl.displayName = "FormControl"
2025-04-12 12:40:52 -04:00
const FormDescription = (
{
ref,
className,
...props
}: React.HTMLAttributes<HTMLParagraphElement> & {
ref: React.RefObject<HTMLParagraphElement>;
}
) => {
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}
/>
)
2025-04-12 12:40:52 -04:00
}
2024-10-06 09:55:45 -04:00
FormDescription.displayName = "FormDescription"
2025-04-12 12:40:52 -04:00
const FormMessage = (
{
ref,
className,
children,
...props
}: React.HTMLAttributes<HTMLParagraphElement> & {
ref: React.RefObject<HTMLParagraphElement>;
}
) => {
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>
)
2025-04-12 12:40:52 -04:00
}
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
}