1 import { InformationCircleSolid } from "@medusajs/icons"
2 import {
3 Hint as HintComponent,
4 Label as LabelComponent,
5 Text,
6 Tooltip,
7 clx,
8 } from "@medusajs/ui"
9 import { Label as RadixLabel, Slot } from "radix-ui"
10 import React, {
11 ReactNode,
12 createContext,
13 forwardRef,
14 useContext,
15 useId,
16 } from "react"
17 import {
18 Controller,
19 ControllerProps,
20 FieldPath,
21 FieldValues,
22 FormProvider,
23 useFormContext,
24 useFormState,
25 } from "react-hook-form"
26 import { useTranslation } from "react-i18next"
27
28 const Provider = FormProvider
29
30 type FormFieldContextValue<
31 TFieldValues extends FieldValues = FieldValues,
32 TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>
33 > = {
34 name: TName
35 }
36
37 const FormFieldContext = createContext<FormFieldContextValue>(
38 {} as FormFieldContextValue
39 )
40
41 const Field = <
42 TFieldValues extends FieldValues = FieldValues,
43 TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>
44 >({
45 ...props
46 }: ControllerProps<TFieldValues, TName>) => {
47 return (
48 <FormFieldContext.Provider value={{ name: props.name }}>
49 <Controller {...props} />
50 </FormFieldContext.Provider>
51 )
52 }
53
54 type FormItemContextValue = {
55 id: string
56 }
57
58 const FormItemContext = createContext<FormItemContextValue>(
59 {} as FormItemContextValue
60 )
61
62 const useFormField = () => {
63 const fieldContext = useContext(FormFieldContext)
64 const itemContext = useContext(FormItemContext)
65 const { getFieldState } = useFormContext()
66
67 const formState = useFormState({ name: fieldContext.name })
68 const fieldState = getFieldState(fieldContext.name, formState)
69
70 if (!fieldContext) {
71 throw new Error("useFormField should be used within a FormField")
72 }
73
74 const { id } = itemContext
75
76 return {
77 id,
78 name: fieldContext.name,
79 formItemId: `${id}-form-item`,
80 formLabelId: `${id}-form-item-label`,
81 formDescriptionId: `${id}-form-item-description`,
82 formErrorMessageId: `${id}-form-item-message`,
83 ...fieldState,
84 }
85 }
86
87 const Item = forwardRef<HTMLDivElement, React.HTMLAttributes<HTMLDivElement>>(
88 ({ className, ...props }, ref) => {
89 const id = useId()
90
91 return (
92 <FormItemContext.Provider value={{ id }}>
93 <div
94 ref={ref}
95 className={clx("flex flex-col space-y-2", className)}
96 {...props}
97 />
98 </FormItemContext.Provider>
99 )
100 }
101 )
102 Item.displayName = "Form.Item"
103
104 const Label = forwardRef<
105 React.ElementRef<typeof RadixLabel.Root>,
106 React.ComponentPropsWithoutRef<typeof RadixLabel.Root> & {
107 optional?: boolean
108 tooltip?: ReactNode
109 icon?: ReactNode
110 }
111 >(({ className, optional = false, tooltip, icon, ...props }, ref) => {
112 const { formLabelId, formItemId } = useFormField()
113 const { t } = useTranslation()
114
115 return (
116 <div className="flex items-center gap-x-1">
117 <LabelComponent
118 id={formLabelId}
119 ref={ref}
120 className={clx(className)}
121 htmlFor={formItemId}
122 size="small"
123 weight="plus"
124 {...props}
125 />
126 {tooltip && (
127 <Tooltip content={tooltip}>
128 <InformationCircleSolid className="text-ui-fg-muted" />
129 </Tooltip>
130 )}
131 {icon}
132 {optional && (
133 <Text size="small" leading="compact" className="text-ui-fg-muted">
134 ({t("fields.optional")})
135 </Text>
136 )}
137 </div>
138 )
139 })
140 Label.displayName = "Form.Label"
141
142 const Control = forwardRef<
143 React.ElementRef<typeof Slot.Root>,
144 React.ComponentPropsWithoutRef<typeof Slot.Root>
145 >(({ ...props }, ref) => {
146 const {
147 error,
148 formItemId,
149 formDescriptionId,
150 formErrorMessageId,
151 formLabelId,
152 } = useFormField()
153
154 return (
155 <Slot.Root
156 ref={ref}
157 id={formItemId}
158 aria-describedby={
159 !error
160 ? `${formDescriptionId}`
161 : `${formDescriptionId} ${formErrorMessageId}`
162 }
163 aria-invalid={!!error}
164 aria-labelledby={formLabelId}
165 {...props}
166 />
167 )
168 })
169 Control.displayName = "Form.Control"
170
171 const Hint = forwardRef<
172 HTMLParagraphElement,
173 React.HTMLAttributes<HTMLParagraphElement>
174 >(({ className, ...props }, ref) => {
175 const { formDescriptionId } = useFormField()
176
177 return (
178 <HintComponent
179 ref={ref}
180 id={formDescriptionId}
181 className={className}
182 {...props}
183 />
184 )
185 })
186 Hint.displayName = "Form.Hint"
187
188 const ErrorMessage = forwardRef<
189 HTMLParagraphElement,
190 React.HTMLAttributes<HTMLParagraphElement>
191 >(({ className, children, ...props }, ref) => {
192 const { error, formErrorMessageId } = useFormField()
193 const msg = error ? String(error?.message) : children
194
195 if (!msg || msg === "undefined") {
196 return null
197 }
198
199 return (
200 <HintComponent
201 ref={ref}
202 id={formErrorMessageId}
203 className={className}
204 variant={error ? "error" : "info"}
205 {...props}
206 >
207 {msg}
208 </HintComponent>
209 )
210 })
211 ErrorMessage.displayName = "Form.ErrorMessage"
212
213 const Form = Object.assign(Provider, {
214 Item,
215 Label,
216 Control,
217 Hint,
218 ErrorMessage,
219 Field,
220 })
221
222 export { Form }