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 RefInputRangeNumber { inputMin: RefObject; inputMax: RefObject; } interface Props { name?: string; title?: string; withLabel?: boolean; valueMin?: number | null; onChangeMin?: (value: number) => void; valueMax?: number | null; onChangeMax?: (value: number) => void; 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; } const InputRangeNumber = ( { name, title, withLabel = true, valueMin, onChangeMin, valueMax, onChangeMax, className, hint, help, disabled = false, readOnly = false, required = false, min = 0, max, step, placeholder, icon, borderless, autoFocus, }: Props, ref: Ref ) => { const { t } = useApp(); useImperativeHandle(ref, () => ({ inputMin, inputMax })); const inputMin = useRef(null); const inputMax = useRef(null); const [actualMin, setActualMin] = useState(valueMin ?? undefined); const [actualMax, setActualMax] = useState(valueMax ?? undefined); useEffect(() => { setActualMin(valueMin ?? undefined); }, [valueMin]); useEffect(() => { setActualMax(valueMax ?? undefined); }, [valueMax]); useEffect(() => { if (onChangeMin) { onChangeMin(actualMin ?? 0); } // eslint-disable-next-line react-hooks/exhaustive-deps }, [actualMin]); useEffect(() => { if (onChangeMax) { onChangeMax(actualMax ?? 0); } // eslint-disable-next-line react-hooks/exhaustive-deps }, [actualMax]); return (
{withLabel && ( )}
{icon && (
)}
setActualMin(Number(e.target.value) ?? 0)} step={step} placeholder={placeholder ?? t("shared.from") + "..."} 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" )} /> - setActualMax(Number(e.target.value) ?? 0)} step={step} placeholder={placeholder ?? t("shared.to") + "..."} disabled={disabled} readOnly={readOnly} 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(InputRangeNumber);