import { useEffect, useState } from "react" import { generateUniqueId, applyColor, getL10Weekday, getCalendarDates, calculateWeekNum } from "../utils" export interface SingleWeekPickerProps { /** * 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. Note * that it will not effect to the screen reader, but the screen reader will still read the * date in the user’s language. * @default navigator.language */ localization?: string /** * The main color of the panel, including the text color and the border color. *@default '#000000' */ mainColor?: string /** * The accent color of the panel, including the background color of the selected date. *@default '#000000' */ accentColor?: string /** * The reversed color of the panel, including the text color of the selected date. *@default '#ffffff' */ reversedColor?: string /** * The hover color of the panel, including the hover background color of the date. *@default '#00000017' */ hoverColor?: string /** * The border color of the panel, including the divider color between the header and the body. *@default '#e0e0e0' */ borderColor?: string } /** * SingleWeekPicker * A panel that allows users to select a week. * * @component * * @param */ export default ({ localization, mainColor = '#000000', accentColor = '#000000', reversedColor = '#ffffff', hoverColor = '#00000017', borderColor = '#e0e0e0' }: SingleWeekPickerProps) => { const [currentMonth, setCurrentMonth] = useState(new Date().getMonth()) const [currentYear, setCurrentYear] = useState(new Date().getFullYear()) const [selectMonth, setSelectMonth] = useState(false) const [calendarWeeks, setCalendarWeeks] = useState([]) const [l10nDays, setL10nDays] = useState([]) const uniqueId = generateUniqueId() 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) } useEffect(() => { applyColor(uniqueId, { mainColor: mainColor, accentColor: accentColor, reversedColor: reversedColor, hoverColor: hoverColor, borderColor: borderColor }) }, [mainColor, accentColor, reversedColor, hoverColor, borderColor]) useEffect(() => { const i18n = localization || navigator.language setL10nDays(getL10Weekday(i18n)) }, [localization]) useEffect(() => { const dates = getCalendarDates(currentMonth, currentYear) let weeks: Date[][] = [] for (let i = 0; i < dates.length; i += 7) weeks.push(dates.slice(i, i + 7)) setCalendarWeeks(weeks) }, [currentMonth, currentYear]) useEffect(() => { const date = new Date(2033, 0, 1) console.log(date) console.log(calculateWeekNum(date)) }, []) if (selectMonth) { return } else { return } }