import { ButtonBase, IconButton, InputAdornment, Menu, TextField, Typography, } from "@mui/material"; import { useEffect, useState } from "react"; import React from "react"; import {format} from "date-fns"; import { EventOutlined } from "@mui/icons-material"; import { EtDatePickerProvider, EtDatePickerProviderProps } from "./EtDatePickerContext"; import { DateType, EthiopianDate } from "./util/EthiopianDateUtils"; import { DatePicker } from "@mui/x-date-pickers"; import EtGrDateCalendar from "./Components/EtGrDateCalendar"; import { useEtLocalization } from "./EtLocalizationProvider"; type CustomFieldProps = Omit< React.ComponentProps, "onChange" | "value" | "InputProps" >; export type EtDateFieldProps = Pick< React.ComponentProps, "disablePast" | "disableFuture" > & { minDate?: Date; maxDate?: Date }; type EtDatePickerProps = { onClick?: () => void; isRange?: boolean; value?: Date | null | [Date | null, Date | null]; onChange?: (date: Date | null | [Date | null, Date | null]) => void; } & CustomFieldProps & EtDateFieldProps; const EtDatePicker: React.FC = ({ onClick, value, onChange, isRange, ...props }) => { const { localType, getLocalMonthName } = useEtLocalization(); const [dateType, setDateType] = useState(localType); const [date, setDate] = useState(undefined); const [startDate, setStartDate] = useState(null); const [endDate, setEndDate] = useState(null); const [anchorEl, setAnchorEl] = React.useState(null); const open = Boolean(anchorEl); const handleClick = ( event: | React.MouseEvent | React.MouseEvent ) => { setAnchorEl(event.currentTarget); }; const handleClose = () => { if (isRange && (!startDate || !endDate)) return; setAnchorEl(null); }; const handleDateChange: EtDatePickerProviderProps['onChange'] = (newValue) => { if (isRange) { const [sDate, eDate] = newValue as [Date | null, Date | null]; setStartDate(sDate); setEndDate(eDate); onChange?.([sDate, eDate]); // Close the modal if both start and end dates are selected if (sDate && eDate) { setAnchorEl(null); } } else { setDate(newValue as Date | null); onChange?.(newValue as Date | null); if (newValue instanceof Date) { setAnchorEl(null); } } }; const handleDateTypeChange = (event: React.MouseEvent) => { const newDateType = dateType === "GC" ? localType : "GC"; setDateType(newDateType); event.stopPropagation(); }; const { disableSwitcher } = useEtLocalization(); useEffect(() => { if (isRange) { if (Array.isArray(value) && value.length === 2) { setStartDate(value[0]); setEndDate(value[1]); } else { setStartDate(null); setEndDate(null); } } else { if (value instanceof Date || value === null) { setDate(value); } else { setDate(undefined); } } }, [value, isRange]); return ( <> { handleClick(event); }, startAdornment: disableSwitcher ? undefined : ( {dateType === "CUSTOM" ? "CU" : dateType} ), endAdornment: ( ), }} /> ); }; export default EtDatePicker;