Skip to content

Commit

Permalink
Merge pull request #242 from cosmos/formfield-string
Browse files Browse the repository at this point in the history
Add field string
  • Loading branch information
abefernan authored Jul 26, 2024
2 parents 968cf8e + 42b617e commit 3772ee4
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 2 deletions.
54 changes: 54 additions & 0 deletions components/forms/CreateTxForm/Fields/FieldString.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
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 isFieldString = (fieldName: string) =>
["sourcePort", "sourceChannel", "memo"].includes(fieldName);

const isOptionalFieldString = (fieldName: string) => fieldName === "label";

export const getFieldString = (fieldName: string) =>
isFieldString(fieldName) || isOptionalFieldString(fieldName) ? FieldString : null;

export const getFieldStringSchema = (fieldName: string) => {
if (!isFieldString(fieldName) && !isOptionalFieldString(fieldName)) {
return null;
}

const zodString = z
.string({ invalid_type_error: "Must be a string", required_error: "Required" })
.trim()
.min(1, "Required");

if (isFieldString(fieldName)) {
return zodString;
}

if (isOptionalFieldString(fieldName)) {
return z.optional(zodString);
}

return null;
};

export default function FieldString({ 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,2 +1,3 @@
export * from "./FieldAddress";
export * from "./FieldAmount";
export * from "./FieldString";
9 changes: 7 additions & 2 deletions lib/form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import {
getFieldAddressSchema,
getFieldAmount,
getFieldAmountSchema,
getFieldString,
getFieldStringSchema,
} from "@/components/forms/CreateTxForm/Fields";
import { FieldSchemaInput } from "@/components/forms/CreateTxForm/Fields/types";
import { z } from "zod";
Expand All @@ -16,10 +18,13 @@ export const prettyFieldName = (fieldName: string) => {
};

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

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

export const getMsgSchema = (fieldNames: readonly string[], schemaInput: FieldSchemaInput) => {
const fieldEntries = fieldNames.map((fieldName) => [
Expand Down

0 comments on commit 3772ee4

Please sign in to comment.