Files
Termix/src/components/ui/form.tsx
T

167 lines
3.7 KiB
TypeScript
Raw Normal View History

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