import { cn } from "@/lib/utils"; import { FormControl, FormDescription, FormField, FormItem, InputProps } from "@/ui"; import { CurrencyData, MoneyValue } from "@shared/contexts"; import { cva, type VariantProps } from "class-variance-authority"; import * as React from "react"; import CurrencyInput from "react-currency-input-field"; import { FieldPath, FieldValues, UseControllerProps, useFormContext } from "react-hook-form"; import { FormErrorMessage } from "./FormErrorMessage"; import { FormLabel, FormLabelProps } from "./FormLabel"; import { FormInputProps, FormInputWithIconProps } from "./FormProps"; export const formCurrencyFieldVariants = cva( "flex h-10 w-full rounded-md bg-background px-3 py-2 text-sm placeholder:text-muted-foreground focus-visible:outline-none disabled:cursor-not-allowed disabled:opacity-50", { variants: { variant: { default: "border border-input ring-offset-background focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 ", outline: "ring-offset-background focus-visible:ring-1 focus-visible:ring-ring focus-visible:ring-offset-2 ", }, }, defaultVariants: { variant: "default", }, } ); export type FormCurrencyFieldProps< TFieldValues extends FieldValues = FieldValues, TName extends FieldPath = FieldPath > = { button?: (props?: React.PropsWithChildren) => React.ReactNode; } & InputProps & FormInputProps & Partial & FormInputWithIconProps & UseControllerProps & VariantProps & { currency: CurrencyData; precision: number; }; export const FormCurrencyField = React.forwardRef( (props, ref) => { const { name, label, hint, description, className, disabled, defaultValue, rules, precision, currency, variant, } = props; const { control } = useFormContext(); const transformToInput = (value: any) => { if (typeof value !== "object") { return value; } const moneyOrError = MoneyValue.create(value); if (moneyOrError.isFailure) { throw moneyOrError.error; } return moneyOrError.object .convertPrecision(precision ?? value.precision) .toUnit() .toString(); }; return ( { return ( {label && } { // "value" ya viene con los "0" de la precisión field.onChange(value ?? ""); }} /> {description && {description}} ); }} /> ); } ); FormCurrencyField.displayName = "FormCurrencyField";