177 lines
4.0 KiB
TypeScript
Raw Normal View History

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