import { useEffect, useRef } from "react"; import { useController } from "react-hook-form"; const DatePicker = ({ name, control, placeholder = "DD-MM-YYYY", className = "", allowText = false, maxDate, // removed default new Date() minDate, ...rest }) => { const inputRef = useRef(null); const { field: { onChange, value, ref } } = useController({ name, control }); useEffect(() => { if (inputRef.current) { flatpickr(inputRef.current, { dateFormat: "d-m-Y", allowInput: allowText, defaultDate: value ? flatpickr.parseDate(value, "Y-m-d") : null, maxDate: maxDate ?? undefined, // only applied if passed minDate: minDate ? new Date(minDate.split("T")[0]) : undefined, onChange: function (selectedDates) { if (selectedDates.length > 0) { // store in YYYY-MM-DD const formatted = flatpickr.formatDate(selectedDates[0], "Y-m-d"); onChange(formatted); } else { onChange(""); } }, ...rest }); } }, [inputRef, value, allowText, maxDate, minDate, rest, onChange]); return (