-
Notifications
You must be signed in to change notification settings - Fork 110
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 #243 from cosmos/formfield-number
Add field number
- Loading branch information
Showing
3 changed files
with
77 additions
and
1 deletion.
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,68 @@ | ||
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 isFieldNumberBigInt = (fieldName: string) => | ||
["proposalId", "creationHeight", "endTime", "timeoutTimestamp", "codeId"].includes(fieldName); | ||
|
||
const isFieldNumberString = (fieldName: string) => | ||
["minSelfDelegation", "commissionRate"].includes(fieldName); | ||
|
||
export const getFieldNumber = (fieldName: string) => | ||
isFieldNumberBigInt(fieldName) || isFieldNumberString(fieldName) ? FieldNumber : null; | ||
|
||
export const getFieldNumberSchema = (fieldName: string) => { | ||
if (isFieldNumberBigInt(fieldName)) { | ||
return z | ||
.any() | ||
.transform((value) => { | ||
try { | ||
return BigInt(value); | ||
} catch (error) { | ||
return value; | ||
} | ||
}) | ||
.pipe( | ||
z | ||
.bigint({ invalid_type_error: "Must be an integer", required_error: "Required" }) | ||
.positive("Must be positive"), | ||
); | ||
} | ||
|
||
if (isFieldNumberString(fieldName)) { | ||
return z.coerce | ||
.number({ invalid_type_error: "Must be a number", required_error: "Required" }) | ||
.positive("Must be positive") | ||
.transform((value) => { | ||
try { | ||
return String(value); | ||
} catch (error) { | ||
return value; | ||
} | ||
}); | ||
} | ||
|
||
return null; | ||
}; | ||
|
||
export default function FieldNumber({ 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,3 +1,4 @@ | ||
export * from "./FieldAddress"; | ||
export * from "./FieldAmount"; | ||
export * from "./FieldNumber"; | ||
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