From af1da4310aa9d26e42182bc0969be871725fec31 Mon Sep 17 00:00:00 2001 From: abefernan <44572727+abefernan@users.noreply.github.com> Date: Wed, 31 Jul 2024 17:02:58 +0200 Subject: [PATCH 1/2] Add FieldVoteOption --- .../CreateTxForm/Fields/FieldVoteOption.tsx | 69 +++++++++++++++++++ components/forms/CreateTxForm/Fields/index.ts | 1 + 2 files changed, 70 insertions(+) create mode 100644 components/forms/CreateTxForm/Fields/FieldVoteOption.tsx diff --git a/components/forms/CreateTxForm/Fields/FieldVoteOption.tsx b/components/forms/CreateTxForm/Fields/FieldVoteOption.tsx new file mode 100644 index 0000000..76315d4 --- /dev/null +++ b/components/forms/CreateTxForm/Fields/FieldVoteOption.tsx @@ -0,0 +1,69 @@ +import { FormControl, FormField, FormItem, FormLabel, FormMessage } from "@/components/ui/form"; +import { + Select, + SelectContent, + SelectItem, + SelectTrigger, + SelectValue, +} from "@/components/ui/select"; +import { prettyFieldName } from "@/lib/form"; +import { printVoteOption, voteOptions } from "@/lib/gov"; +import { voteOptionFromJSON } from "cosmjs-types/cosmos/gov/v1beta1/gov"; +import * as z from "zod"; +import type { FieldProps } from "./types"; + +const selectVoteOptions = voteOptions.map((opt) => { + const voteOptionObj = voteOptionFromJSON(opt); + + return { + label: printVoteOption(voteOptionObj), + value: voteOptionObj, + }; +}); + +const isFieldVoteOption = (fieldName: string) => fieldName === "option"; + +export const getFieldVoteOption = (fieldName: string) => + isFieldVoteOption(fieldName) ? FieldVoteOption : null; + +export const getFieldVoteOptionSchema = (fieldName: string) => + isFieldVoteOption(fieldName) + ? z.coerce + .number({ + invalid_type_error: "Invalid vote option", + required_error: "Invalid vote option", + }) + .nonnegative("Invalid vote option") + : null; + +export default function FieldVoteOption({ form, fieldFormName }: FieldProps) { + return ( + ( + + {prettyFieldName(fieldFormName)} + + + + )} + /> + ); +} diff --git a/components/forms/CreateTxForm/Fields/index.ts b/components/forms/CreateTxForm/Fields/index.ts index 193c24b..e687a79 100644 --- a/components/forms/CreateTxForm/Fields/index.ts +++ b/components/forms/CreateTxForm/Fields/index.ts @@ -6,3 +6,4 @@ export * from "./FieldDescription"; export * from "./FieldNumber"; export * from "./FieldString"; export * from "./FieldTimeoutHeight"; +export * from "./FieldVoteOption"; From 60da10d80d68e4ee837f75839148cb7555cc5b7c Mon Sep 17 00:00:00 2001 From: abefernan <44572727+abefernan@users.noreply.github.com> Date: Wed, 31 Jul 2024 17:03:08 +0200 Subject: [PATCH 2/2] Add FieldVoteOption to form helpers --- lib/form.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/form.ts b/lib/form.ts index ac6a7b5..97e1254 100644 --- a/lib/form.ts +++ b/lib/form.ts @@ -15,6 +15,8 @@ import { getFieldStringSchema, getFieldTimeoutHeight, getFieldTimeoutHeightSchema, + getFieldVoteOption, + getFieldVoteOptionSchema, } from "@/components/forms/CreateTxForm/Fields"; import { FieldSchemaInput } from "@/components/forms/CreateTxForm/Fields/types"; import { z } from "zod"; @@ -36,6 +38,7 @@ export const getField = (fieldName: string) => getFieldTimeoutHeight(fieldName) || getFieldDescription(fieldName) || getFieldCommission(fieldName) || + getFieldVoteOption(fieldName) || null; const getFieldSchema = (fieldName: string, schemaInput: FieldSchemaInput) => @@ -47,6 +50,7 @@ const getFieldSchema = (fieldName: string, schemaInput: FieldSchemaInput) => getFieldTimeoutHeightSchema(fieldName) || getFieldDescriptionSchema(fieldName) || getFieldCommissionSchema(fieldName) || + getFieldVoteOptionSchema(fieldName) || null; export const getMsgSchema = (fieldNames: readonly string[], schemaInput: FieldSchemaInput) => {