Skip to content

Commit

Permalink
Merge pull request #237 from cosmos/formgen-form-utils
Browse files Browse the repository at this point in the history
Add form utils
  • Loading branch information
abefernan authored Jul 23, 2024
2 parents 08e85b9 + 56a4c93 commit c902ec6
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
12 changes: 12 additions & 0 deletions components/forms/CreateTxForm/Fields/types.ts
Original file line number Diff line number Diff line change
@@ -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<any>;
readonly fieldFormName: string;
}

export type FieldSchemaInput = {
readonly chain?: ChainInfo;
};
28 changes: 28 additions & 0 deletions lib/form.ts
Original file line number Diff line number Diff line change
@@ -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));
};

0 comments on commit c902ec6

Please sign in to comment.