import LeftArrowIcon from '@/assets/icons/left-arrow.svg' import RightArrowIcon from '@/assets/icons/right-arrow.svg' import { useEffect, useState } from 'react' import { getCalendarDates, getL10Weekday } from '../utils' interface Props { /** * Control the selected * date programmatically, including situations like provide a default value or control the selected * date by parent component. */ value?: Date | { year: number, month: number, day: number } /** * A callback function that will be called when a date is selected inside the panel. * @param date - The date user selected. * @returns */ onSelect?: (date: { year: number, month: number, day: number }) => void /** * The language code that will be used to localize the panel. * Accept standard ISO 639-1 language code, such as 'zh-CN', 'en-US', 'ja-JP', etc. Default to the * language of the user’s browser. Note that it will not effect to the screen reader, but the screen * reader will still read the date in the user’s language. */ localization?: string } /** * A panel that allows users to select a date. * * @component * * @param {Props} props */ export default ({ value, onSelect, localization }: Props) => { const [currentMonth, setCurrentMonth] = useState(new Date().getMonth()) const [currentYear, setCurrentYear] = useState(new Date().getFullYear()) const [selectedDate, setSelectedDate] = useState(new Date()) const [dates, setDates] = useState([]) const [l10nDays, setL10nDays] = useState([]) const [selectMonth, setSelectMonth] = useState(false) useEffect(() => { setDates(getCalendarDates(currentMonth, currentYear)) }, [currentMonth, currentYear]) useEffect(() => { if (!value) return const date = value instanceof Date ? value : new Date(value.year, value.month - 1, value.day) setSelectedDate(date) setCurrentMonth(date.getMonth()) setCurrentYear(date.getFullYear()) }, [value]) useEffect(() => { const i18n = localization || navigator.language setL10nDays(getL10Weekday(i18n)) }, [localization]) function selectDate(date: Date) { setSelectedDate(date) onSelect?.({ year: date.getFullYear(), month: date.getMonth() + 1, day: date.getDate() }) } function skipToLastMonth() { if (currentMonth === 0) { setCurrentMonth(11) setCurrentYear(currentYear - 1) } else setCurrentMonth(currentMonth - 1) } function skipToNextMonth() { if (currentMonth === 11) { setCurrentMonth(0) setCurrentYear(currentYear + 1) } else setCurrentMonth(currentMonth + 1) } if (selectMonth) return (
setCurrentYear(parseInt(e.target.value))} aria-label="Year input, type a year to go to that year" />
{Array.from({ length: 12 }).map((_, index) => )}
) else return (
{l10nDays.map((day, index) =>
{day}
)} {dates.map(date => )}
) }