-
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 #247 from cosmos/formgen-field-comission
Add field comission
- Loading branch information
Showing
3 changed files
with
99 additions
and
0 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,94 @@ | ||
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 isFieldCommission = (fieldName: string) => fieldName === "commission"; | ||
|
||
export const getFieldCommission = (fieldName: string) => | ||
isFieldCommission(fieldName) ? FieldCommission : null; | ||
|
||
export const getFieldCommissionSchema = (fieldName: string) => | ||
isFieldCommission(fieldName) | ||
? z.object({ | ||
rate: 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; | ||
} | ||
}), | ||
maxRate: 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; | ||
} | ||
}), | ||
maxChangeRate: 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; | ||
} | ||
}), | ||
}) | ||
: null; | ||
|
||
export default function FieldCommission({ form, fieldFormName }: FieldProps) { | ||
const prettyLabel = prettyFieldName(fieldFormName); | ||
|
||
return ( | ||
<> | ||
<FormField | ||
control={form.control} | ||
name={`${fieldFormName}.rate`} | ||
render={({ field }) => ( | ||
<FormItem> | ||
<FormLabel>{`${prettyLabel} Rate`}</FormLabel> | ||
<FormControl> | ||
<Input placeholder="Enter rate" {...field} /> | ||
</FormControl> | ||
<FormMessage /> | ||
</FormItem> | ||
)} | ||
/> | ||
<FormField | ||
control={form.control} | ||
name={`${fieldFormName}.maxRate`} | ||
render={({ field }) => ( | ||
<FormItem> | ||
<FormLabel>{`${prettyLabel} Max Rate`}</FormLabel> | ||
<FormControl> | ||
<Input placeholder="Enter max rate" {...field} /> | ||
</FormControl> | ||
<FormMessage /> | ||
</FormItem> | ||
)} | ||
/> | ||
<FormField | ||
control={form.control} | ||
name={`${fieldFormName}.maxChangeRate`} | ||
render={({ field }) => ( | ||
<FormItem> | ||
<FormLabel>{`${prettyLabel} Max Change Rate`}</FormLabel> | ||
<FormControl> | ||
<Input placeholder="Enter max change rate" {...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
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