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 description #246

Merged
merged 2 commits into from
Jul 31, 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
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
Loading