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