import { useState, useEffect, FC } from 'react'; import DatePicker from 'react-multi-date-picker'; import persian from 'react-date-object/calendars/persian'; import persian_fa from 'react-date-object/locales/persian_fa'; import DateObject from 'react-date-object'; import CalenderIcon from '../assets/images/calendar.svg' import { clx } from '../helpers/utils'; type Props = { onChange: (date: string) => void; defaulValue?: string; error_text?: string; placeholder: string; reset?: boolean; isDateTime?: boolean; className?: string; label?: string }; const DatePickerComponent: FC = (props: Props) => { const [value, setValue] = useState(null); useEffect(() => { if (props.reset) { setValue(null); } }, [props.reset]); useEffect(() => { if (value) { const formattedDate = `${value.year}/${value.month.number}/${value.day}`; props.onChange(formattedDate); } // eslint-disable-next-line react-hooks/exhaustive-deps }, [value]); useEffect(() => { if (props.defaulValue && !value) { const defaultDate = new DateObject({ date: props.defaulValue, calendar: persian, locale: persian_fa, }); setValue(defaultDate); } }, [props.defaulValue, value]); return (
{props.label &&
{props.label}
}
setValue(date as DateObject)} calendar={persian} locale={persian_fa} calendarPosition="bottom-right" className={`rmdp-mobile ${props.className}`} /> {props.error_text && props.error_text !== '' && (
{props.error_text}
)}
); }; export default DatePickerComponent;