Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add field number #243

Merged
merged 2 commits into from
Jul 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions components/forms/CreateTxForm/Fields/FieldNumber.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
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 isFieldNumberBigInt = (fieldName: string) =>
["proposalId", "creationHeight", "endTime", "timeoutTimestamp", "codeId"].includes(fieldName);

const isFieldNumberString = (fieldName: string) =>
["minSelfDelegation", "commissionRate"].includes(fieldName);

export const getFieldNumber = (fieldName: string) =>
isFieldNumberBigInt(fieldName) || isFieldNumberString(fieldName) ? FieldNumber : null;

export const getFieldNumberSchema = (fieldName: string) => {
if (isFieldNumberBigInt(fieldName)) {
return z
.any()
.transform((value) => {
try {
return BigInt(value);
} catch (error) {
return value;
}
})
.pipe(
z
.bigint({ invalid_type_error: "Must be an integer", required_error: "Required" })
.positive("Must be positive"),
);
}

if (isFieldNumberString(fieldName)) {
return 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;
}
});
}

return null;
};

export default function FieldNumber({ form, fieldFormName }: FieldProps) {
const prettyLabel = prettyFieldName(fieldFormName);

return (
<FormField
control={form.control}
name={fieldFormName}
render={({ field }) => (
<FormItem>
<FormLabel>{prettyLabel}</FormLabel>
<FormControl>
<Input placeholder={`Enter ${prettyLabel.toLowerCase()}`} {...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,3 +1,4 @@
export * from "./FieldAddress";
export * from "./FieldAmount";
export * from "./FieldNumber";
export * from "./FieldString";
9 changes: 8 additions & 1 deletion lib/form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import {
getFieldAddressSchema,
getFieldAmount,
getFieldAmountSchema,
getFieldNumber,
getFieldNumberSchema,
getFieldString,
getFieldStringSchema,
} from "@/components/forms/CreateTxForm/Fields";
Expand All @@ -18,12 +20,17 @@ export const prettyFieldName = (fieldName: string) => {
};

export const getField = (fieldName: string) =>
getFieldAddress(fieldName) || getFieldAmount(fieldName) || getFieldString(fieldName) || null;
getFieldAddress(fieldName) ||
getFieldAmount(fieldName) ||
getFieldString(fieldName) ||
getFieldNumber(fieldName) ||
null;

const getFieldSchema = (fieldName: string, schemaInput: FieldSchemaInput) =>
getFieldAddressSchema(fieldName, schemaInput) ||
getFieldAmountSchema(fieldName) ||
getFieldStringSchema(fieldName) ||
getFieldNumberSchema(fieldName) ||
null;

export const getMsgSchema = (fieldNames: readonly string[], schemaInput: FieldSchemaInput) => {
Expand Down
Loading