"use client"; import { useReducer } from "react"; import { FormControl, FormField, FormItem, FormLabel, FormMessage, } from "../ui/form"; // Shadcn UI import import { Input } from "../ui/input"; // Shandcn UI Input import { UseFormReturn } from "react-hook-form"; type TextInputProps = { form: UseFormReturn; name: string; label: string; placeholder: string; }; // Brazilian currency config const moneyFormatter = Intl.NumberFormat("pt-BR", { currency: "BRL", currencyDisplay: "symbol", currencySign: "standard", style: "currency", minimumFractionDigits: 2, maximumFractionDigits: 2, }); export default function MoneyInput(props: TextInputProps) { const initialValue = props.form.getValues()[props.name] ? moneyFormatter.format(props.form.getValues()[props.name]) : ""; const [value, setValue] = useReducer((_: any, next: string) => { const digits = next.replace(/\D/g, ""); return moneyFormatter.format(Number(digits) / 100); }, initialValue); function handleChange(realChangeFn: Function, formattedValue: string) { const digits = formattedValue.replace(/\D/g, ""); const realValue = Number(digits) / 100; realChangeFn(realValue); } return ( { field.value = value; const _change = field.onChange; return ( {props.label} { setValue(ev.target.value); handleChange(_change, ev.target.value); }} value={value} /> ); }} /> ); }