'use client'; import React from 'react'; import { msg } from '@lingui/core/macro'; import { useLingui } from '@lingui/react'; import { Trans } from '@lingui/react/macro'; import { CalendarIcon } from 'lucide-react'; import { DateTime } from 'luxon'; import { cn } from '../lib/utils'; import { Button } from './button'; import { Calendar } from './calendar'; import { Input } from './input'; import { Popover, PopoverContent, PopoverTrigger } from './popover'; export interface DateTimePickerProps { value?: Date; onChange?: (date: Date | undefined) => void; placeholder?: string; disabled?: boolean; className?: string; minDate?: Date; } export const DateTimePicker = ({ value, onChange, placeholder, disabled = false, className, minDate = new Date(), }: DateTimePickerProps) => { const { _ } = useLingui(); const [open, setOpen] = React.useState(false); const handleDateSelect = (selectedDate: Date | undefined) => { if (!selectedDate) { onChange?.(undefined); return; } if (value) { const existingTime = DateTime.fromJSDate(value); const newDateTime = DateTime.fromJSDate(selectedDate).set({ hour: existingTime.hour, minute: existingTime.minute, }); onChange?.(newDateTime.toJSDate()); } else { const now = DateTime.now(); const newDateTime = DateTime.fromJSDate(selectedDate).set({ hour: now.hour, minute: now.minute, }); onChange?.(newDateTime.toJSDate()); } setOpen(false); }; const handleTimeChange = (event: React.ChangeEvent) => { const timeValue = event.target.value; if (!timeValue || !value) return; const [hours, minutes] = timeValue.split(':').map(Number); const newDateTime = DateTime.fromJSDate(value).set({ hour: hours, minute: minutes, }); onChange?.(newDateTime.toJSDate()); }; const formatDateTime = (date: Date) => { return DateTime.fromJSDate(date).toFormat('MMM dd, yyyy'); }; const formatTime = (date: Date) => { return DateTime.fromJSDate(date).toFormat('HH:mm'); }; return (
{ return date < minDate; } } initialFocus /> {value && (
at
)}
); };