import React, { useState } from "react"; import { CalendarIcon } from "lucide-react"; import dayjs from "dayjs"; import timezone from "dayjs/plugin/timezone"; import { cn } from "@/lib/utils"; import { Button } from "@/components/ui/button"; import { Calendar } from "@/components/ui/calendar_v9"; import { Input } from "@/components/ui/input"; import { Popover, PopoverContent, PopoverTrigger, } from "@/components/ui/popover"; import { DoneButton, EARLIEST_DATE, LATEST_DATE, PickerInput, PREVENT_DEFAULT, QuickOption, } from "./SelectDate"; dayjs.extend(timezone); export type PickDateTimeProps = { timestamp?: Date; quickOptions?: Array<{ label: string; timestamp: Date }>; numberOfMonths?: 1 | 2; showTimezone?: boolean; required?: boolean; onSelect: (datetime: Date | undefined) => void; }; export const PickDateTime: React.FC = ({ timestamp, quickOptions, numberOfMonths, showTimezone, required = false, onSelect, }) => { const [month, setMonth] = useState(timestamp); function initializeTimestamp(e) { if (!timestamp) { const now = dayjs().toDate(); onSelect(now); } } return ( <> {quickOptions?.length ? (
Quick Options
{quickOptions.map((option, idx) => ( { onSelect(option.timestamp); setMonth(option.timestamp); }} /> ))}
) : null}
{ onSelect(timestamp); setMonth(timestamp); }} numberOfMonths={numberOfMonths || 1} captionLayout="dropdown" startMonth={EARLIEST_DATE} endMonth={LATEST_DATE} hidden={{ before: EARLIEST_DATE, after: LATEST_DATE }} required={required} />
{ const value = dayjs(e.target.value).toDate(); if (dayjs(value).isValid()) { setMonth(value); onSelect(value); } }} onMouseDown={initializeTimestamp} onFocus={PREVENT_DEFAULT} onClick={PREVENT_DEFAULT} />
{showTimezone && (
Time Zone: {(dayjs as any)?.["tz"]?.guess()}
)} ); }; // Popover containing a PickDateTime const SelectDateTime: React.FC< Omit & { initialTimestamp?: Date; align?: "center" | "start" | "end"; } > = ({ initialTimestamp, align = "start", quickOptions, showTimezone, onSelect, }) => { const [isOpen, setIsOpen] = useState(false); const [timestamp, setTimestamp] = useState( initialTimestamp, ); const toggle = (value?: boolean) => { if (!isOpen) { setTimestamp(initialTimestamp); } setIsOpen(value === undefined ? !isOpen : value); }; return (
{ onSelect(timestamp); toggle(false); }} />
); }; export default SelectDateTime;