Skip to content

Commit

Permalink
Merge pull request #247 from cosmos/formgen-field-comission
Browse files Browse the repository at this point in the history
Add field comission
  • Loading branch information
abefernan authored Jul 31, 2024
2 parents d80535f + c230ec9 commit ac1cfe0
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 0 deletions.
94 changes: 94 additions & 0 deletions components/forms/CreateTxForm/Fields/FieldCommission.tsx
Original file line number Diff line number Diff line change
@@ -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 change: 1 addition & 0 deletions components/forms/CreateTxForm/Fields/index.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down
4 changes: 4 additions & 0 deletions lib/form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import {
getFieldAmountSchema,
getFieldBoolean,
getFieldBooleanSchema,
getFieldCommission,
getFieldCommissionSchema,
getFieldDescription,
getFieldDescriptionSchema,
getFieldNumber,
Expand Down Expand Up @@ -33,6 +35,7 @@ export const getField = (fieldName: string) =>
getFieldBoolean(fieldName) ||
getFieldTimeoutHeight(fieldName) ||
getFieldDescription(fieldName) ||
getFieldCommission(fieldName) ||
null;

const getFieldSchema = (fieldName: string, schemaInput: FieldSchemaInput) =>
Expand All @@ -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) => {
Expand Down

0 comments on commit ac1cfe0

Please sign in to comment.