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

180 lines
4.4 KiB
TypeScript
Raw Normal View History

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