From 045bded8b160b0b9121f4337176f680f247e428d Mon Sep 17 00:00:00 2001 From: abefernan <44572727+abefernan@users.noreply.github.com> Date: Mon, 22 Jul 2024 12:30:35 +0200 Subject: [PATCH 1/2] Add address field and schema --- .../CreateTxForm/Fields/FieldAddress.tsx | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 components/forms/CreateTxForm/Fields/FieldAddress.tsx diff --git a/components/forms/CreateTxForm/Fields/FieldAddress.tsx b/components/forms/CreateTxForm/Fields/FieldAddress.tsx new file mode 100644 index 00000000..a6fe54f8 --- /dev/null +++ b/components/forms/CreateTxForm/Fields/FieldAddress.tsx @@ -0,0 +1,39 @@ +import { FormControl, FormField, FormItem, FormLabel, FormMessage } from "@/components/ui/form"; +import { Input } from "@/components/ui/input"; +import { useChains } from "@/context/ChainsContext"; +import { exampleAddress } from "@/lib/displayHelpers"; +import { prettyFieldName } from "@/lib/form"; +import * as z from "zod"; +import type { FieldProps, FieldSchemaInput } from "./types"; + +export const getFieldAddressSchema = ({ chain }: FieldSchemaInput) => { + if (!chain) { + throw new Error("Could not get field address schema because of missing chain parameter"); + } + + return z + .string({ invalid_type_error: "Must be a string", required_error: "Required" }) + .trim() + .min(1, "Required") + .startsWith(chain.addressPrefix, `Invalid prefix for ${chain.chainDisplayName}`); +}; + +export default function FieldAddress({ form, fieldFormName }: FieldProps) { + const { chain } = useChains(); + + return ( + ( + + {prettyFieldName(fieldFormName)} + + + + + + )} + /> + ); +} From b7ee35c0d001c74bf2cdb125d878af3b0863d45e Mon Sep 17 00:00:00 2001 From: abefernan <44572727+abefernan@users.noreply.github.com> Date: Mon, 22 Jul 2024 12:31:10 +0200 Subject: [PATCH 2/2] Add address field getters --- lib/form.ts | 29 +++++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/lib/form.ts b/lib/form.ts index d48757cf..e635a89f 100644 --- a/lib/form.ts +++ b/lib/form.ts @@ -1,3 +1,6 @@ +import FieldAddress, { + getFieldAddressSchema, +} from "@/components/forms/CreateTxForm/Fields/FieldAddress"; import { FieldSchemaInput } from "@/components/forms/CreateTxForm/Fields/types"; import { z } from "zod"; @@ -8,14 +11,28 @@ export const prettyFieldName = (fieldName: string) => { return capitalizedName; }; -export const getField = (_fieldName: string) => { - /* Will return a FormField component per fieldName */ - return () => null; +export const getField = (fieldName: string) => { + switch (fieldName) { + case "fromAddress": + case "toAddress": + case "delegatorAddress": + case "validatorAddress": + return FieldAddress; + default: + return () => null; + } }; -const getFieldSchema = (_fieldName: string) => { - /* Will return a zod schema getter per fieldName */ - return (_schemaInput: unknown) => null; +const getFieldSchema = (fieldName: string) => { + switch (fieldName) { + case "fromAddress": + case "toAddress": + case "delegatorAddress": + case "validatorAddress": + return getFieldAddressSchema; + default: + throw new Error(`No schema found for ${fieldName} field`); + } }; export const getMsgSchema = (fieldNames: readonly string[], schemaInput: FieldSchemaInput) => {