import { isValidDate } from "@web3r/flowerkit/date"; import { formatDate } from "date-fns"; import type { ReactNode, MouseEvent } from "react"; import { cloneElement, isValidElement, useContext, useMemo, } from "react"; import { DatePickerFastNav } from "./DatePickerFastNav"; import { Btn } from "../../btn"; import type { TDatePickerCustomHeaderSlotProps, THeaderStateActions, TDatePickerCustomHeaderProps } from "../config/types"; import { DatePickerContext } from "../context"; import { getDateFormat } from "../lib"; /** * Шапка календаря * @returns {ReactNode} */ export const DatePickerCustomHeader = ({ dateInputValue, pickerRef, name, date, maxDate, minDate, changeYear, changeMonth, decreaseMonth, increaseMonth, prevMonthButtonDisabled, nextMonthButtonDisabled, onCtrlChange, slotHeader, dateFormat, timeFormat, isTime, isFastNav, iconPrev, iconNext, }: TDatePickerCustomHeaderProps): ReactNode => { const { getCN } = useContext(DatePickerContext); const stateActions = useMemo(() => ({ decreaseMonth, changeYear, changeMonth, increaseMonth, }), [ changeMonth, changeYear, decreaseMonth, increaseMonth ]); const onPrevClick = (event: MouseEvent) => { event.preventDefault(); if (typeof onCtrlChange === "function") { onCtrlChange({ data: { name, value: date, dateInputValue, type: "decreaseMonth", }, ref: pickerRef, event, stateActions, }); } else { decreaseMonth(); } }; const onNextClick = (event: MouseEvent) => { event.preventDefault(); if (typeof onCtrlChange === "function") { onCtrlChange({ data: { name, value: date, dateInputValue, type: "increaseMonth", }, ref: pickerRef, event: event, stateActions, }); } else { increaseMonth(); } }; const format = getDateFormat(isTime, dateFormat, timeFormat); const currentDateTitle = isValidDate(date) ? formatDate(date, format) : ""; const customSlot = useMemo(() => isValidElement(slotHeader) ? cloneElement(slotHeader, { ...slotHeader?.props ?? {}, stateActions, date, }) : null, [ slotHeader, date, onCtrlChange, stateActions ]); return (
{currentDateTitle}
{isFastNav && } {!!customSlot &&
{customSlot}
}
); };