193 lines
4.3 KiB
TypeScript
193 lines
4.3 KiB
TypeScript
'use client';
|
|
|
|
import { Slot } from '@radix-ui/react-slot';
|
|
import {
|
|
type ComponentPropsWithoutRef,
|
|
type ComponentRef,
|
|
type HTMLAttributes,
|
|
createContext,
|
|
forwardRef,
|
|
useContext,
|
|
useId,
|
|
} from 'react';
|
|
import {
|
|
Controller,
|
|
type ControllerProps,
|
|
type FieldPath,
|
|
type FieldValues,
|
|
FormProvider,
|
|
useFormContext,
|
|
} from 'react-hook-form';
|
|
|
|
import { cn } from '@/utils/tw-merge';
|
|
|
|
import { Label } from './Label';
|
|
|
|
import type * as LabelPrimitive from '@radix-ui/react-label';
|
|
|
|
const Form = FormProvider;
|
|
|
|
type FormFieldContextValue<
|
|
TFieldValues extends FieldValues = FieldValues,
|
|
TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>,
|
|
> = {
|
|
name: TName;
|
|
};
|
|
|
|
const FormFieldContext = createContext<FormFieldContextValue>(
|
|
{} as FormFieldContextValue,
|
|
);
|
|
|
|
const FormField = <
|
|
TFieldValues extends FieldValues = FieldValues,
|
|
TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>,
|
|
>({
|
|
...props
|
|
}: ControllerProps<TFieldValues, TName>) => (
|
|
// eslint-disable-next-line react/jsx-no-constructed-context-values
|
|
<FormFieldContext.Provider value={{ name: props.name }}>
|
|
<Controller {...props} />
|
|
</FormFieldContext.Provider>
|
|
);
|
|
|
|
type FormItemContextValue = {
|
|
id: string;
|
|
};
|
|
|
|
const FormItemContext = createContext<FormItemContextValue>(
|
|
{} as FormItemContextValue,
|
|
);
|
|
|
|
const FormItem = forwardRef<HTMLDivElement, HTMLAttributes<HTMLDivElement>>(
|
|
({ className, ...props }, ref) => {
|
|
const id = useId();
|
|
|
|
return (
|
|
// eslint-disable-next-line react/jsx-no-constructed-context-values
|
|
<FormItemContext.Provider value={{ id }}>
|
|
<div
|
|
ref={ref}
|
|
className={cn('space-y-2', className)}
|
|
{...props}
|
|
/>
|
|
</FormItemContext.Provider>
|
|
);
|
|
},
|
|
);
|
|
FormItem.displayName = 'FormItem';
|
|
|
|
const useFormField = () => {
|
|
const fieldContext = useContext(FormFieldContext);
|
|
const itemContext = useContext(FormItemContext);
|
|
const { getFieldState, formState } = useFormContext();
|
|
|
|
const fieldState = getFieldState(fieldContext.name, formState);
|
|
|
|
if (!fieldContext) {
|
|
throw new Error('useFormField should be used within <FormField>');
|
|
}
|
|
|
|
const { id } = itemContext;
|
|
|
|
return {
|
|
id,
|
|
name: fieldContext.name,
|
|
formItemId: `${id}-form-item`,
|
|
formDescriptionId: `${id}-form-item-description`,
|
|
formMessageId: `${id}-form-item-message`,
|
|
...fieldState,
|
|
};
|
|
};
|
|
const FormLabel = forwardRef<
|
|
ComponentRef<typeof LabelPrimitive.Root>,
|
|
ComponentPropsWithoutRef<typeof LabelPrimitive.Root>
|
|
>(({ className, ...props }, ref) => {
|
|
const { error, formItemId } = useFormField();
|
|
|
|
return (
|
|
<Label
|
|
ref={ref}
|
|
className={cn(error && 'text-destructive', className)}
|
|
htmlFor={formItemId}
|
|
{...props}
|
|
/>
|
|
);
|
|
});
|
|
FormLabel.displayName = 'FormLabel';
|
|
|
|
const FormControl = forwardRef<
|
|
ComponentRef<typeof Slot>,
|
|
ComponentPropsWithoutRef<typeof Slot>
|
|
>(({ ...props }, ref) => {
|
|
const {
|
|
error, formItemId, formDescriptionId, formMessageId,
|
|
} = useFormField();
|
|
|
|
return (
|
|
<Slot
|
|
ref={ref}
|
|
aria-describedby={
|
|
!error
|
|
? formDescriptionId
|
|
: `${formDescriptionId} ${formMessageId}`
|
|
}
|
|
aria-invalid={!!error}
|
|
id={formItemId}
|
|
{...props}
|
|
/>
|
|
);
|
|
});
|
|
FormControl.displayName = 'FormControl';
|
|
|
|
const FormDescription = forwardRef<
|
|
HTMLParagraphElement,
|
|
HTMLAttributes<HTMLParagraphElement>
|
|
>(({ className, ...props }, ref) => {
|
|
const { formDescriptionId } = useFormField();
|
|
|
|
return (
|
|
<p
|
|
ref={ref}
|
|
className={cn('text-sm text-muted-foreground', className)}
|
|
id={formDescriptionId}
|
|
{...props}
|
|
/>
|
|
);
|
|
});
|
|
FormDescription.displayName = 'FormDescription';
|
|
|
|
const FormMessage = forwardRef<
|
|
HTMLParagraphElement,
|
|
HTMLAttributes<HTMLParagraphElement>
|
|
>(({ className, children, ...props }, ref) => {
|
|
const { error, formMessageId } = useFormField();
|
|
const body = error ? String(error?.message) : children;
|
|
|
|
if (!body) {
|
|
return null;
|
|
}
|
|
|
|
return (
|
|
<p
|
|
ref={ref}
|
|
className={cn('text-sm font-medium text-destructive', className)}
|
|
id={formMessageId}
|
|
{...props}
|
|
>
|
|
{body}
|
|
</p>
|
|
);
|
|
});
|
|
FormMessage.displayName = 'FormMessage';
|
|
|
|
export {
|
|
Form,
|
|
FormControl,
|
|
FormDescription,
|
|
FormField,
|
|
FormItem,
|
|
FormLabel,
|
|
FormMessage,
|
|
useFormField,
|
|
};
|