Skip to content

Commit

Permalink
Merge pull request #246 from cosmos/formgen-field-description
Browse files Browse the repository at this point in the history
Add field description
  • Loading branch information
abefernan authored Jul 31, 2024
2 parents 527dda2 + 75294eb commit d80535f
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 0 deletions.
111 changes: 111 additions & 0 deletions components/forms/CreateTxForm/Fields/FieldDescription.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
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 isFieldDescription = (fieldName: string) => fieldName === "description";

export const getFieldDescription = (fieldName: string) =>
isFieldDescription(fieldName) ? FieldDescription : null;

export const getFieldDescriptionSchema = (fieldName: string) =>
isFieldDescription(fieldName)
? z.object({
moniker: z
.string({ invalid_type_error: "Must be a string", required_error: "Required" })
.trim()
.min(1, "Required"),
identity: z
.string({ invalid_type_error: "Must be a string", required_error: "Required" })
.trim()
.min(1, "Required"),
website: z
.string({ invalid_type_error: "Must be a string", required_error: "Required" })
.trim()
.url("Must be a url")
.min(1, "Required"),
securityContact: z
.string({ invalid_type_error: "Must be a string", required_error: "Required" })
.trim()
.min(1, "Required"),
details: z
.string({ invalid_type_error: "Must be a string", required_error: "Required" })
.trim()
.min(1, "Required"),
})
: null;

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

return (
<>
<FormField
control={form.control}
name={`${fieldFormName}.moniker`}
render={({ field }) => (
<FormItem>
<FormLabel>{`${prettyLabel} Moniker`}</FormLabel>
<FormControl>
<Input placeholder="Enter moniker" {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name={`${fieldFormName}.identity`}
render={({ field }) => (
<FormItem>
<FormLabel>{`${prettyLabel} Identity`}</FormLabel>
<FormControl>
<Input placeholder="Enter identity" {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name={`${fieldFormName}.website`}
render={({ field }) => (
<FormItem>
<FormLabel>{`${prettyLabel} Website`}</FormLabel>
<FormControl>
<Input placeholder="Enter website" {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name={`${fieldFormName}.securityContact`}
render={({ field }) => (
<FormItem>
<FormLabel>{`${prettyLabel} Security Contact`}</FormLabel>
<FormControl>
<Input placeholder="Enter security contact" {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name={`${fieldFormName}.details`}
render={({ field }) => (
<FormItem>
<FormLabel>{`${prettyLabel} Details`}</FormLabel>
<FormControl>
<Input placeholder="Enter details" {...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 "./FieldDescription";
export * from "./FieldNumber";
export * from "./FieldString";
export * from "./FieldTimeoutHeight";
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,
getFieldDescription,
getFieldDescriptionSchema,
getFieldNumber,
getFieldNumberSchema,
getFieldString,
Expand All @@ -30,6 +32,7 @@ export const getField = (fieldName: string) =>
getFieldNumber(fieldName) ||
getFieldBoolean(fieldName) ||
getFieldTimeoutHeight(fieldName) ||
getFieldDescription(fieldName) ||
null;

const getFieldSchema = (fieldName: string, schemaInput: FieldSchemaInput) =>
Expand All @@ -39,6 +42,7 @@ const getFieldSchema = (fieldName: string, schemaInput: FieldSchemaInput) =>
getFieldNumberSchema(fieldName) ||
getFieldBooleanSchema(fieldName) ||
getFieldTimeoutHeightSchema(fieldName) ||
getFieldDescriptionSchema(fieldName) ||
null;

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

0 comments on commit d80535f

Please sign in to comment.