Merge pull request #239 from cosmos/formgen-field-amount

Add field amount
This commit is contained in:
Abel Fernández 2024-07-23 11:48:27 +02:00 committed by GitHub
commit a166a4b488
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 56 additions and 0 deletions

View File

@ -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>
)}
/>
</>
);
}

View File

@ -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";
@ -18,6 +21,8 @@ export const getField = (fieldName: string) => {
case "delegatorAddress":
case "validatorAddress":
return FieldAddress;
case "amount":
return FieldAmount;
default:
return () => null;
}
@ -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`);
}