import { useMemo, useState } from 'react'; import { CalendarDays, ChevronLeft, ChevronRight, Clock3, X } from 'lucide-react'; type DateTimeInputProps = { label?: string; value: string; onChange: (value: string) => void; placeholder?: string; }; const weekDays = ['一', '二', '三', '四', '五', '六', '日']; function formatDate(date: Date) { const year = date.getFullYear(); const month = String(date.getMonth() + 1).padStart(2, '0'); const day = String(date.getDate()).padStart(2, '0'); return `${year}-${month}-${day}`; } function parseDate(value?: string) { if (!value) { return undefined; } const [year, month, day] = value.split('-').map(Number); return new Date(year, month - 1, day); } function getMonthLabel(date: Date) { return `${date.getFullYear()}年${date.getMonth() + 1}月`; } function getCalendarDays(viewDate: Date) { const year = viewDate.getFullYear(); const month = viewDate.getMonth(); const firstDate = new Date(year, month, 1); const firstWeekday = (firstDate.getDay() + 6) % 7; const startDate = new Date(year, month, 1 - firstWeekday); return Array.from({ length: 42 }, (_, index) => { const date = new Date(startDate); date.setDate(startDate.getDate() + index); return { date, dateString: formatDate(date), inCurrentMonth: date.getMonth() === month, }; }); } function splitDateTime(value: string) { const [date = '', time = ''] = value.split('T'); return { date, time }; } export function DateTimeInput({ label, value, onChange, placeholder = '选择定时发送时间' }: DateTimeInputProps) { const { date, time } = splitDateTime(value); const [open, setOpen] = useState(false); const [viewDate, setViewDate] = useState(() => parseDate(date) ?? new Date()); const calendarDays = useMemo(() => getCalendarDays(viewDate), [viewDate]); function moveMonth(offset: number) { setViewDate((current) => new Date(current.getFullYear(), current.getMonth() + offset, 1)); } function updateDate(nextDate: string) { onChange(`${nextDate}T${time || '09:00'}`); } function updateTime(nextTime: string) { onChange(`${date || formatDate(new Date())}T${nextTime}`); } return (
{label ? {label} : null} {open ? (
{getMonthLabel(viewDate)}
{weekDays.map((day) => {day})}
{calendarDays.map((item) => ( ))}
) : null}
); }