From 298a6d5e15e10342122814d8f0a15971293f0367 Mon Sep 17 00:00:00 2001 From: Leo Giovanetti Date: Wed, 3 Aug 2022 13:59:40 -0300 Subject: [PATCH] Tweaks for Website signup form (#3635) * WIP, only deep testing needed * Feedback suggestions * Uses early returns instead * Allows overriding t function via prop Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com> Co-authored-by: zomars --- apps/web/pages/auth/setup.tsx | 2 +- packages/ui/v2/form/fields.tsx | 150 ++++++++++++++++++++++----------- yarn.lock | 5 -- 3 files changed, 101 insertions(+), 56 deletions(-) diff --git a/apps/web/pages/auth/setup.tsx b/apps/web/pages/auth/setup.tsx index 591ce37c81..8f4921a410 100644 --- a/apps/web/pages/auth/setup.tsx +++ b/apps/web/pages/auth/setup.tsx @@ -44,7 +44,7 @@ const SetupFormStep1 = (props: { setIsLoading: (val: boolean) => void }) => { ctx.addIssue({ code: z.ZodIssueCode.custom, path: [key], - message: t(`password_hint_${key}`), + message: key, }); } }); diff --git a/packages/ui/v2/form/fields.tsx b/packages/ui/v2/form/fields.tsx index 906cd74875..4096c1312b 100644 --- a/packages/ui/v2/form/fields.tsx +++ b/packages/ui/v2/form/fields.tsx @@ -1,7 +1,15 @@ import { useId } from "@radix-ui/react-id"; import React, { forwardRef, ReactElement, ReactNode, Ref } from "react"; import { Info, Circle, Check, X } from "react-feather"; -import { FieldValues, FormProvider, SubmitHandler, useFormContext, UseFormReturn } from "react-hook-form"; +import { + FieldValues, + FormProvider, + SubmitHandler, + useFormContext, + UseFormReturn, + FormState, + FieldErrors, +} from "react-hook-form"; import classNames from "@calcom/lib/classNames"; import { getErrorFromUnknown } from "@calcom/lib/errors"; @@ -33,6 +41,87 @@ 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 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}`)} +
  • + ); + })} +
+
+ ); + } + + if (hintErrors && fieldErrors) { + // hints passed, field errors exist + return ( +
+
    + {hintErrors.map((key: string) => { + const submitted = formState.isSubmitted; + const error = fieldErrors[key] || fieldErrors.message; + return ( +
  • + {error !== undefined ? ( + submitted ? ( + + ) : ( + + ) + ) : ( + + )} + {t(`${fieldName}_hint_${key}`)} +
  • + ); + })} +
+
+ ); + } + + if (!hintErrors) return null; + + // hints passed, no errors exist, proceed to just show hints + return ( +
+
    + {hintErrors.map((key: string) => { + // if field was changed, as no error exist, show checked status and color + const dirty = formState.dirtyFields[fieldName]; + return ( +
  • + {!!dirty ? ( + + ) : ( + + )} + {t(`${fieldName}_hint_${key}`)} +
  • + ); + })} +
+
+ ); +} + export function InputLeading(props: JSX.IntrinsicElements["div"]) { return ( @@ -51,13 +140,15 @@ type InputFieldProps = { error?: string; labelSrOnly?: boolean; containerClassName?: string; + t?: (key: string) => string; } & React.ComponentProps & { labelProps?: React.ComponentProps; }; const InputField = forwardRef(function InputField(props, ref) { const id = useId(); - const { t } = useLocale(); + const { t: _t } = useLocale(); + const t = props.t || _t; const methods = useFormContext(); const { label = t(props.name), @@ -74,6 +165,8 @@ const InputField = forwardRef(function InputF hintErrors, labelSrOnly, containerClassName, + // eslint-disable-next-line @typescript-eslint/no-unused-vars + t: __t, ...passThrough } = props; @@ -124,58 +217,13 @@ const InputField = forwardRef(function InputF ) : ( )} - {methods?.formState?.errors[props.name] && methods?.formState?.errors[props.name].message && ( + {methods.formState.errors[props.name]?.message && (
{methods.formState.errors[props.name].message}
)} - {hintErrors && ( -
- {methods?.formState?.errors[props.name] !== undefined ? ( -
    - {hintErrors.map((errorKey) => { - const error = - methods?.formState?.errors[props.name][errorKey] || - methods?.formState?.errors[props.name].message; - const submitted = methods?.formState.isSubmitted; - return ( -
  • - {error !== undefined ? ( - submitted ? ( - - ) : ( - - ) - ) : ( - - )} - {error?.message || t(`${props.name}_hint_${errorKey}`)} -
  • - ); - })} -
- ) : ( -
    - {hintErrors.map((errorKey) => { - const dirty = methods?.formState.dirtyFields[props.name]; - return ( -
  • - {!!dirty ? ( - - ) : ( - - )} - {t(`${props.name}_hint_${errorKey}`)} -
  • - ); - })} -
- )} -
- )} + {hint &&
{hint}
} ); @@ -237,6 +285,7 @@ export const TextArea = forwardRef(function type TextAreaFieldProps = { label?: ReactNode; + t?: (key: string) => string; } & React.ComponentProps & { labelProps?: React.ComponentProps; }; @@ -246,7 +295,8 @@ export const TextAreaField = forwardRef ref ) { const id = useId(); - const { t } = useLocale(); + const { t: _t } = useLocale(); + const t = props.t || _t; const methods = useFormContext(); const { label = t(props.name as string), diff --git a/yarn.lock b/yarn.lock index 10fee8449b..11e90dedf9 100644 --- a/yarn.lock +++ b/yarn.lock @@ -19522,11 +19522,6 @@ react-gtm-module@^2.0.11: resolved "https://registry.yarnpkg.com/react-gtm-module/-/react-gtm-module-2.0.11.tgz#14484dac8257acd93614e347c32da9c5ac524206" integrity sha512-8gyj4TTxeP7eEyc2QKawEuQoAZdjKvMY4pgWfycGmqGByhs17fR+zEBs0JUDq4US/l+vbTl+6zvUIx27iDo/Vw== -react-hook-form@^7.16.2: - version "7.33.1" - resolved "https://registry.yarnpkg.com/react-hook-form/-/react-hook-form-7.33.1.tgz#8c4410e3420788d3b804d62cc4c142915c2e46d0" - integrity sha512-ydTfTxEJdvgjCZBj5DDXRc58oTEfnFupEwwTAQ9FSKzykEJkX+3CiAkGtAMiZG7IPWHuzgT6AOBfogiKhUvKgg== - react-hook-form@^7.31.1: version "7.31.1" resolved "https://registry.yarnpkg.com/react-hook-form/-/react-hook-form-7.31.1.tgz#16c357dd366bc226172e6acbb5a1672873bbfb28"