import { format, isAfter, isBefore, setYear, startOfMonth, startOfYear, } from "date-fns"; import React from "react"; import { ArrowLeftIcon, ArrowRightIcon } from "@navikt/aksel-icons"; import { Button } from "../../../button"; import { Select } from "../../../form/select"; import { useDateTranslationContext } from "../../Date.locale"; import { useMonthPickerContext } from "../MonthPicker.context"; type MonthPickerCaptionProps = { popupLabelId?: string; }; const MonthPickerCaption = ({ popupLabelId }: MonthPickerCaptionProps) => { const { fromDate, toDate, locale, year, onYearChange, caption } = useMonthPickerContext(); const translate = useDateTranslationContext().translate; const years: Date[] = []; if (caption === "dropdown" && fromDate && toDate) { const fromYear = fromDate.getFullYear(); const toDateYear = toDate.getFullYear(); for (let currYear = fromYear; currYear <= toDateYear; currYear++) { years.push(setYear(startOfYear(new Date()), currYear)); } if (!years.map((x) => x.getFullYear()).includes(year.getFullYear())) { years.push(setYear(startOfYear(new Date()), year.getFullYear())); } years.sort((a, b) => b.getFullYear() - a.getFullYear()); } const handleYearChange = (event: React.ChangeEvent) => { onYearChange(setYear(startOfMonth(new Date()), Number(event.target.value))); }; const handleButtonClick = (val: number) => { const newYear = Number(year.getFullYear() + val); onYearChange(setYear(year, newYear)); }; const disablePreviousYear = () => { return fromDate ? isBefore(year?.getFullYear() - 1, fromDate?.getFullYear()) : false; }; const disableNextYear = () => { return toDate ? isAfter(year?.getFullYear() + 1, toDate?.getFullYear()) : false; }; return (
{caption === "dropdown" && ( {year.getFullYear()} )}
); }; export { MonthPickerCaption };