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 RefInputDate { inputMin: RefObject; inputMax: RefObject; } interface Props { name: string; title: string; valueMin?: Date | null; onChangeMin?: (date: Date) => void; valueMax?: Date | null; onChangeMax?: (date: Date) => void; className?: string; help?: string; disabled?: boolean; readOnly?: boolean; required?: boolean; hint?: ReactNode; icon?: string; darkMode?: boolean; autoFocus?: boolean; } const InputDate = ( { name, title, valueMin, valueMax, onChangeMin, onChangeMax, className, help, disabled = false, readOnly = false, required = false, hint, icon, darkMode, autoFocus, }: Props, ref: Ref ) => { useImperativeHandle(ref, () => ({ inputMin, inputMax })); const inputMin = useRef(null); const inputMax = useRef(null); const [actualMin, setActualMin] = useState(""); const [actualMax, setActualMax] = useState(""); useEffect(() => { if (valueMin) { const date = new Date(valueMin); if (date) { setActualMin(date.toISOString().split("T")[0]); } } }, [valueMin]); useEffect(() => { if (valueMax) { const date = new Date(valueMax); if (date) { setActualMax(date.toISOString().split("T")[0]); } } }, [valueMax]); useEffect(() => { if (onChangeMin && actualMin) { const date = new Date(actualMin); if (date) { onChangeMin(date); } } // eslint-disable-next-line react-hooks/exhaustive-deps }, [actualMin]); useEffect(() => { if (onChangeMax && actualMax) { const date = new Date(actualMax); if (date) { onChangeMax(date); } } // eslint-disable-next-line react-hooks/exhaustive-deps }, [actualMax]); return (
{icon && (
)}
setActualMin(e.target.value)} 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", icon && "pl-10" )} /> - setActualMax(e.target.value)} 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", icon && "pl-10" )} />
); }; export default forwardRef(InputDate);