FactuGES_ERP/packages/shadcn-ui/src/components/form.tsx

154 lines
3.7 KiB
TypeScript
Raw Normal View History

2025-09-24 11:49:17 +00:00
"use client";
2025-05-05 17:11:44 +00:00
2025-09-24 11:49:17 +00:00
import * as LabelPrimitive from "@radix-ui/react-label";
import { Slot } from "@radix-ui/react-slot";
import * as React from "react";
2025-05-05 17:11:44 +00:00
import {
Controller,
type ControllerProps,
type FieldPath,
type FieldValues,
2025-09-24 11:49:17 +00:00
FormProvider,
useFormContext,
useFormState,
} from "react-hook-form";
2025-05-05 17:11:44 +00:00
2025-09-24 11:49:17 +00:00
import { Label } from "@repo/shadcn-ui/components/label";
import { cn } from "@repo/shadcn-ui/lib/utils";
2025-05-05 17:11:44 +00:00
2025-09-24 11:49:17 +00:00
const Form = FormProvider;
2025-05-05 17:11:44 +00:00
type FormFieldContextValue<
TFieldValues extends FieldValues = FieldValues,
TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>,
> = {
2025-09-24 11:49:17 +00:00
name: TName;
};
2025-05-05 17:11:44 +00:00
2025-09-24 11:49:17 +00:00
const FormFieldContext = React.createContext<FormFieldContextValue>({} as FormFieldContextValue);
2025-05-05 17:11:44 +00: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>
2025-09-24 11:49:17 +00:00
);
};
2025-05-05 17:11:44 +00:00
const useFormField = () => {
2025-09-24 11:49:17 +00:00
const fieldContext = React.useContext(FormFieldContext);
const itemContext = React.useContext(FormItemContext);
const { getFieldState } = useFormContext();
const formState = useFormState({ name: fieldContext.name });
const fieldState = getFieldState(fieldContext.name, formState);
2025-05-05 17:11:44 +00:00
if (!fieldContext) {
2025-09-24 11:49:17 +00:00
throw new Error("useFormField should be used within <FormField>");
2025-05-05 17:11:44 +00:00
}
2025-09-24 11:49:17 +00:00
const { id } = itemContext;
2025-05-05 17:11:44 +00:00
return {
id,
name: fieldContext.name,
formItemId: `${id}-form-item`,
formDescriptionId: `${id}-form-item-description`,
formMessageId: `${id}-form-item-message`,
...fieldState,
2025-09-24 11:49:17 +00:00
};
};
2025-05-05 17:11:44 +00:00
type FormItemContextValue = {
2025-09-24 11:49:17 +00:00
id: string;
};
2025-05-05 17:11:44 +00:00
2025-09-24 11:49:17 +00:00
const FormItemContext = React.createContext<FormItemContextValue>({} as FormItemContextValue);
2025-05-05 17:11:44 +00:00
function FormItem({ className, ...props }: React.ComponentProps<"div">) {
2025-09-24 11:49:17 +00:00
const id = React.useId();
2025-05-05 17:11:44 +00:00
return (
<FormItemContext.Provider value={{ id }}>
2025-09-24 11:49:17 +00:00
<div data-slot='form-item' className={cn("grid gap-2", className)} {...props} />
2025-05-05 17:11:44 +00:00
</FormItemContext.Provider>
2025-09-24 11:49:17 +00:00
);
2025-05-05 17:11:44 +00:00
}
2025-09-24 11:49:17 +00:00
function FormLabel({ className, ...props }: React.ComponentProps<typeof LabelPrimitive.Root>) {
const { error, formItemId } = useFormField();
2025-05-05 17:11:44 +00:00
return (
<Label
2025-09-24 11:49:17 +00:00
data-slot='form-label'
2025-05-05 17:11:44 +00:00
data-error={!!error}
className={cn("data-[error=true]:text-destructive", className)}
htmlFor={formItemId}
{...props}
/>
2025-09-24 11:49:17 +00:00
);
2025-05-05 17:11:44 +00:00
}
function FormControl({ ...props }: React.ComponentProps<typeof Slot>) {
2025-09-24 11:49:17 +00:00
const { error, formItemId, formDescriptionId, formMessageId } = useFormField();
2025-05-05 17:11:44 +00:00
return (
<Slot
2025-09-24 11:49:17 +00:00
data-slot='form-control'
2025-05-05 17:11:44 +00:00
id={formItemId}
2025-09-24 11:49:17 +00:00
aria-describedby={!error ? `${formDescriptionId}` : `${formDescriptionId} ${formMessageId}`}
2025-05-05 17:11:44 +00:00
aria-invalid={!!error}
{...props}
/>
2025-09-24 11:49:17 +00:00
);
2025-05-05 17:11:44 +00:00
}
function FormDescription({ className, ...props }: React.ComponentProps<"p">) {
2025-09-24 11:49:17 +00:00
const { formDescriptionId } = useFormField();
2025-05-05 17:11:44 +00:00
return (
<p
2025-09-24 11:49:17 +00:00
data-slot='form-description'
2025-05-05 17:11:44 +00:00
id={formDescriptionId}
className={cn("text-muted-foreground text-sm", className)}
{...props}
/>
2025-09-24 11:49:17 +00:00
);
2025-05-05 17:11:44 +00:00
}
function FormMessage({ className, ...props }: React.ComponentProps<"p">) {
2025-09-24 11:49:17 +00:00
const { error, formMessageId } = useFormField();
const body = error ? String(error?.message ?? "") : props.children;
2025-05-05 17:11:44 +00:00
if (!body) {
2025-09-24 11:49:17 +00:00
return null;
2025-05-05 17:11:44 +00:00
}
return (
<p
2025-09-24 11:49:17 +00:00
data-slot='form-message'
2025-05-05 17:11:44 +00:00
id={formMessageId}
className={cn("text-destructive text-sm", className)}
{...props}
>
{body}
</p>
2025-09-24 11:49:17 +00:00
);
2025-05-05 17:11:44 +00:00
}
export {
Form,
FormControl,
FormDescription,
FormField,
2025-09-24 11:49:17 +00:00
FormItem,
FormLabel,
FormMessage,
2025-10-14 13:46:57 +00:00
useFormField
2025-09-24 11:49:17 +00:00
};
2025-10-14 13:46:57 +00:00