Merge pull request #247 from cosmos/formgen-field-comission

Add field comission
This commit is contained in:
Abel Fernández
2024-07-31 17:06:37 +02:00
committed by GitHub
3 changed files with 99 additions and 0 deletions
@@ -0,0 +1,94 @@
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";
const isFieldCommission = (fieldName: string) => fieldName === "commission";
export const getFieldCommission = (fieldName: string) =>
isFieldCommission(fieldName) ? FieldCommission : null;
export const getFieldCommissionSchema = (fieldName: string) =>
isFieldCommission(fieldName)
? z.object({
rate: z.coerce
.number({ invalid_type_error: "Must be a number", required_error: "Required" })
.positive("Must be positive")
.transform((value) => {
try {
return String(value);
} catch (error) {
return value;
}
}),
maxRate: z.coerce
.number({ invalid_type_error: "Must be a number", required_error: "Required" })
.positive("Must be positive")
.transform((value) => {
try {
return String(value);
} catch (error) {
return value;
}
}),
maxChangeRate: z.coerce
.number({ invalid_type_error: "Must be a number", required_error: "Required" })
.positive("Must be positive")
.transform((value) => {
try {
return String(value);
} catch (error) {
return value;
}
}),
})
: null;
export default function FieldCommission({ form, fieldFormName }: FieldProps) {
const prettyLabel = prettyFieldName(fieldFormName);
return (
<>
<FormField
control={form.control}
name={`${fieldFormName}.rate`}
render={({ field }) => (
<FormItem>
<FormLabel>{`${prettyLabel} Rate`}</FormLabel>
<FormControl>
<Input placeholder="Enter rate" {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name={`${fieldFormName}.maxRate`}
render={({ field }) => (
<FormItem>
<FormLabel>{`${prettyLabel} Max Rate`}</FormLabel>
<FormControl>
<Input placeholder="Enter max rate" {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name={`${fieldFormName}.maxChangeRate`}
render={({ field }) => (
<FormItem>
<FormLabel>{`${prettyLabel} Max Change Rate`}</FormLabel>
<FormControl>
<Input placeholder="Enter max change rate" {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
</>
);
}
@@ -1,6 +1,7 @@
export * from "./FieldAddress";
export * from "./FieldAmount";
export * from "./FieldBoolean";
export * from "./FieldCommission";
export * from "./FieldDescription";
export * from "./FieldNumber";
export * from "./FieldString";
+4
View File
@@ -5,6 +5,8 @@ import {
getFieldAmountSchema,
getFieldBoolean,
getFieldBooleanSchema,
getFieldCommission,
getFieldCommissionSchema,
getFieldDescription,
getFieldDescriptionSchema,
getFieldNumber,
@@ -33,6 +35,7 @@ export const getField = (fieldName: string) =>
getFieldBoolean(fieldName) ||
getFieldTimeoutHeight(fieldName) ||
getFieldDescription(fieldName) ||
getFieldCommission(fieldName) ||
null;
const getFieldSchema = (fieldName: string, schemaInput: FieldSchemaInput) =>
@@ -43,6 +46,7 @@ const getFieldSchema = (fieldName: string, schemaInput: FieldSchemaInput) =>
getFieldBooleanSchema(fieldName) ||
getFieldTimeoutHeightSchema(fieldName) ||
getFieldDescriptionSchema(fieldName) ||
getFieldCommissionSchema(fieldName) ||
null;
export const getMsgSchema = (fieldNames: readonly string[], schemaInput: FieldSchemaInput) => {