import * as React from "react"; import { cn } from "@/lib/utils"; import { FormControl, FormDescription, FormField, FormItem, Input, InputProps } from "@/ui"; import { Quantity } from "@shared/contexts"; import { cva, type VariantProps } from "class-variance-authority"; import { FieldPath, FieldValues, UseControllerProps, useFormContext } from "react-hook-form"; import { FormErrorMessage } from "./FormErrorMessage"; import { FormLabel, FormLabelProps } from "./FormLabel"; import { FormInputProps, FormInputWithIconProps } from "./FormProps"; const formQuantityFieldVariants = cva( "text-right [appearance:textfield] [&::-webkit-outer-spin-button]:appearance-none [&::-webkit-inner-spin-button]:appearance-none", { variants: { variant: { default: "", outline: "border-0 focus-visible:border focus-visible:border-input focus-visible:ring-0 focus-visible:ring-offset-0 ", }, }, defaultVariants: { variant: "default", }, } ); export type FormQuantityFieldProps< TFieldValues extends FieldValues = FieldValues, TName extends FieldPath = FieldPath > = { button?: (props?: React.PropsWithChildren) => React.ReactNode; defaultValue?: any; } & InputProps & FormInputProps & Partial & FormInputWithIconProps & UseControllerProps & VariantProps & { precision: number; }; export const FormQuantityField = React.forwardRef( (props, ref) => { const { name, label, hint, description, placeholder, className, disabled, defaultValue, rules, precision, variant, } = props; const { control } = useFormContext(); const transformToInput = (value: any) => { if (typeof value !== "object") { return value; } const quantityOrError = Quantity.create(value); if (quantityOrError.isFailure) { throw quantityOrError.error; } return ( quantityOrError.object .toNumber() //.toPrecision(precision ?? value.precision) .toString() ); }; return ( { return ( {label && } { // "value" ya viene con los "0" de la precisión console.log(value); field.onChange(value ?? ""); }} /> {description && {description}} ); }} /> ); } );