import { Locale, format, getMonth, getYear, setMonth, setYear, startOfMonth, } from "date-fns"; import React, { ChangeEvent, useCallback } from "react"; import { CalendarMonth, useDayPicker } from "react-day-picker"; import { ArrowLeftIcon, ArrowRightIcon } from "@navikt/aksel-icons"; import { Button } from "../../../button"; import { Select } from "../../../form/select"; import { BodyShort } from "../../../typography"; import { omit } from "../../../utils-external"; import { useDateTranslationContext } from "../../Date.locale"; import { calendarRange, getMonthOptions, getYearOptions, } from "../../date-utils"; import { MultipleMode } from "../DatePicker.types"; import { DatePickerWeekRow } from "./DatePicker.WeekRow"; const DatePickerMonths = ({ children, calendarMonth, locale, onWeekNumberClick, popupLabelId, ...rest }: { calendarMonth: CalendarMonth; displayIndex: number; locale: Locale; onWeekNumberClick: MultipleMode["onWeekNumberClick"]; popupLabelId?: string; } & React.HTMLAttributes) => { const { dayPickerProps, goToMonth, previousMonth, nextMonth } = useDayPicker(); const { captionLayout } = dayPickerProps; const translate = useDateTranslationContext().translate; const handleMonthChange = useCallback( (date: Date, e: ChangeEvent) => { const selectedMonth = Number(e.target.value); const newMonth = setMonth(startOfMonth(date), selectedMonth); goToMonth(newMonth); }, [goToMonth], ); const handleYearChange = useCallback( (date: Date, e: ChangeEvent) => { const selectedYear = Number(e.target.value); const newMonth = setYear(startOfMonth(date), selectedYear); goToMonth(newMonth); }, [goToMonth], ); const [navStart, navEnd] = calendarRange({ captionLayout: captionLayout === "dropdown" ? "dropdown" : "label", startMonth: dayPickerProps.startMonth, endMonth: dayPickerProps.endMonth, today: dayPickerProps.today, }); const months = getMonthOptions({ displayMonth: calendarMonth.date, navStart, navEnd, locale, }); const dropdownYears = getYearOptions({ navStart, navEnd, locale }); return (
{captionLayout?.startsWith("dropdown") && ( {format(calendarMonth.date, "LLLL y", { locale })} )}
{children}
); }; export { DatePickerMonths };