UNPKG

@dnb/eufemia

Version:

DNB Eufemia Design System UI Library

147 lines (146 loc) 3.82 kB
"use client"; import React, { useCallback, useContext } from 'react'; import { addMonths, addYears, isSameMonth, isSameYear, subMonths, subYears } from 'date-fns'; import Button from "../Button.js"; import { useTranslation } from "../../shared/index.js"; import DatePickerContext from "./DatePickerContext.js"; import { formatDate } from "../date-format/DateFormatUtils.js"; const titleFormats = { both: { month: 'long', year: 'numeric' }, month: { month: 'long' }, year: { year: 'numeric' } }; const dateHandlers = { month: { prev: subMonths, next: addMonths }, year: { prev: subYears, next: addYears } }; export function DatePickerCalendarNav({ type = 'both', id, nr, date, locale }) { const { minDate, maxDate, views, setViews, props: { link: isLinkedCalendars } } = useContext(DatePickerContext); const { selectedMonth, selectedYear } = useTranslation().DatePicker; const title = type === 'year' ? selectedYear : selectedMonth; const titleFormat = titleFormats[type]; const buttonDateType = type === 'year' ? 'year' : 'month'; const onNav = useCallback(({ nr, type: navigationType }) => { const handlerType = type === 'year' ? 'year' : 'month'; const updatedViews = views.map(view => { if (view.nr === nr || isLinkedCalendars && view.nr === 1) { const month = dateHandlers[handlerType][navigationType](view.month, 1); return { ...view, month }; } return view; }); setViews(updatedViews); }, [type, views, setViews, isLinkedCalendars]); return React.createElement("div", { className: 'dnb-date-picker__header__row' + (type === 'year' ? " dnb-date-picker__header__row--year" : "") }, React.createElement("div", { className: "dnb-date-picker__header__nav" }, React.createElement(CalendarNavButton, { type: "prev", nr: nr, date: date, dateLimit: minDate, dateType: buttonDateType, dateFormat: titleFormat, locale: locale, onClick: onNav })), React.createElement("label", { id: `${id}--title`, className: "dnb-date-picker__header__title dnb-no-focus", title: title.replace(/%s/, formatDate(date, { locale, options: titleFormat })), tabIndex: -1 }, formatDate(date, { locale, options: titleFormat })), React.createElement("div", { className: "dnb-date-picker__header__nav" }, React.createElement(CalendarNavButton, { type: "next", dateType: buttonDateType, nr: nr, date: date, dateLimit: maxDate, dateFormat: titleFormat, locale: locale, onClick: onNav }))); } const navButtonDisabledHandlers = { month: isSameMonth, year: isSameYear }; function CalendarNavButton({ type, dateType, dateFormat, nr, date, dateLimit, locale, onClick, onKeyDown }) { const translations = useTranslation().DatePicker; const translationKey = `${type}${capitalizeFirstLetter(dateType)}`; const dateHandler = dateHandlers[dateType][type]; const title = translations[translationKey].replace(/%s/, formatDate(dateHandler(date, 1), { locale, options: dateFormat })); const disabled = dateLimit && navButtonDisabledHandlers[dateType](date, dateLimit); const icon = type === 'prev' ? 'chevron_left' : 'chevron_right'; return React.createElement(Button, { className: `dnb-date-picker__${type}` + (disabled ? " disabled" : ""), icon: icon, size: "small", "aria-label": title, onClick: () => onClick && !disabled && onClick({ nr, type }), onKeyDown: onKeyDown }); } function capitalizeFirstLetter(value) { return `${value.charAt(0).toLocaleUpperCase()}${value.slice(1)}`; } //# sourceMappingURL=DatePickerCalendarNavigator.js.map