-
Notifications
You must be signed in to change notification settings - Fork 109
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #242 from cosmos/formfield-string
Add field string
- Loading branch information
Showing
3 changed files
with
62 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
)} | ||
/> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from "./FieldAddress"; | ||
export * from "./FieldAmount"; | ||
export * from "./FieldString"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters