diff --git a/packages/ui/v2/form/fields.tsx b/packages/ui/v2/form/fields.tsx index 4096c1312b..62e48adb65 100644 --- a/packages/ui/v2/form/fields.tsx +++ b/packages/ui/v2/form/fields.tsx @@ -1,14 +1,13 @@ import { useId } from "@radix-ui/react-id"; import React, { forwardRef, ReactElement, ReactNode, Ref } from "react"; -import { Info, Circle, Check, X } from "react-feather"; +import { Check, Circle, Info, X } from "react-feather"; import { + FieldErrors, FieldValues, FormProvider, SubmitHandler, useFormContext, UseFormReturn, - FormState, - FieldErrors, } from "react-hook-form"; import classNames from "@calcom/lib/classNames"; @@ -43,27 +42,38 @@ export function Label(props: JSX.IntrinsicElements["label"]) { function HintsOrErrors(props: { hintErrors?: string[]; - formState: FormState; fieldName: string; t: (key: string) => string; }) { - const { hintErrors, formState, fieldName, t } = props; + const methods = useFormContext() as ReturnType | null; + /* If there's no methods it means we're using these components outside a React Hook Form context */ + if (!methods) return null; + const { formState } = methods; + const { hintErrors, fieldName, t } = props; const fieldErrors: FieldErrors | undefined = formState.errors[fieldName]; if (!hintErrors && fieldErrors && !fieldErrors.message) { // no hints passed, field errors exist and they are custom ones return ( -
-
    - {Object.keys(fieldErrors).map((key: string) => { - return ( -
  • - {t(`${fieldName}_hint_${key}`)} -
  • - ); - })} -
-
+ <> + {fieldErrors?.message && ( +
+ + {fieldErrors.message} +
+ )} +
+
    + {Object.keys(fieldErrors).map((key: string) => { + return ( +
  • + {t(`${fieldName}_hint_${key}`)} +
  • + ); + })} +
+
+ ); } @@ -149,7 +159,6 @@ const InputField = forwardRef(function InputF const id = useId(); const { t: _t } = useLocale(); const t = props.t || _t; - const methods = useFormContext(); const { label = t(props.name), labelProps, @@ -217,13 +226,7 @@ const InputField = forwardRef(function InputF ) : ( )} - {methods.formState.errors[props.name]?.message && ( -
- - {methods.formState.errors[props.name].message} -
- )} - + {hint &&
{hint}
} );