Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add field address #238

Merged
merged 2 commits into from
Jul 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions components/forms/CreateTxForm/Fields/FieldAddress.tsx
Original file line number Diff line number Diff line change
@@ -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 (
<FormField
control={form.control}
name={fieldFormName}
render={({ field }) => (
<FormItem>
<FormLabel>{prettyFieldName(fieldFormName)}</FormLabel>
<FormControl>
<Input placeholder={`E.g. "${exampleAddress(0, chain.addressPrefix)}"`} {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
);
}
29 changes: 23 additions & 6 deletions lib/form.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand All @@ -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) => {
Expand Down
Loading