From 7b4bc27df067757a5e1f35760c556b9704aee5bd Mon Sep 17 00:00:00 2001 From: abefernan <44572727+abefernan@users.noreply.github.com> Date: Mon, 22 Jul 2024 12:22:41 +0200 Subject: [PATCH 1/2] Add future Fields' props and schema input --- components/forms/CreateTxForm/Fields/types.ts | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 components/forms/CreateTxForm/Fields/types.ts diff --git a/components/forms/CreateTxForm/Fields/types.ts b/components/forms/CreateTxForm/Fields/types.ts new file mode 100644 index 00000000..4ed707c3 --- /dev/null +++ b/components/forms/CreateTxForm/Fields/types.ts @@ -0,0 +1,12 @@ +import { ChainInfo } from "@/context/ChainsContext/types"; +import { UseFormReturn } from "react-hook-form"; + +export interface FieldProps { + // eslint-disable-next-line @typescript-eslint/no-explicit-any + readonly form: UseFormReturn; + readonly fieldFormName: string; +} + +export type FieldSchemaInput = { + readonly chain?: ChainInfo; +}; From 56a4c93cc14989850b3cd706c9d55629593c8a6e Mon Sep 17 00:00:00 2001 From: abefernan <44572727+abefernan@users.noreply.github.com> Date: Mon, 22 Jul 2024 12:22:56 +0200 Subject: [PATCH 2/2] Add initial form utils --- lib/form.ts | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 lib/form.ts diff --git a/lib/form.ts b/lib/form.ts new file mode 100644 index 00000000..d48757cf --- /dev/null +++ b/lib/form.ts @@ -0,0 +1,28 @@ +import { FieldSchemaInput } from "@/components/forms/CreateTxForm/Fields/types"; +import { z } from "zod"; + +export const prettyFieldName = (fieldName: string) => { + const splitName = fieldName.split(/(?=[A-Z])/).join(" "); + const capitalizedName = splitName.charAt(0).toUpperCase() + splitName.slice(1); + + return capitalizedName; +}; + +export const getField = (_fieldName: string) => { + /* Will return a FormField component per fieldName */ + return () => null; +}; + +const getFieldSchema = (_fieldName: string) => { + /* Will return a zod schema getter per fieldName */ + return (_schemaInput: unknown) => null; +}; + +export const getMsgSchema = (fieldNames: readonly string[], schemaInput: FieldSchemaInput) => { + const fieldEntries = fieldNames.map((fieldName) => [ + fieldName, + getFieldSchema(fieldName)(schemaInput), + ]); + + return z.object(Object.fromEntries(fieldEntries)); +};