import { isSameMonth, setMonth, setYear, startOfMonth } from "date-fns"; import React, { useState } from "react"; import { BodyShort } from "../../../typography"; import { isMatch } from "../../date-utils"; import { useMonthPickerContext } from "../MonthPicker.context"; import MonthButton from "./MonthPicker.Button"; const getAllMonths = () => { const months: Date[] = []; const date = startOfMonth(new Date()); for (let month = 0; month <= 11; month++) { months.push(setMonth(date, month)); } return months; }; const MonthPickerTable = () => { const [focus, setFocus] = useState(); const { selected, year, disabled } = useMonthPickerContext(); const months: Date[] = getAllMonths(); const hasSelected = selected && months.some((m) => isSameMonth(setYear(m, year.getFullYear()), selected)); const getRootFallback = () => { const today = startOfMonth(new Date()); if ( year?.getFullYear() === today.getFullYear() && !isMatch(today, disabled) ) { return today; } for (let i = 0; i < months.length; i++) { const m = months[i]; if (!isMatch(setYear(m, year.getFullYear()), disabled)) { return setYear(m, year.getFullYear()); } } }; const [tabRoot, setTabRoot] = useState( hasSelected ? selected : getRootFallback(), ); if (tabRoot?.getFullYear() !== year.getFullYear()) { setTabRoot(hasSelected ? selected : getRootFallback()); } const tableMonths = [ months.slice(0, 4), months.slice(4, 8), months.slice(8, 12), ]; return ( {tableMonths.map((tableRow, i) => ( {tableRow.map((month: Date) => { return ( ); })} ))} ); }; export { MonthPickerTable };