import { compareAsc, compareDesc, format, isSameMonth, setYear, } from "date-fns"; import React, { useEffect, useRef } from "react"; import { cl } from "../../../utils/helpers"; import { dateIsInCurrentMonth, isMatch } from "../../date-utils"; import { useMonthPickerContext } from "../MonthPicker.context"; import { nextEnabled } from "../MonthPicker.util"; interface MonthType { month: Date; months: Date[]; focus: Date | undefined; setFocus: (date?: Date) => void; tabRoot?: Date; setTabRoot: (date?: Date) => void; } const disableMonth = (month: Date, fromDate?: Date, toDate?: Date) => { if (fromDate && toDate) { return ( (compareAsc(month, fromDate) === -1 && !isSameMonth(month, fromDate)) || (compareDesc(month, toDate) === -1 && !isSameMonth(month, toDate)) ); } if (fromDate) { return compareAsc(month, fromDate) === -1 && !isSameMonth(month, fromDate); } if (toDate) { return compareDesc(month, toDate) === -1 && !isSameMonth(month, toDate); } return false; }; export const MonthButton = ({ month, months, focus, setFocus, tabRoot, setTabRoot, }: MonthType) => { const ref = useRef(null); const { fromDate, toDate, locale, selected, disabled, year, onYearChange, onMonthSelect, caption, } = useMonthPickerContext(); const isSelected = selected && isSameMonth(month, selected); useEffect(() => { if (focus) { isSameMonth(month, focus) && ref.current && ref.current.focus(); setFocus(); } }, [focus, month, setFocus]); const isDisabled = isMatch(setYear(month, year.getFullYear()), disabled) || disableMonth(month, fromDate, toDate); const isThisMonth = dateIsInCurrentMonth(month, year); return ( ); }; export default MonthButton;