Skip to content

Commit

Permalink
Merge pull request #239 from cosmos/formgen-field-amount
Browse files Browse the repository at this point in the history
Add field amount
  • Loading branch information
abefernan authored Jul 23, 2024
2 parents 9d2e860 + 32e828a commit a166a4b
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
49 changes: 49 additions & 0 deletions components/forms/CreateTxForm/Fields/FieldAmount.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { FormControl, FormField, FormItem, FormLabel, FormMessage } from "@/components/ui/form";
import { Input } from "@/components/ui/input";
import { prettyFieldName } from "@/lib/form";
import * as z from "zod";
import type { FieldProps } from "./types";

export const getFieldAmountSchema = () =>
z.object({
denom: z
.string({ invalid_type_error: "Must be a string", required_error: "Required" })
.trim()
.min(1, "Required"),
amount: z
.number({ invalid_type_error: "Must be a number", required_error: "Required" })
.positive("Must be positive"),
});

export default function FieldAmount({ form, fieldFormName }: FieldProps) {
return (
<>
<FormField
control={form.control}
name={`${fieldFormName}.denom`}
render={({ field }) => (
<FormItem>
<FormLabel>{"Denom" + prettyFieldName(fieldFormName)}</FormLabel>
<FormControl>
<Input placeholder="Enter denom" {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name={`${fieldFormName}.amount`}
render={({ field }) => (
<FormItem>
<FormLabel>{prettyFieldName(fieldFormName)}</FormLabel>
<FormControl>
<Input placeholder="Enter amount" {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
</>
);
}
7 changes: 7 additions & 0 deletions lib/form.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import FieldAddress, {
getFieldAddressSchema,
} from "@/components/forms/CreateTxForm/Fields/FieldAddress";
import FieldAmount, {
getFieldAmountSchema,
} from "@/components/forms/CreateTxForm/Fields/FieldAmount";
import { FieldSchemaInput } from "@/components/forms/CreateTxForm/Fields/types";
import { z } from "zod";

Expand All @@ -18,6 +21,8 @@ export const getField = (fieldName: string) => {
case "delegatorAddress":
case "validatorAddress":
return FieldAddress;
case "amount":
return FieldAmount;
default:
return () => null;
}
Expand All @@ -30,6 +35,8 @@ const getFieldSchema = (fieldName: string) => {
case "delegatorAddress":
case "validatorAddress":
return getFieldAddressSchema;
case "amount":
return getFieldAmountSchema;
default:
throw new Error(`No schema found for ${fieldName} field`);
}
Expand Down

0 comments on commit a166a4b

Please sign in to comment.