Merge pull request #239 from cosmos/formgen-field-amount
Add field amount
This commit is contained in:
commit
a166a4b488
49
components/forms/CreateTxForm/Fields/FieldAmount.tsx
Normal file
49
components/forms/CreateTxForm/Fields/FieldAmount.tsx
Normal 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>
|
||||
)}
|
||||
/>
|
||||
</>
|
||||
);
|
||||
}
|
||||
@ -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`);
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user