import { useApp } from "../../../hooks"; import clsx from "clsx"; import { forwardRef, ReactNode, Ref, RefObject, useEffect, useImperativeHandle, useRef, useState } from "react"; import EntityIcon from "../../layouts/icons/EntityIcon"; import HintTooltip from "../tooltips/HintTooltip"; export interface RefInputNumber { input: RefObject; } interface Props { name?: string; title?: string; withLabel?: boolean; value?: number; setValue?: React.Dispatch>; className?: string; help?: string; min?: number; max?: number; disabled?: boolean; readOnly?: boolean; required?: boolean; hint?: ReactNode; step?: string; placeholder?: string; icon?: string; borderless?: boolean; autoFocus?: boolean; canUnset?: boolean; } const InputNumber = ( { name, title, withLabel = true, value, setValue, className, hint, help, disabled = false, readOnly = false, required = false, min = 0, max, step, placeholder, icon, borderless, autoFocus, canUnset, }: Props, ref: Ref ) => { const { t } = useApp(); useImperativeHandle(ref, () => ({ input })); const input = useRef(null); const [actualValue, setActualValue] = useState(value); useEffect(() => { setActualValue(value); }, [value]); useEffect(() => { if (setValue && actualValue !== undefined) { setValue(actualValue); } // eslint-disable-next-line react-hooks/exhaustive-deps }, [actualValue]); return (
{withLabel && ( )}
{icon && (
)} setActualValue(Number(e.currentTarget.value))} step={step} placeholder={placeholder} disabled={disabled} readOnly={readOnly} autoFocus={autoFocus} className={clsx( "block w-full min-w-0 flex-1 rounded-md border-gray-300 focus:border-accent-500 focus:ring-accent-500 sm:text-sm", className, disabled || readOnly ? "cursor-not-allowed bg-gray-100" : "hover:bg-gray-50 focus:bg-gray-50", icon && "pl-10", borderless && "border-transparent" )} />
); }; export default forwardRef(InputNumber);