import React, { useState, useEffect } from 'react'; import { Calendar, Clock } from 'lucide-react'; import './DateTimePicker.css'; const DateTimePicker = ({ value, onChange, placeholder = "选择会议时间" }) => { const [date, setDate] = useState(''); const [time, setTime] = useState(''); const [showQuickSelect, setShowQuickSelect] = useState(false); const [isInitialized, setIsInitialized] = useState(false); // 组件卸载时清理状态 useEffect(() => { return () => { setShowQuickSelect(false); }; }, []); // 初始化时间值 useEffect(() => { if (value && !isInitialized) { const dateObj = new Date(value); if (!isNaN(dateObj.getTime())) { // 转换为本地时间字符串 const timeZoneOffset = dateObj.getTimezoneOffset() * 60000; const localDate = new Date(dateObj.getTime() - timeZoneOffset); const isoString = localDate.toISOString(); setDate(isoString.split('T')[0]); setTime(isoString.split('T')[1].slice(0, 5)); } setIsInitialized(true); } else if (!value && !isInitialized) { setDate(''); setTime(''); setIsInitialized(true); } }, [value, isInitialized]); // 当日期或时间改变时,更新父组件的值 useEffect(() => { // 只在初始化完成后才触发onChange if (!isInitialized) return; if (date && time) { const dateTimeString = `${date}T${time}`; onChange?.(dateTimeString); } else if (!date && !time) { onChange?.(''); } }, [date, time, isInitialized]); // 移除onChange依赖 // 快速选择时间的选项 const timeOptions = [ { label: '09:00', value: '09:00' }, { label: '10:00', value: '10:00' }, { label: '11:00', value: '11:00' }, { label: '14:00', value: '14:00' }, { label: '15:00', value: '15:00' }, { label: '16:00', value: '16:00' }, { label: '17:00', value: '17:00' }, ]; // 快速选择日期的选项 const getQuickDateOptions = () => { const today = new Date(); const options = []; // 今天 options.push({ label: '今天', value: today.toISOString().split('T')[0] }); // 明天 const tomorrow = new Date(today); tomorrow.setDate(today.getDate() + 1); options.push({ label: '明天', value: tomorrow.toISOString().split('T')[0] }); // 后天 const dayAfterTomorrow = new Date(today); dayAfterTomorrow.setDate(today.getDate() + 2); options.push({ label: '后天', value: dayAfterTomorrow.toISOString().split('T')[0] }); return options; }; const quickDateOptions = getQuickDateOptions(); const formatDisplayText = () => { if (!date && !time) return placeholder; if (date && time) { const dateObj = new Date(`${date}T${time}`); return dateObj.toLocaleString('zh-CN', { year: 'numeric', month: 'long', day: 'numeric', hour: '2-digit', minute: '2-digit' }); } if (date) { const dateObj = new Date(date); return dateObj.toLocaleDateString('zh-CN', { year: 'numeric', month: 'long', day: 'numeric' }); } return placeholder; }; const clearDateTime = () => { setDate(''); setTime(''); // 重置初始化状态,允许后续值的设定 setIsInitialized(false); onChange?.(''); }; return (
{ e.stopPropagation(); setShowQuickSelect(!showQuickSelect); }}> {formatDisplayText()} {(date || time) && ( )}
{showQuickSelect && (

选择日期

{quickDateOptions.map((option) => ( ))}
{ e.preventDefault(); e.stopPropagation(); setDate(e.target.value); }} onClick={(e) => e.stopPropagation()} className="date-input" />

选择时间

{timeOptions.map((option) => ( ))}
{ e.preventDefault(); e.stopPropagation(); setTime(e.target.value); }} onClick={(e) => e.stopPropagation()} className="time-input" />
)} {showQuickSelect && (
setShowQuickSelect(false)} /> )}
); }; export default DateTimePicker;