'use client'; import { useState, useRef, useEffect } from 'react'; import { ChevronLeft, ChevronRight, Calendar } from 'lucide-react'; interface MonthPickerProps { value: string; onChange: (value: string) => void; placeholder?: string; } const MONTHS = ['1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月', '9月', '10月', '11月', '12月']; export function MonthPicker({ value, onChange, placeholder = '选择月份' }: MonthPickerProps) { const [open, setOpen] = useState(false); const [year, setYear] = useState(() => { if (value) return parseInt(value.slice(0, 4)); return new Date().getFullYear(); }); const ref = useRef(null); useEffect(() => { const handler = (e: MouseEvent) => { if (ref.current && !ref.current.contains(e.target as Node)) setOpen(false); }; document.addEventListener('mousedown', handler); return () => document.removeEventListener('mousedown', handler); }, []); const selectedMonth = value ? parseInt(value.slice(5, 7)) : null; const selectedYear = value ? parseInt(value.slice(0, 4)) : null; const handleSelect = (month: number) => { const m = String(month).padStart(2, '0'); onChange(`${year}-${m}`); setOpen(false); }; const handleClear = (e: React.MouseEvent) => { e.stopPropagation(); onChange(''); setOpen(false); }; const displayText = value ? `${selectedYear}年${selectedMonth}月` : ''; return (
{open && (
{/* Year nav */}
{year}年
{/* Month grid */}
{MONTHS.map((label, i) => { const month = i + 1; const isSelected = selectedYear === year && selectedMonth === month; const isCurrent = new Date().getFullYear() === year && new Date().getMonth() === i; return ( ); })}
)}
); }