import "react-datepicker/dist/react-datepicker.css" import * as PopoverPrimitive from "@radix-ui/react-popover" import React, { useEffect, useState } from "react" import ArrowDownIcon from "../../fundamentals/icons/arrow-down-icon" import Button from "../../fundamentals/button" import CustomHeader from "./custom-header" import { DateTimePickerProps } from "./types" import InputContainer from "../../fundamentals/input-container" import InputHeader from "../../fundamentals/input-header" import ReactDatePicker from "react-datepicker" import clsx from "clsx" import moment from "moment" const getDateClassname = ( d: Date, tempDate: Date | null, greyPastDates: boolean = true ): string => { const classes: string[] = ["date"] if ( tempDate && moment(d).format("YY,MM,DD") === moment(tempDate).format("YY,MM,DD") ) { classes.push("chosen") } else if ( greyPastDates && moment(d).format("YY,MM,DD") < moment(new Date()).format("YY,MM,DD") ) { classes.push("past") } return classes.join(" ") } const DatePicker: React.FC = ({ date, onSubmitDate, label = "start date", required = false, tooltipContent, tooltip, }) => { const [tempDate, setTempDate] = useState(date || null) const [isOpen, setIsOpen] = useState(false) useEffect(() => setTempDate(date), [isOpen]) const submitDate = () => { if (!tempDate || !date) { onSubmitDate(null) setIsOpen(false) return } // update only date, month and year const newDate = new Date(date.getTime()) newDate.setUTCDate(tempDate.getUTCDate()) newDate.setUTCMonth(tempDate.getUTCMonth()) newDate.setUTCFullYear(tempDate.getUTCFullYear()) onSubmitDate(newDate) setIsOpen(false) } return (
setTempDate(date)} />
) } type CalendarComponentProps = { date: Date | null onChange: ( date: Date | null, event: React.SyntheticEvent | undefined ) => void greyPastDates?: boolean } export const CalendarComponent = ({ date, onChange, greyPastDates = true, }: CalendarComponentProps) => ( getDateClassname(d, date, greyPastDates)} renderCustomHeader={({ ...props }) => } /> ) export default DatePicker