UNPKG

react-day-picker

Version:

Customizable Date Picker for React

1,167 lines (1,104 loc) 95.8 kB
import * as React from 'react'; import React__default, { createContext, useContext, useState, forwardRef, useEffect, useRef } from 'react'; import { enUS } from 'date-fns/locale'; import { format, startOfMonth, endOfMonth, startOfDay, isSameYear, setMonth, setYear, startOfYear, differenceInCalendarMonths, addMonths, isSameMonth, isBefore, startOfISOWeek, startOfWeek, addDays, isSameDay, isAfter, subDays, differenceInCalendarDays, isDate, max, min, addWeeks, addYears, endOfISOWeek, endOfWeek, getUnixTime, getISOWeek, getWeek, getWeeksInMonth, parse } from 'date-fns'; /****************************************************************************** Copyright (c) Microsoft Corporation. Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. ***************************************************************************** */ var __assign = function() { __assign = Object.assign || function __assign(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; function __rest(s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; } function __spreadArray(to, from, pack) { if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) { if (ar || !(i in from)) { if (!ar) ar = Array.prototype.slice.call(from, 0, i); ar[i] = from[i]; } } return to.concat(ar || Array.prototype.slice.call(from)); } /** Returns true when the props are of type {@link DayPickerMultipleProps}. */ function isDayPickerMultiple(props) { return props.mode === 'multiple'; } /** Returns true when the props are of type {@link DayPickerRangeProps}. */ function isDayPickerRange(props) { return props.mode === 'range'; } /** Returns true when the props are of type {@link DayPickerSingleProps}. */ function isDayPickerSingle(props) { return props.mode === 'single'; } /** * The name of the default CSS classes. */ var defaultClassNames = { root: 'rdp', multiple_months: 'rdp-multiple_months', with_weeknumber: 'rdp-with_weeknumber', vhidden: 'rdp-vhidden', button_reset: 'rdp-button_reset', button: 'rdp-button', caption: 'rdp-caption', caption_start: 'rdp-caption_start', caption_end: 'rdp-caption_end', caption_between: 'rdp-caption_between', caption_label: 'rdp-caption_label', caption_dropdowns: 'rdp-caption_dropdowns', dropdown: 'rdp-dropdown', dropdown_month: 'rdp-dropdown_month', dropdown_year: 'rdp-dropdown_year', dropdown_icon: 'rdp-dropdown_icon', months: 'rdp-months', month: 'rdp-month', table: 'rdp-table', tbody: 'rdp-tbody', tfoot: 'rdp-tfoot', head: 'rdp-head', head_row: 'rdp-head_row', head_cell: 'rdp-head_cell', nav: 'rdp-nav', nav_button: 'rdp-nav_button', nav_button_previous: 'rdp-nav_button_previous', nav_button_next: 'rdp-nav_button_next', nav_icon: 'rdp-nav_icon', row: 'rdp-row', weeknumber: 'rdp-weeknumber', cell: 'rdp-cell', day: 'rdp-day', day_today: 'rdp-day_today', day_outside: 'rdp-day_outside', day_selected: 'rdp-day_selected', day_disabled: 'rdp-day_disabled', day_hidden: 'rdp-day_hidden', day_range_start: 'rdp-day_range_start', day_range_end: 'rdp-day_range_end', day_range_middle: 'rdp-day_range_middle' }; /** * The default formatter for the caption. */ function formatCaption(month, options) { return format(month, 'LLLL y', options); } /** * The default formatter for the Day button. */ function formatDay(day, options) { return format(day, 'd', options); } /** * The default formatter for the Month caption. */ function formatMonthCaption(month, options) { return format(month, 'LLLL', options); } /** * The default formatter for the week number. */ function formatWeekNumber(weekNumber) { return "".concat(weekNumber); } /** * The default formatter for the name of the weekday. */ function formatWeekdayName(weekday, options) { return format(weekday, 'cccccc', options); } /** * The default formatter for the Year caption. */ function formatYearCaption(year, options) { return format(year, 'yyyy', options); } var formatters = /*#__PURE__*/Object.freeze({ __proto__: null, formatCaption: formatCaption, formatDay: formatDay, formatMonthCaption: formatMonthCaption, formatWeekNumber: formatWeekNumber, formatWeekdayName: formatWeekdayName, formatYearCaption: formatYearCaption }); /** * The default ARIA label for the day button. */ var labelDay = function (day, activeModifiers, options) { return format(day, 'do MMMM (EEEE)', options); }; /** * The default ARIA label for the WeekNumber element. */ var labelMonthDropdown = function () { return 'Month: '; }; /** * The default ARIA label for next month button in navigation */ var labelNext = function () { return 'Go to next month'; }; /** * The default ARIA label for previous month button in navigation */ var labelPrevious = function () { return 'Go to previous month'; }; /** * The default ARIA label for the Weekday element. */ var labelWeekday = function (day, options) { return format(day, 'cccc', options); }; /** * The default ARIA label for the WeekNumber element. */ var labelWeekNumber = function (n) { return "Week n. ".concat(n); }; /** * The default ARIA label for the WeekNumber element. */ var labelYearDropdown = function () { return 'Year: '; }; var labels = /*#__PURE__*/Object.freeze({ __proto__: null, labelDay: labelDay, labelMonthDropdown: labelMonthDropdown, labelNext: labelNext, labelPrevious: labelPrevious, labelWeekNumber: labelWeekNumber, labelWeekday: labelWeekday, labelYearDropdown: labelYearDropdown }); /** * Returns the default values to use in the DayPickerContext, in case they are * not passed down with the DayPicker initial props. */ function getDefaultContextValues() { var captionLayout = 'buttons'; var classNames = defaultClassNames; var locale = enUS; var modifiersClassNames = {}; var modifiers = {}; var numberOfMonths = 1; var styles = {}; var today = new Date(); return { captionLayout: captionLayout, classNames: classNames, formatters: formatters, labels: labels, locale: locale, modifiersClassNames: modifiersClassNames, modifiers: modifiers, numberOfMonths: numberOfMonths, styles: styles, today: today, mode: 'default' }; } /** Return the `fromDate` and `toDate` prop values values parsing the DayPicker props. */ function parseFromToProps(props) { var fromYear = props.fromYear, toYear = props.toYear, fromMonth = props.fromMonth, toMonth = props.toMonth; var fromDate = props.fromDate, toDate = props.toDate; if (fromMonth) { fromDate = startOfMonth(fromMonth); } else if (fromYear) { fromDate = new Date(fromYear, 0, 1); } if (toMonth) { toDate = endOfMonth(toMonth); } else if (toYear) { toDate = new Date(toYear, 11, 31); } return { fromDate: fromDate ? startOfDay(fromDate) : undefined, toDate: toDate ? startOfDay(toDate) : undefined }; } /** * The DayPicker context shares the props passed to DayPicker within internal * and custom components. It is used to set the default values and perform * one-time calculations required to render the days. * * Access to this context from the {@link useDayPicker} hook. */ var DayPickerContext = createContext(undefined); /** * The provider for the {@link DayPickerContext}, assigning the defaults from the * initial DayPicker props. */ function DayPickerProvider(props) { var _a; var initialProps = props.initialProps; var defaultContextValues = getDefaultContextValues(); var _b = parseFromToProps(initialProps), fromDate = _b.fromDate, toDate = _b.toDate; var captionLayout = (_a = initialProps.captionLayout) !== null && _a !== void 0 ? _a : defaultContextValues.captionLayout; if (captionLayout !== 'buttons' && (!fromDate || !toDate)) { // When no from/to dates are set, the caption is always buttons captionLayout = 'buttons'; } var onSelect; if (isDayPickerSingle(initialProps) || isDayPickerMultiple(initialProps) || isDayPickerRange(initialProps)) { onSelect = initialProps.onSelect; } var value = __assign(__assign(__assign({}, defaultContextValues), initialProps), { captionLayout: captionLayout, classNames: __assign(__assign({}, defaultContextValues.classNames), initialProps.classNames), components: __assign({}, initialProps.components), formatters: __assign(__assign({}, defaultContextValues.formatters), initialProps.formatters), fromDate: fromDate, labels: __assign(__assign({}, defaultContextValues.labels), initialProps.labels), mode: initialProps.mode || defaultContextValues.mode, modifiers: __assign(__assign({}, defaultContextValues.modifiers), initialProps.modifiers), modifiersClassNames: __assign(__assign({}, defaultContextValues.modifiersClassNames), initialProps.modifiersClassNames), onSelect: onSelect, styles: __assign(__assign({}, defaultContextValues.styles), initialProps.styles), toDate: toDate }); return (React__default.createElement(DayPickerContext.Provider, { value: value }, props.children)); } /** * Hook to access the {@link DayPickerContextValue}. * * Use the DayPicker context to access to the props passed to DayPicker inside * internal or custom components. */ function useDayPicker() { var context = useContext(DayPickerContext); if (!context) { throw new Error("useDayPicker must be used within a DayPickerProvider."); } return context; } /** Render the caption for the displayed month. This component is used when `captionLayout="buttons"`. */ function CaptionLabel(props) { var _a = useDayPicker(), locale = _a.locale, classNames = _a.classNames, styles = _a.styles, formatCaption = _a.formatters.formatCaption; return (React__default.createElement("div", { className: classNames.caption_label, style: styles.caption_label, "aria-live": "polite", role: "presentation", id: props.id }, formatCaption(props.displayMonth, { locale: locale }))); } /** * Render the icon in the styled drop-down. */ function IconDropdown(props) { return (React__default.createElement("svg", __assign({ width: "8px", height: "8px", viewBox: "0 0 120 120", "data-testid": "iconDropdown" }, props), React__default.createElement("path", { d: "M4.22182541,48.2218254 C8.44222828,44.0014225 15.2388494,43.9273804 19.5496459,47.9996989 L19.7781746,48.2218254 L60,88.443 L100.221825,48.2218254 C104.442228,44.0014225 111.238849,43.9273804 115.549646,47.9996989 L115.778175,48.2218254 C119.998577,52.4422283 120.07262,59.2388494 116.000301,63.5496459 L115.778175,63.7781746 L67.7781746,111.778175 C63.5577717,115.998577 56.7611506,116.07262 52.4503541,112.000301 L52.2218254,111.778175 L4.22182541,63.7781746 C-0.0739418023,59.4824074 -0.0739418023,52.5175926 4.22182541,48.2218254 Z", fill: "currentColor", fillRule: "nonzero" }))); } /** * Render a styled select component – displaying a caption and a custom * drop-down icon. */ function Dropdown(props) { var _a, _b; var onChange = props.onChange, value = props.value, children = props.children, caption = props.caption, className = props.className, style = props.style; var dayPicker = useDayPicker(); var IconDropdownComponent = (_b = (_a = dayPicker.components) === null || _a === void 0 ? void 0 : _a.IconDropdown) !== null && _b !== void 0 ? _b : IconDropdown; return (React__default.createElement("div", { className: className, style: style }, React__default.createElement("span", { className: dayPicker.classNames.vhidden }, props['aria-label']), React__default.createElement("select", { name: props.name, "aria-label": props['aria-label'], className: dayPicker.classNames.dropdown, style: dayPicker.styles.dropdown, value: value, onChange: onChange }, children), React__default.createElement("div", { className: dayPicker.classNames.caption_label, style: dayPicker.styles.caption_label, "aria-hidden": "true" }, caption, React__default.createElement(IconDropdownComponent, { className: dayPicker.classNames.dropdown_icon, style: dayPicker.styles.dropdown_icon })))); } /** Render the dropdown to navigate between months. */ function MonthsDropdown(props) { var _a; var _b = useDayPicker(), fromDate = _b.fromDate, toDate = _b.toDate, styles = _b.styles, locale = _b.locale, formatMonthCaption = _b.formatters.formatMonthCaption, classNames = _b.classNames, components = _b.components, labelMonthDropdown = _b.labels.labelMonthDropdown; // Dropdown should appear only when both from/toDate is set if (!fromDate) return React__default.createElement(React__default.Fragment, null); if (!toDate) return React__default.createElement(React__default.Fragment, null); var dropdownMonths = []; if (isSameYear(fromDate, toDate)) { // only display the months included in the range var date = startOfMonth(fromDate); for (var month = fromDate.getMonth(); month <= toDate.getMonth(); month++) { dropdownMonths.push(setMonth(date, month)); } } else { // display all the 12 months var date = startOfMonth(new Date()); // Any date should be OK, as we just need the year for (var month = 0; month <= 11; month++) { dropdownMonths.push(setMonth(date, month)); } } var handleChange = function (e) { var selectedMonth = Number(e.target.value); var newMonth = setMonth(startOfMonth(props.displayMonth), selectedMonth); props.onChange(newMonth); }; var DropdownComponent = (_a = components === null || components === void 0 ? void 0 : components.Dropdown) !== null && _a !== void 0 ? _a : Dropdown; return (React__default.createElement(DropdownComponent, { name: "months", "aria-label": labelMonthDropdown(), className: classNames.dropdown_month, style: styles.dropdown_month, onChange: handleChange, value: props.displayMonth.getMonth(), caption: formatMonthCaption(props.displayMonth, { locale: locale }) }, dropdownMonths.map(function (m) { return (React__default.createElement("option", { key: m.getMonth(), value: m.getMonth() }, formatMonthCaption(m, { locale: locale }))); }))); } /** * Render a dropdown to change the year. Take in account the `nav.fromDate` and * `toDate` from context. */ function YearsDropdown(props) { var _a; var displayMonth = props.displayMonth; var _b = useDayPicker(), fromDate = _b.fromDate, toDate = _b.toDate, locale = _b.locale, styles = _b.styles, classNames = _b.classNames, components = _b.components, formatYearCaption = _b.formatters.formatYearCaption, labelYearDropdown = _b.labels.labelYearDropdown; var years = []; // Dropdown should appear only when both from/toDate is set if (!fromDate) return React__default.createElement(React__default.Fragment, null); if (!toDate) return React__default.createElement(React__default.Fragment, null); var fromYear = fromDate.getFullYear(); var toYear = toDate.getFullYear(); for (var year = fromYear; year <= toYear; year++) { years.push(setYear(startOfYear(new Date()), year)); } var handleChange = function (e) { var newMonth = setYear(startOfMonth(displayMonth), Number(e.target.value)); props.onChange(newMonth); }; var DropdownComponent = (_a = components === null || components === void 0 ? void 0 : components.Dropdown) !== null && _a !== void 0 ? _a : Dropdown; return (React__default.createElement(DropdownComponent, { name: "years", "aria-label": labelYearDropdown(), className: classNames.dropdown_year, style: styles.dropdown_year, onChange: handleChange, value: displayMonth.getFullYear(), caption: formatYearCaption(displayMonth, { locale: locale }) }, years.map(function (year) { return (React__default.createElement("option", { key: year.getFullYear(), value: year.getFullYear() }, formatYearCaption(year, { locale: locale }))); }))); } /** * Helper hook for using controlled/uncontrolled values from a component props. * * When the value is not controlled, pass `undefined` as `controlledValue` and * use the returned setter to update it. * * When the value is controlled, pass the controlled value as second * argument, which will be always returned as `value`. */ function useControlledValue(defaultValue, controlledValue) { var _a = useState(defaultValue), uncontrolledValue = _a[0], setValue = _a[1]; var value = controlledValue === undefined ? uncontrolledValue : controlledValue; return [value, setValue]; } /** Return the initial month according to the given options. */ function getInitialMonth(context) { var month = context.month, defaultMonth = context.defaultMonth, today = context.today; var initialMonth = month || defaultMonth || today || new Date(); var toDate = context.toDate, fromDate = context.fromDate, _a = context.numberOfMonths, numberOfMonths = _a === void 0 ? 1 : _a; // Fix the initialMonth if is after the to-date if (toDate && differenceInCalendarMonths(toDate, initialMonth) < 0) { var offset = -1 * (numberOfMonths - 1); initialMonth = addMonths(toDate, offset); } // Fix the initialMonth if is before the from-date if (fromDate && differenceInCalendarMonths(initialMonth, fromDate) < 0) { initialMonth = fromDate; } return startOfMonth(initialMonth); } /** Controls the navigation state. */ function useNavigationState() { var context = useDayPicker(); var initialMonth = getInitialMonth(context); var _a = useControlledValue(initialMonth, context.month), month = _a[0], setMonth = _a[1]; var goToMonth = function (date) { var _a; if (context.disableNavigation) return; var month = startOfMonth(date); setMonth(month); (_a = context.onMonthChange) === null || _a === void 0 ? void 0 : _a.call(context, month); }; return [month, goToMonth]; } /** * Return the months to display in the component according to the number of * months and the from/to date. */ function getDisplayMonths(month, _a) { var reverseMonths = _a.reverseMonths, numberOfMonths = _a.numberOfMonths; var start = startOfMonth(month); var end = startOfMonth(addMonths(start, numberOfMonths)); var monthsDiff = differenceInCalendarMonths(end, start); var months = []; for (var i = 0; i < monthsDiff; i++) { var nextMonth = addMonths(start, i); months.push(nextMonth); } if (reverseMonths) months = months.reverse(); return months; } /** * Returns the next month the user can navigate to according to the given * options. * * Please note that the next month is not always the next calendar month: * * - if after the `toDate` range, is undefined; * - if the navigation is paged, is the number of months displayed ahead. * */ function getNextMonth(startingMonth, options) { if (options.disableNavigation) { return undefined; } var toDate = options.toDate, pagedNavigation = options.pagedNavigation, _a = options.numberOfMonths, numberOfMonths = _a === void 0 ? 1 : _a; var offset = pagedNavigation ? numberOfMonths : 1; var month = startOfMonth(startingMonth); if (!toDate) { return addMonths(month, offset); } var monthsDiff = differenceInCalendarMonths(toDate, startingMonth); if (monthsDiff < numberOfMonths) { return undefined; } // Jump forward as the number of months when paged navigation return addMonths(month, offset); } /** * Returns the next previous the user can navigate to, according to the given * options. * * Please note that the previous month is not always the previous calendar * month: * * - if before the `fromDate` date, is `undefined`; * - if the navigation is paged, is the number of months displayed before. * */ function getPreviousMonth(startingMonth, options) { if (options.disableNavigation) { return undefined; } var fromDate = options.fromDate, pagedNavigation = options.pagedNavigation, _a = options.numberOfMonths, numberOfMonths = _a === void 0 ? 1 : _a; var offset = pagedNavigation ? numberOfMonths : 1; var month = startOfMonth(startingMonth); if (!fromDate) { return addMonths(month, -offset); } var monthsDiff = differenceInCalendarMonths(month, fromDate); if (monthsDiff <= 0) { return undefined; } // Jump back as the number of months when paged navigation return addMonths(month, -offset); } /** * The Navigation context shares details and methods to navigate the months in DayPicker. * Access this context from the {@link useNavigation} hook. */ var NavigationContext = createContext(undefined); /** Provides the values for the {@link NavigationContext}. */ function NavigationProvider(props) { var dayPicker = useDayPicker(); var _a = useNavigationState(), currentMonth = _a[0], goToMonth = _a[1]; var displayMonths = getDisplayMonths(currentMonth, dayPicker); var nextMonth = getNextMonth(currentMonth, dayPicker); var previousMonth = getPreviousMonth(currentMonth, dayPicker); var isDateDisplayed = function (date) { return displayMonths.some(function (displayMonth) { return isSameMonth(date, displayMonth); }); }; var goToDate = function (date, refDate) { if (isDateDisplayed(date)) { return; } if (refDate && isBefore(date, refDate)) { goToMonth(addMonths(date, 1 + dayPicker.numberOfMonths * -1)); } else { goToMonth(date); } }; var value = { currentMonth: currentMonth, displayMonths: displayMonths, goToMonth: goToMonth, goToDate: goToDate, previousMonth: previousMonth, nextMonth: nextMonth, isDateDisplayed: isDateDisplayed }; return (React__default.createElement(NavigationContext.Provider, { value: value }, props.children)); } /** * Hook to access the {@link NavigationContextValue}. Use this hook to navigate * between months or years in DayPicker. * * This hook is meant to be used inside internal or custom components. */ function useNavigation() { var context = useContext(NavigationContext); if (!context) { throw new Error('useNavigation must be used within a NavigationProvider'); } return context; } /** * Render a caption with the dropdowns to navigate between months and years. */ function CaptionDropdowns(props) { var _a; var _b = useDayPicker(), classNames = _b.classNames, styles = _b.styles, components = _b.components; var goToMonth = useNavigation().goToMonth; var handleMonthChange = function (newMonth) { goToMonth(newMonth); }; var CaptionLabelComponent = (_a = components === null || components === void 0 ? void 0 : components.CaptionLabel) !== null && _a !== void 0 ? _a : CaptionLabel; var captionLabel = (React__default.createElement(CaptionLabelComponent, { id: props.id, displayMonth: props.displayMonth })); return (React__default.createElement("div", { className: classNames.caption_dropdowns, style: styles.caption_dropdowns }, React__default.createElement("div", { className: classNames.vhidden }, captionLabel), React__default.createElement(MonthsDropdown, { onChange: handleMonthChange, displayMonth: props.displayMonth }), React__default.createElement(YearsDropdown, { onChange: handleMonthChange, displayMonth: props.displayMonth }))); } /** * Render the "previous month" button in the navigation. */ function IconLeft(props) { return (React__default.createElement("svg", __assign({ width: "16px", height: "16px", viewBox: "0 0 120 120" }, props), React__default.createElement("path", { d: "M69.490332,3.34314575 C72.6145263,0.218951416 77.6798462,0.218951416 80.8040405,3.34314575 C83.8617626,6.40086786 83.9268205,11.3179931 80.9992143,14.4548388 L80.8040405,14.6568542 L35.461,60 L80.8040405,105.343146 C83.8617626,108.400868 83.9268205,113.317993 80.9992143,116.454839 L80.8040405,116.656854 C77.7463184,119.714576 72.8291931,119.779634 69.6923475,116.852028 L69.490332,116.656854 L18.490332,65.6568542 C15.4326099,62.5991321 15.367552,57.6820069 18.2951583,54.5451612 L18.490332,54.3431458 L69.490332,3.34314575 Z", fill: "currentColor", fillRule: "nonzero" }))); } /** * Render the "next month" button in the navigation. */ function IconRight(props) { return (React__default.createElement("svg", __assign({ width: "16px", height: "16px", viewBox: "0 0 120 120" }, props), React__default.createElement("path", { d: "M49.8040405,3.34314575 C46.6798462,0.218951416 41.6145263,0.218951416 38.490332,3.34314575 C35.4326099,6.40086786 35.367552,11.3179931 38.2951583,14.4548388 L38.490332,14.6568542 L83.8333725,60 L38.490332,105.343146 C35.4326099,108.400868 35.367552,113.317993 38.2951583,116.454839 L38.490332,116.656854 C41.5480541,119.714576 46.4651794,119.779634 49.602025,116.852028 L49.8040405,116.656854 L100.804041,65.6568542 C103.861763,62.5991321 103.926821,57.6820069 100.999214,54.5451612 L100.804041,54.3431458 L49.8040405,3.34314575 Z", fill: "currentColor" }))); } /** Render a button HTML element applying the reset class name. */ var Button = forwardRef(function (props, ref) { var _a = useDayPicker(), classNames = _a.classNames, styles = _a.styles; var classNamesArr = [classNames.button_reset, classNames.button]; if (props.className) { classNamesArr.push(props.className); } var className = classNamesArr.join(' '); var style = __assign(__assign({}, styles.button_reset), styles.button); if (props.style) { Object.assign(style, props.style); } return (React__default.createElement("button", __assign({}, props, { ref: ref, type: "button", className: className, style: style }))); }); /** A component rendering the navigation buttons or the drop-downs. */ function Navigation(props) { var _a, _b; var _c = useDayPicker(), dir = _c.dir, locale = _c.locale, classNames = _c.classNames, styles = _c.styles, _d = _c.labels, labelPrevious = _d.labelPrevious, labelNext = _d.labelNext, components = _c.components; if (!props.nextMonth && !props.previousMonth) { return React__default.createElement(React__default.Fragment, null); } var previousLabel = labelPrevious(props.previousMonth, { locale: locale }); var previousClassName = [ classNames.nav_button, classNames.nav_button_previous ].join(' '); var nextLabel = labelNext(props.nextMonth, { locale: locale }); var nextClassName = [ classNames.nav_button, classNames.nav_button_next ].join(' '); var IconRightComponent = (_a = components === null || components === void 0 ? void 0 : components.IconRight) !== null && _a !== void 0 ? _a : IconRight; var IconLeftComponent = (_b = components === null || components === void 0 ? void 0 : components.IconLeft) !== null && _b !== void 0 ? _b : IconLeft; return (React__default.createElement("div", { className: classNames.nav, style: styles.nav }, !props.hidePrevious && (React__default.createElement(Button, { name: "previous-month", "aria-label": previousLabel, className: previousClassName, style: styles.nav_button_previous, disabled: !props.previousMonth, onClick: props.onPreviousClick }, dir === 'rtl' ? (React__default.createElement(IconRightComponent, { className: classNames.nav_icon, style: styles.nav_icon })) : (React__default.createElement(IconLeftComponent, { className: classNames.nav_icon, style: styles.nav_icon })))), !props.hideNext && (React__default.createElement(Button, { name: "next-month", "aria-label": nextLabel, className: nextClassName, style: styles.nav_button_next, disabled: !props.nextMonth, onClick: props.onNextClick }, dir === 'rtl' ? (React__default.createElement(IconLeftComponent, { className: classNames.nav_icon, style: styles.nav_icon })) : (React__default.createElement(IconRightComponent, { className: classNames.nav_icon, style: styles.nav_icon })))))); } /** * Render a caption with a button-based navigation. */ function CaptionNavigation(props) { var numberOfMonths = useDayPicker().numberOfMonths; var _a = useNavigation(), previousMonth = _a.previousMonth, nextMonth = _a.nextMonth, goToMonth = _a.goToMonth, displayMonths = _a.displayMonths; var displayIndex = displayMonths.findIndex(function (month) { return isSameMonth(props.displayMonth, month); }); var isFirst = displayIndex === 0; var isLast = displayIndex === displayMonths.length - 1; var hideNext = numberOfMonths > 1 && (isFirst || !isLast); var hidePrevious = numberOfMonths > 1 && (isLast || !isFirst); var handlePreviousClick = function () { if (!previousMonth) return; goToMonth(previousMonth); }; var handleNextClick = function () { if (!nextMonth) return; goToMonth(nextMonth); }; return (React__default.createElement(Navigation, { displayMonth: props.displayMonth, hideNext: hideNext, hidePrevious: hidePrevious, nextMonth: nextMonth, previousMonth: previousMonth, onPreviousClick: handlePreviousClick, onNextClick: handleNextClick })); } /** * Render the caption of a month. The caption has a different layout when * setting the {@link DayPickerBase.captionLayout} prop. */ function Caption(props) { var _a; var _b = useDayPicker(), classNames = _b.classNames, disableNavigation = _b.disableNavigation, styles = _b.styles, captionLayout = _b.captionLayout, components = _b.components; var CaptionLabelComponent = (_a = components === null || components === void 0 ? void 0 : components.CaptionLabel) !== null && _a !== void 0 ? _a : CaptionLabel; var caption; if (disableNavigation) { caption = (React__default.createElement(CaptionLabelComponent, { id: props.id, displayMonth: props.displayMonth })); } else if (captionLayout === 'dropdown') { caption = (React__default.createElement(CaptionDropdowns, { displayMonth: props.displayMonth, id: props.id })); } else if (captionLayout === 'dropdown-buttons') { caption = (React__default.createElement(React__default.Fragment, null, React__default.createElement(CaptionDropdowns, { displayMonth: props.displayMonth, id: props.id }), React__default.createElement(CaptionNavigation, { displayMonth: props.displayMonth, id: props.id }))); } else { caption = (React__default.createElement(React__default.Fragment, null, React__default.createElement(CaptionLabelComponent, { id: props.id, displayMonth: props.displayMonth }), React__default.createElement(CaptionNavigation, { displayMonth: props.displayMonth, id: props.id }))); } return (React__default.createElement("div", { className: classNames.caption, style: styles.caption }, caption)); } /** Render the Footer component (empty as default).*/ // eslint-disable-next-line @typescript-eslint/no-unused-vars function Footer(props) { var _a = useDayPicker(), footer = _a.footer, styles = _a.styles, tfoot = _a.classNames.tfoot; if (!footer) return React__default.createElement(React__default.Fragment, null); return (React__default.createElement("tfoot", { className: tfoot, style: styles.tfoot }, React__default.createElement("tr", null, React__default.createElement("td", { colSpan: 8 }, footer)))); } /** * Generate a series of 7 days, starting from the week, to use for formatting * the weekday names (Monday, Tuesday, etc.). */ function getWeekdays(locale, /** The index of the first day of the week (0 - Sunday). */ weekStartsOn, /** Use ISOWeek instead of locale/ */ ISOWeek) { var start = ISOWeek ? startOfISOWeek(new Date()) : startOfWeek(new Date(), { locale: locale, weekStartsOn: weekStartsOn }); var days = []; for (var i = 0; i < 7; i++) { var day = addDays(start, i); days.push(day); } return days; } /** * Render the HeadRow component - i.e. the table head row with the weekday names. */ function HeadRow() { var _a = useDayPicker(), classNames = _a.classNames, styles = _a.styles, showWeekNumber = _a.showWeekNumber, locale = _a.locale, weekStartsOn = _a.weekStartsOn, ISOWeek = _a.ISOWeek, formatWeekdayName = _a.formatters.formatWeekdayName, labelWeekday = _a.labels.labelWeekday; var weekdays = getWeekdays(locale, weekStartsOn, ISOWeek); return (React__default.createElement("tr", { style: styles.head_row, className: classNames.head_row }, showWeekNumber && (React__default.createElement("td", { style: styles.head_cell, className: classNames.head_cell })), weekdays.map(function (weekday, i) { return (React__default.createElement("th", { key: i, scope: "col", className: classNames.head_cell, style: styles.head_cell, "aria-label": labelWeekday(weekday, { locale: locale }) }, formatWeekdayName(weekday, { locale: locale }))); }))); } /** Render the table head. */ function Head() { var _a; var _b = useDayPicker(), classNames = _b.classNames, styles = _b.styles, components = _b.components; var HeadRowComponent = (_a = components === null || components === void 0 ? void 0 : components.HeadRow) !== null && _a !== void 0 ? _a : HeadRow; return (React__default.createElement("thead", { style: styles.head, className: classNames.head }, React__default.createElement(HeadRowComponent, null))); } /** Render the content of the day cell. */ function DayContent(props) { var _a = useDayPicker(), locale = _a.locale, formatDay = _a.formatters.formatDay; return React__default.createElement(React__default.Fragment, null, formatDay(props.date, { locale: locale })); } /** * The SelectMultiple context shares details about the selected days when in * multiple selection mode. * * Access this context from the {@link useSelectMultiple} hook. */ var SelectMultipleContext = createContext(undefined); /** Provides the values for the {@link SelectMultipleContext}. */ function SelectMultipleProvider(props) { if (!isDayPickerMultiple(props.initialProps)) { var emptyContextValue = { selected: undefined, modifiers: { disabled: [] } }; return (React__default.createElement(SelectMultipleContext.Provider, { value: emptyContextValue }, props.children)); } return (React__default.createElement(SelectMultipleProviderInternal, { initialProps: props.initialProps, children: props.children })); } function SelectMultipleProviderInternal(_a) { var initialProps = _a.initialProps, children = _a.children; var selected = initialProps.selected, min = initialProps.min, max = initialProps.max; var onDayClick = function (day, activeModifiers, e) { var _a, _b; (_a = initialProps.onDayClick) === null || _a === void 0 ? void 0 : _a.call(initialProps, day, activeModifiers, e); var isMinSelected = Boolean(activeModifiers.selected && min && (selected === null || selected === void 0 ? void 0 : selected.length) === min); if (isMinSelected) { return; } var isMaxSelected = Boolean(!activeModifiers.selected && max && (selected === null || selected === void 0 ? void 0 : selected.length) === max); if (isMaxSelected) { return; } var selectedDays = selected ? __spreadArray([], selected, true) : []; if (activeModifiers.selected) { var index = selectedDays.findIndex(function (selectedDay) { return isSameDay(day, selectedDay); }); selectedDays.splice(index, 1); } else { selectedDays.push(day); } (_b = initialProps.onSelect) === null || _b === void 0 ? void 0 : _b.call(initialProps, selectedDays, day, activeModifiers, e); }; var modifiers = { disabled: [] }; if (selected) { modifiers.disabled.push(function (day) { var isMaxSelected = max && selected.length > max - 1; var isSelected = selected.some(function (selectedDay) { return isSameDay(selectedDay, day); }); return Boolean(isMaxSelected && !isSelected); }); } var contextValue = { selected: selected, onDayClick: onDayClick, modifiers: modifiers }; return (React__default.createElement(SelectMultipleContext.Provider, { value: contextValue }, children)); } /** * Hook to access the {@link SelectMultipleContextValue}. * * This hook is meant to be used inside internal or custom components. */ function useSelectMultiple() { var context = useContext(SelectMultipleContext); if (!context) { throw new Error('useSelectMultiple must be used within a SelectMultipleProvider'); } return context; } /** * Add a day to an existing range. * * The returned range takes in account the `undefined` values and if the added * day is already present in the range. */ function addToRange(day, range) { var _a = range || {}, from = _a.from, to = _a.to; if (!from) { return { from: day, to: undefined }; } if (!to && isSameDay(from, day)) { return { from: from, to: day }; } if (!to && isBefore(day, from)) { return { from: day, to: from }; } if (!to) { return { from: from, to: day }; } if (isSameDay(to, day) && isSameDay(from, day)) { return undefined; } if (isSameDay(to, day)) { return { from: to, to: undefined }; } if (isSameDay(from, day)) { return undefined; } if (isAfter(from, day)) { return { from: day, to: to }; } return { from: from, to: day }; } /** * The SelectRange context shares details about the selected days when in * range selection mode. * * Access this context from the {@link useSelectRange} hook. */ var SelectRangeContext = createContext(undefined); /** Provides the values for the {@link SelectRangeProvider}. */ function SelectRangeProvider(props) { if (!isDayPickerRange(props.initialProps)) { var emptyContextValue = { selected: undefined, modifiers: { range_start: [], range_end: [], range_middle: [], disabled: [] } }; return (React__default.createElement(SelectRangeContext.Provider, { value: emptyContextValue }, props.children)); } return (React__default.createElement(SelectRangeProviderInternal, { initialProps: props.initialProps, children: props.children })); } function SelectRangeProviderInternal(_a) { var initialProps = _a.initialProps, children = _a.children; var selected = initialProps.selected; var _b = selected || {}, selectedFrom = _b.from, selectedTo = _b.to; var min = initialProps.min; var max = initialProps.max; var onDayClick = function (day, activeModifiers, e) { var _a, _b; (_a = initialProps.onDayClick) === null || _a === void 0 ? void 0 : _a.call(initialProps, day, activeModifiers, e); var newRange = addToRange(day, selected); (_b = initialProps.onSelect) === null || _b === void 0 ? void 0 : _b.call(initialProps, newRange, day, activeModifiers, e); }; var modifiers = { range_start: [], range_end: [], range_middle: [], disabled: [] }; if (selectedFrom) { modifiers.range_start = [selectedFrom]; if (!selectedTo) { modifiers.range_end = [selectedFrom]; } else { modifiers.range_end = [selectedTo]; if (!isSameDay(selectedFrom, selectedTo)) { modifiers.range_middle = [ { after: selectedFrom, before: selectedTo } ]; } } } if (min) { if (selectedFrom && !selectedTo) { modifiers.disabled.push({ after: subDays(selectedFrom, min - 1), before: addDays(selectedFrom, min - 1) }); } if (selectedFrom && selectedTo) { modifiers.disabled.push({ after: selectedFrom, before: addDays(selectedFrom, min - 1) }); } } if (max) { if (selectedFrom && !selectedTo) { modifiers.disabled.push({ before: addDays(selectedFrom, -max + 1) }); modifiers.disabled.push({ after: addDays(selectedFrom, max - 1) }); } if (selectedFrom && selectedTo) { var selectedCount = differenceInCalendarDays(selectedTo, selectedFrom) + 1; var offset = max - selectedCount; modifiers.disabled.push({ before: subDays(selectedFrom, offset) }); modifiers.disabled.push({ after: addDays(selectedTo, offset) }); } } return (React__default.createElement(SelectRangeContext.Provider, { value: { selected: selected, onDayClick: onDayClick, modifiers: modifiers } }, children)); } /** * Hook to access the {@link SelectRangeContextValue}. * * This hook is meant to be used inside internal or custom components. */ function useSelectRange() { var context = useContext(SelectRangeContext); if (!context) { throw new Error('useSelectRange must be used within a SelectRangeProvider'); } return context; } /** Normalize to array a matcher input. */ function matcherToArray(matcher) { if (Array.isArray(matcher)) { return __spreadArray([], matcher, true); } else if (matcher !== undefined) { return [matcher]; } else { return []; } } /** Create CustomModifiers from dayModifiers */ function getCustomModifiers(dayModifiers) { var customModifiers = {}; Object.entries(dayModifiers).forEach(function (_a) { var modifier = _a[0], matcher = _a[1]; customModifiers[modifier] = matcherToArray(matcher); }); return customModifiers; } /** The name of the modifiers that are used internally by DayPicker. */ var InternalModifier; (function (InternalModifier) { InternalModifier["Outside"] = "outside"; /** Name of the modifier applied to the disabled days, using the `disabled` prop. */ InternalModifier["Disabled"] = "disabled"; /** Name of the modifier applied to the selected days using the `selected` prop). */ InternalModifier["Selected"] = "selected"; /** Name of the modifier applied to the hidden days using the `hidden` prop). */ InternalModifier["Hidden"] = "hidden"; /** Name of the modifier applied to the day specified using the `today` prop). */ InternalModifier["Today"] = "today"; /** The modifier applied to the day starting a selected range, when in range selection mode. */ InternalModifier["RangeStart"] = "range_start"; /** The modifier applied to the day ending a selected range, when in range selection mode. */ InternalModifier["RangeEnd"] = "range_end"; /** The modifier applied to the days between the start and the end of a selected range, when in range selection mode. */ InternalModifier["RangeMiddle"] = "range_middle"; })(InternalModifier || (InternalModifier = {})); var Selected = InternalModifier.Selected, Disabled = InternalModifier.Disabled, Hidden = InternalModifier.Hidden, Today = InternalModifier.Today, RangeEnd = InternalModifier.RangeEnd, RangeMiddle = InternalModifier.RangeMiddle, RangeStart = InternalModifier.RangeStart, Outside = InternalModifier.Outside; /** Return the {@link InternalModifiers} from the DayPicker and select contexts. */ function getInternalModifiers(dayPicker, selectMultiple, selectRange) { var _a; var internalModifiers = (_a = {}, _a[Selected] = matcherToArray(dayPicker.selected), _a[Disabled] = matcherToArray(dayPicker.disabled), _a[Hidden] = matcherToArray(dayPicker.hidden), _a[Today] = [dayPicker.today], _a[RangeEnd] = [], _a[RangeMiddle] = [], _a[RangeStart] = [], _a[Outside] = [], _a); if (dayPicker.fromDate) { internalModifiers[Disabled].push({ before: dayPicker.fromDate }); } if (dayPicker.toDate) { internalModifiers[Disabled].push({ after: dayPicker.toDate }); } if (isDayPickerMultiple(dayPicker)) { internalModifiers[Disabled] = internalModifiers[Disabled].concat(selectMultiple.modifiers[Disabled]); } else if (isDayPickerRange(dayPicker)) { internalModifiers[Disabled] = internalModifiers[Disabled].concat(selectRange.modifiers[Disabled]); internalModifiers[RangeStart] = selectRange.modifiers[RangeStart]; internalModifiers[RangeMiddle] = selectRange.modifiers[RangeMiddle]; internalModifiers[RangeEnd] = selectRange.modifiers[RangeEnd]; } return internalModifiers; } /** The Modifiers context store the modifiers used in DayPicker. To access the value of this context, use {@link useModifiers}. */ var ModifiersContext = createContext(undefined); /** Provide the value for the {@link ModifiersContext}. */ function ModifiersProvider(props) { var dayPicker = useDayPicker(); var selectMultiple = useSelectMultiple(); var selectRange = useSelectRange(); var internalModifiers = getInternalModifiers(dayPicker, selectMultiple, selectRange); var customModifiers = getCustomModifiers(dayPicker.modifiers); var modifiers = __assign(__assign({}, internalModifiers), customModifiers); return (React__default.createElement(ModifiersContext.Provider, { value: modifiers }, props.children)); } /** * Return the modifiers used by DayPicker. * * This hook is meant to be used inside internal or custom components. * Requires to be wrapped into {@link ModifiersProvider}. * */ function useModifiers() { var context = useContext(ModifiersContext); if (!context) { throw new Error('useModifiers must be used within a ModifiersProvider'); } return context; } /** Returns true if `matcher` is of type {@link DateInterval}. */ function isDateInterval(matcher) { return Boolean(matcher && typeof matcher === 'object' && 'before' in matcher && 'after' in matcher); } /** Returns true if `value` is a {@link DateRange} type. */ function isDateRange(value) { return Boolean(value && typeof value === 'object' && 'from' in value); } /** Returns true if `value` is of type {@link DateAfter}. */ function isDateAfterType(value) { return Boolean(value && typeof value === 'object' && 'after' in value); } /** Returns true if `value` is of type {@link DateBefore}. */ function isDateBeforeType(value) { return Boolean(value && typeof value === 'object' && 'before' in value); } /** Returns true if `value` is a {@link DayOfWeek} type. */ function isDayOfWeekType(value) { return Boolean(value && typeof value === 'object' && 'dayOfWeek' in value); } /** Return `true` whether `date` is inside `range`. */ function isDateInRange(date, range) { var _a; var from = range.from, to = range.to; if (!from) { return false; } if (!to && isSameDay(from, date)) { return true; } if (!to) { return false; } var isRangeInverted = differenceInCalendarDays(to, from) < 0; if (isRangeInverted) { _a = [to, from], from = _a[0], to = _a[1]; } var isInRange = differenceInCalendarDays(date, from) >= 0 && differenceInCalendarDays(to, date) >= 0; return isInRange; } /** Returns true if `value` is a Date type. */ function isDateType(value) { return isDate(value); } /** Returns true if `value` is an array of valid dates. */ function isArrayOfDates(value) { return Array.isArray(value) && value.every(isDate); } /** * Returns whether a day matches against at least one of the given Matchers. * * ``` * const day = new Date(2022, 5, 19); * const matcher1: DateRange = { * from: new Date(2021, 12, 21), * to: new Date(2021, 12, 30) * } * const matcher2: DateRange = { * from: new Date(2022, 5, 1), * to: new Date(2022, 5, 23) * } * * const isMatch(day, [matcher1, matcher2]); // true, since day is in the matcher1 range. * ``` * *