import { InformationCircleSolid } from "@medusajs/icons" import { Hint as HintComponent, Label as LabelComponent, Text, Tooltip, clx, } from "@medusajs/ui" import { Label as RadixLabel, Slot } from "radix-ui" import React, { ReactNode, createContext, forwardRef, useContext, useId, } from "react" import { Controller, ControllerProps, FieldPath, FieldValues, FormProvider, useFormContext, useFormState, } from "react-hook-form" import { useTranslation } from "react-i18next" const Provider = FormProvider type FormFieldContextValue< TFieldValues extends FieldValues = FieldValues, TName extends FieldPath = FieldPath > = { name: TName } const FormFieldContext = createContext( {} as FormFieldContextValue ) const Field = < TFieldValues extends FieldValues = FieldValues, TName extends FieldPath = FieldPath >({ ...props }: ControllerProps) => { return ( ) } type FormItemContextValue = { id: string } const FormItemContext = createContext( {} as FormItemContextValue ) const useFormField = () => { const fieldContext = useContext(FormFieldContext) const itemContext = useContext(FormItemContext) const { getFieldState } = useFormContext() const formState = useFormState({ name: fieldContext.name }) const fieldState = getFieldState(fieldContext.name, formState) if (!fieldContext) { throw new Error("useFormField should be used within a FormField") } const { id } = itemContext return { id, name: fieldContext.name, formItemId: `${id}-form-item`, formLabelId: `${id}-form-item-label`, formDescriptionId: `${id}-form-item-description`, formErrorMessageId: `${id}-form-item-message`, ...fieldState, } } const Item = forwardRef>( ({ className, ...props }, ref) => { const id = useId() return (
) } ) Item.displayName = "Form.Item" const Label = forwardRef< React.ElementRef, React.ComponentPropsWithoutRef & { optional?: boolean tooltip?: ReactNode icon?: ReactNode } >(({ className, optional = false, tooltip, icon, ...props }, ref) => { const { formLabelId, formItemId } = useFormField() const { t } = useTranslation() return (
{tooltip && ( )} {icon} {optional && ( ({t("fields.optional")}) )}
) }) Label.displayName = "Form.Label" const Control = forwardRef< React.ElementRef, React.ComponentPropsWithoutRef >(({ ...props }, ref) => { const { error, formItemId, formDescriptionId, formErrorMessageId, formLabelId, } = useFormField() return ( ) }) Control.displayName = "Form.Control" const Hint = forwardRef< HTMLParagraphElement, React.HTMLAttributes >(({ className, ...props }, ref) => { const { formDescriptionId } = useFormField() return ( ) }) Hint.displayName = "Form.Hint" const ErrorMessage = forwardRef< HTMLParagraphElement, React.HTMLAttributes >(({ className, children, ...props }, ref) => { const { error, formErrorMessageId } = useFormField() const msg = error ? String(error?.message) : children if (!msg || msg === "undefined") { return null } return ( {msg} ) }) ErrorMessage.displayName = "Form.ErrorMessage" const Form = Object.assign(Provider, { Item, Label, Control, Hint, ErrorMessage, Field, }) export { Form }