/* * Copyright 2023 Palantir Technologies, Inc. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import classNames from "classnames"; import { useCallback, useContext, useMemo, useRef } from "react"; import { CaptionLabel, type CaptionProps, useDayPicker, useNavigation } from "react-day-picker"; import innerText from "react-innertext"; import { Button, DISPLAYNAME_PREFIX, HTMLSelect, type OptionProps } from "@blueprintjs/core"; import { ChevronLeft, ChevronRight } from "@blueprintjs/icons"; import { DateUtils, Months } from "../../common"; import { DatePickerCaptionClasses as CaptionClasses, ReactDayPickerClasses } from "../../common/classes"; import { useMonthSelectRightOffset } from "../../common/useMonthSelectRightOffset"; import { DatePickerContext } from "../date-picker/datePickerContext"; /** * Custom react-day-picker caption component used in non-contiguous two-month date range pickers. * * We need to override the whole caption instead of its lower-level components because react-day-picker * does not have built-in support for non-contiguous range pickers. * * @see https://daypicker.dev/guides/custom-components */ export const DatePickerCaption = (props: CaptionProps) => { const { classNames: rdpClassNames, formatters, fromDate, toDate, labels } = useDayPicker(); const { locale, reverseMonthAndYearMenus } = useContext(DatePickerContext); // non-null assertion because we define these values in defaultProps const minYear = fromDate!.getFullYear(); const maxYear = toDate!.getFullYear(); const displayMonth = props.displayMonth.getMonth(); const displayYear = props.displayMonth.getFullYear(); const containerElement = useRef(null); const monthSelectElement = useRef(null); const { currentMonth, goToMonth, nextMonth, previousMonth } = useNavigation(); const handlePreviousClick = useCallback( () => previousMonth && goToMonth(previousMonth), [previousMonth, goToMonth], ); const handleNextClick = useCallback(() => nextMonth && goToMonth(nextMonth), [nextMonth, goToMonth]); const prevButton = (