'use client'; import styles from '@components/DatePicker.module.css'; import * as React from 'react'; interface DatePickerProps { year?: number; month?: number; } const WEEKDAYS = ['SU', 'MO', 'TU', 'WE', 'TH', 'FR', 'SA']; const MONTH_NAMES = [ 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December' ]; const MAX_CELLS = 42; const DatePicker: React.FC = ({ year, month }) => { const today = new Date(); const [currentYear, setYear] = React.useState(year || today.getFullYear()); const [currentMonth, setMonth] = React.useState(month || today.getMonth() + 1); const first = new Date(currentYear, currentMonth - 1, 1); const startingWeekday = first.getDay(); const daysInMonth = new Date(currentYear, currentMonth, 0).getDate(); const cells: React.ReactNode[] = []; for (let i = 0; i < startingWeekday; i++) { cells.push(
); } for (let day = 1; day <= daysInMonth; day++) { const presentationDay = String(day).padStart(2, '0'); cells.push(
{presentationDay}
); } while (cells.length < MAX_CELLS) { cells.push(
); } const onSwitchPreviousMonth = () => { let newMonth = currentMonth - 1; let newYear = currentYear; if (newMonth < 1) { newMonth = 12; newYear--; } setMonth(newMonth); setYear(newYear); }; const onSwitchNextMonth = () => { let newMonth = currentMonth + 1; let newYear = currentYear; if (newMonth > 12) { newMonth = 1; newYear++; } setMonth(newMonth); setYear(newYear); }; return (
{currentYear} {MONTH_NAMES[currentMonth - 1].toUpperCase()}
{WEEKDAYS.map((day) => (
{day}
))}
{cells}
); }; export default DatePicker;