"use client"; import * as React from "react"; import { ChevronUpDownIcon } from "@heroicons/react/20/solid"; import { format } from "date-fns"; import { Calendar } from "./Calendar"; import { Popover, PopoverContent, PopoverTrigger } from "./Popover"; import { Button } from "./Buttons"; import { cn } from "~/utils/cn"; import { SimpleTooltip } from "./Tooltip"; import { XIcon } from "lucide-react"; type DateTimePickerProps = { label: string; value?: Date; onChange?: (date: Date | undefined) => void; showSeconds?: boolean; showNowButton?: boolean; showClearButton?: boolean; showInlineLabel?: boolean; className?: string; }; export function DateTimePicker({ label, value, onChange, showSeconds = true, showNowButton = false, showClearButton = false, showInlineLabel = false, className, }: DateTimePickerProps) { const [open, setOpen] = React.useState(false); // Extract time parts from value const hours = value ? value.getHours().toString().padStart(2, "0") : ""; const minutes = value ? value.getMinutes().toString().padStart(2, "0") : ""; const seconds = value ? value.getSeconds().toString().padStart(2, "0") : ""; const timeValue = showSeconds ? `${hours}:${minutes}:${seconds}` : `${hours}:${minutes}`; const handleDateSelect = (date: Date | undefined) => { if (date) { // Preserve the time from the current value if it exists if (value) { date.setHours(value.getHours()); date.setMinutes(value.getMinutes()); date.setSeconds(value.getSeconds()); } onChange?.(date); } else { onChange?.(undefined); } setOpen(false); }; const handleTimeChange = (e: React.ChangeEvent) => { const timeString = e.target.value; if (!timeString) return; const [h, m, s] = timeString.split(":").map(Number); const newDate = value ? new Date(value) : new Date(); newDate.setHours(h || 0); newDate.setMinutes(m || 0); newDate.setSeconds(s || 0); onChange?.(newDate); }; const handleNowClick = () => { onChange?.(new Date()); }; const handleClearClick = () => { onChange?.(undefined); }; return (
{showInlineLabel && ( {label} )} {showNowButton && ( )} {showClearButton && ( } content="Clear" disableHoverableContent asChild /> )}
); }