import * as React from 'react'; import classNames from 'classnames'; import { BookingCalendarDatePickerClasses, bookingCalendarDatePickerDefaultClasses, } from './classes'; import { Moment } from 'moment'; import { BookingCalendarDateRange, BookingCalendarItemT, GetNewMomentFunctionType, defaultGetNewMomentFunction, } from '../common'; import { Button } from '../../button/Button'; import * as EventIcon from 'material-design-icons/action/svg/production/ic_event_24px.svg'; try { var DatePicker = require('react-datepicker').default; } catch { DatePicker = undefined; } // import DatePicker from 'react-datepicker'; export interface BookingCalendarDatePickerBaseProps< T extends BookingCalendarItemT > { from: Moment; till: Moment; onRangeChange?: (range: BookingCalendarDateRange) => void; getNewMomentFunction?: GetNewMomentFunctionType; } export interface BookingCalendarDatePickerProps extends BookingCalendarDatePickerClasses, BookingCalendarDatePickerBaseProps {} export function BookingCalendarDatePicker( props: BookingCalendarDatePickerProps ) { const { className, buttonClassName, from, till, onRangeChange, getNewMomentFunction = defaultGetNewMomentFunction, } = props; const [shownModal, setShownModal] = React.useState(); const calendarRef = React.useRef(); const onButtonClick = React.useCallback(() => { setShownModal('start'); calendarRef.current.setOpen(true); }, []); return (
} selected={from?.toDate()} // selectsRange={true} selectsStart={shownModal !== 'end'} selectsEnd={shownModal === 'end'} startDate={from?.toDate()} endDate={till?.toDate()} shouldCloseOnSelect={shownModal === 'end'} //dateFormat={DAY_FORMAT} onChange={(dt: Date) => { if (shownModal !== 'end') { const dtm = getNewMomentFunction(dt); let validTill = till.clone(); if (dtm.isAfter(till)) { validTill = dtm.clone().add(1, 'day'); } onRangeChange({ from: dtm, till: validTill }); setShownModal('end'); } else { let dtm = getNewMomentFunction(dt).startOf('day').add(1, 'day'); let validFrom = from.clone(); if (dtm.isBefore(from)) { validFrom = dtm.clone().add(-1, 'day'); } onRangeChange({ from: validFrom, till: dtm }); setShownModal(null); } }} withPortal={true} />
); }