UNPKG

18.4 kBJavaScriptView Raw
1"use strict";
2var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3 if (k2 === undefined) k2 = k;
4 var desc = Object.getOwnPropertyDescriptor(m, k);
5 if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6 desc = { enumerable: true, get: function() { return m[k]; } };
7 }
8 Object.defineProperty(o, k2, desc);
9}) : (function(o, m, k, k2) {
10 if (k2 === undefined) k2 = k;
11 o[k2] = m[k];
12}));
13var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14 Object.defineProperty(o, "default", { enumerable: true, value: v });
15}) : function(o, v) {
16 o["default"] = v;
17});
18var __importStar = (this && this.__importStar) || function (mod) {
19 if (mod && mod.__esModule) return mod;
20 var result = {};
21 if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22 __setModuleDefault(result, mod);
23 return result;
24};
25Object.defineProperty(exports, "__esModule", { value: true });
26exports.DayPicker = DayPicker;
27const react_1 = __importStar(require("react"));
28const UI_js_1 = require("./UI.js");
29const getClassNamesForModifiers_js_1 = require("./helpers/getClassNamesForModifiers.js");
30const getComponents_js_1 = require("./helpers/getComponents.js");
31const getDataAttributes_js_1 = require("./helpers/getDataAttributes.js");
32const getDateLib_js_1 = require("./helpers/getDateLib.js");
33const getDefaultClassNames_js_1 = require("./helpers/getDefaultClassNames.js");
34const getFormatters_js_1 = require("./helpers/getFormatters.js");
35const getMonthOptions_js_1 = require("./helpers/getMonthOptions.js");
36const getStyleForModifiers_js_1 = require("./helpers/getStyleForModifiers.js");
37const getWeekdays_js_1 = require("./helpers/getWeekdays.js");
38const getYearOptions_js_1 = require("./helpers/getYearOptions.js");
39const defaultLabels = __importStar(require("./labels/index.js"));
40const locales_js_1 = require("./lib/locales.js");
41const useCalendar_js_1 = require("./useCalendar.js");
42const useDayPicker_js_1 = require("./useDayPicker.js");
43const useFocus_js_1 = require("./useFocus.js");
44const useGetModifiers_js_1 = require("./useGetModifiers.js");
45const useSelection_js_1 = require("./useSelection.js");
46const rangeIncludesDate_js_1 = require("./utils/rangeIncludesDate.js");
47const typeguards_js_1 = require("./utils/typeguards.js");
48/**
49 * Render the date picker calendar.
50 *
51 * @group DayPicker
52 * @see https://daypicker.dev
53 */
54function DayPicker(props) {
55 const { components, formatters, labels, dateLib, locale, classNames } = (0, react_1.useMemo)(() => ({
56 dateLib: (0, getDateLib_js_1.getDateLib)(props.dateLib),
57 components: (0, getComponents_js_1.getComponents)(props.components),
58 formatters: (0, getFormatters_js_1.getFormatters)(props.formatters),
59 labels: { ...defaultLabels, ...props.labels },
60 locale: { ...locales_js_1.enUS, ...props.locale },
61 classNames: { ...(0, getDefaultClassNames_js_1.getDefaultClassNames)(), ...props.classNames }
62 }), [
63 props.classNames,
64 props.components,
65 props.dateLib,
66 props.formatters,
67 props.labels,
68 props.locale
69 ]);
70 const { captionLayout, firstWeekContainsDate, mode, onDayBlur, onDayClick, onDayFocus, onDayKeyDown, onDayMouseEnter, onDayMouseLeave, onNextClick, onPrevClick, showWeekNumber, styles, useAdditionalDayOfYearTokens, useAdditionalWeekYearTokens, weekStartsOn } = props;
71 const formatOptions = {
72 locale,
73 weekStartsOn,
74 firstWeekContainsDate,
75 useAdditionalWeekYearTokens,
76 useAdditionalDayOfYearTokens
77 };
78 const labelOptions = formatOptions;
79 const { formatCaption, formatDay, formatMonthDropdown, formatWeekNumber, formatWeekNumberHeader, formatWeekdayName, formatYearDropdown } = formatters;
80 const calendar = (0, useCalendar_js_1.useCalendar)(props, dateLib);
81 const { days, months, navStart, navEnd, previousMonth, nextMonth, goToMonth } = calendar;
82 const getModifiers = (0, useGetModifiers_js_1.useGetModifiers)(days, props, dateLib);
83 const { isSelected, select, selected: selectedValue } = (0, useSelection_js_1.useSelection)(props, dateLib) ?? {};
84 const { blur, focused, isFocusTarget, moveFocus, setFocused } = (0, useFocus_js_1.useFocus)(props, calendar, getModifiers, isSelected ?? (() => false), dateLib);
85 const { labelDayButton, labelGridcell, labelGrid, labelMonthDropdown, labelNav, labelNext, labelPrevious, labelWeekday, labelWeekNumber, labelWeekNumberHeader, labelYearDropdown } = labels;
86 const weekdays = (0, react_1.useMemo)(() => (0, getWeekdays_js_1.getWeekdays)(locale, props.weekStartsOn, props.ISOWeek, dateLib), [dateLib, locale, props.ISOWeek, props.weekStartsOn]);
87 const isInteractive = mode !== undefined || onDayClick !== undefined;
88 const handlePreviousClick = (0, react_1.useCallback)(() => {
89 if (!previousMonth)
90 return;
91 goToMonth(previousMonth);
92 onPrevClick?.(previousMonth);
93 }, [previousMonth, goToMonth, onPrevClick]);
94 const handleNextClick = (0, react_1.useCallback)(() => {
95 if (!nextMonth)
96 return;
97 goToMonth(nextMonth);
98 onNextClick?.(nextMonth);
99 }, [goToMonth, nextMonth, onNextClick]);
100 const handleDayClick = (0, react_1.useCallback)((day, m) => (e) => {
101 e.preventDefault();
102 e.stopPropagation();
103 setFocused(day);
104 select?.(day.date, m, e);
105 onDayClick?.(day.date, m, e);
106 }, [select, onDayClick, setFocused]);
107 const handleDayFocus = (0, react_1.useCallback)((day, m) => (e) => {
108 setFocused(day);
109 onDayFocus?.(day.date, m, e);
110 }, [onDayFocus, setFocused]);
111 const handleDayBlur = (0, react_1.useCallback)((day, m) => (e) => {
112 blur();
113 onDayBlur?.(day.date, m, e);
114 }, [blur, onDayBlur]);
115 const handleDayKeyDown = (0, react_1.useCallback)((day, modifiers) => (e) => {
116 const keyMap = {
117 ArrowLeft: ["day", props.dir === "rtl" ? "after" : "before"],
118 ArrowRight: ["day", props.dir === "rtl" ? "before" : "after"],
119 ArrowDown: ["week", "after"],
120 ArrowUp: ["week", "before"],
121 PageUp: [e.shiftKey ? "year" : "month", "before"],
122 PageDown: [e.shiftKey ? "year" : "month", "after"],
123 Home: ["startOfWeek", "before"],
124 End: ["endOfWeek", "after"]
125 };
126 if (keyMap[e.key]) {
127 e.preventDefault();
128 e.stopPropagation();
129 const [moveBy, moveDir] = keyMap[e.key];
130 moveFocus(moveBy, moveDir);
131 }
132 onDayKeyDown?.(day.date, modifiers, e);
133 }, [moveFocus, onDayKeyDown, props.dir]);
134 const handleDayMouseEnter = (0, react_1.useCallback)((day, modifiers) => (e) => {
135 onDayMouseEnter?.(day.date, modifiers, e);
136 }, [onDayMouseEnter]);
137 const handleDayMouseLeave = (0, react_1.useCallback)((day, modifiers) => (e) => {
138 onDayMouseLeave?.(day.date, modifiers, e);
139 }, [onDayMouseLeave]);
140 const { className, style } = (0, react_1.useMemo)(() => ({
141 className: [classNames[UI_js_1.UI.Root], props.className]
142 .filter(Boolean)
143 .join(" "),
144 style: { ...styles?.[UI_js_1.UI.Root], ...props.style }
145 }), [classNames, props.className, props.style, styles]);
146 const dataAttributes = (0, getDataAttributes_js_1.getDataAttributes)(props);
147 const contextValue = {
148 selected: selectedValue,
149 select: select,
150 isSelected,
151 months,
152 nextMonth,
153 previousMonth,
154 goToMonth,
155 getModifiers
156 };
157 return (react_1.default.createElement(useDayPicker_js_1.dayPickerContext.Provider, { value: contextValue },
158 react_1.default.createElement(components.Root, { className: className, style: style, dir: props.dir, id: props.id, lang: props.lang, nonce: props.nonce, title: props.title, ...dataAttributes },
159 react_1.default.createElement(components.Months, { className: classNames[UI_js_1.UI.Months], style: styles?.[UI_js_1.UI.Months] },
160 !props.hideNavigation && (react_1.default.createElement(components.Nav, { role: "navigation", className: classNames[UI_js_1.UI.Nav], style: styles?.[UI_js_1.UI.Nav], "aria-label": labelNav() },
161 react_1.default.createElement(components.Button, { type: "button", className: classNames[UI_js_1.UI.ButtonPrevious], tabIndex: previousMonth ? undefined : -1, disabled: previousMonth ? undefined : true, "aria-label": labelPrevious(previousMonth, labelOptions), onClick: handlePreviousClick },
162 react_1.default.createElement(components.Chevron, { disabled: previousMonth ? undefined : true, className: classNames[UI_js_1.UI.Chevron], orientation: "left" })),
163 react_1.default.createElement(components.Button, { type: "button", className: classNames[UI_js_1.UI.ButtonNext], tabIndex: nextMonth ? undefined : -1, disabled: nextMonth ? undefined : true, "aria-label": labelNext(nextMonth, labelOptions), onClick: handleNextClick },
164 react_1.default.createElement(components.Chevron, { disabled: previousMonth ? undefined : true, orientation: "right", className: classNames[UI_js_1.UI.Chevron] })))),
165 months.map((calendarMonth, displayIndex) => {
166 const handleMonthChange = (e) => {
167 const selectedMonth = Number(e.target.value);
168 const month = dateLib.setMonth(dateLib.startOfMonth(calendarMonth.date), selectedMonth);
169 goToMonth(month);
170 };
171 const handleYearChange = (e) => {
172 const month = dateLib.setYear(dateLib.startOfMonth(calendarMonth.date), Number(e.target.value));
173 goToMonth(month);
174 };
175 const dropdownMonths = (0, getMonthOptions_js_1.getMonthOptions)(calendarMonth.date, navStart, navEnd, formatters, locale, dateLib);
176 const dropdownYears = (0, getYearOptions_js_1.getYearOptions)(months[0].date, navStart, navEnd, formatters, dateLib);
177 return (react_1.default.createElement(components.Month, { className: classNames[UI_js_1.UI.Month], style: styles?.[UI_js_1.UI.Month], key: displayIndex, displayIndex: displayIndex, calendarMonth: calendarMonth },
178 react_1.default.createElement(components.MonthCaption, { className: classNames[UI_js_1.UI.MonthCaption], style: styles?.[UI_js_1.UI.MonthCaption], calendarMonth: calendarMonth, displayIndex: displayIndex }, captionLayout?.startsWith("dropdown") ? (react_1.default.createElement(components.DropdownNav, { className: classNames[UI_js_1.UI.Dropdowns], style: styles?.[UI_js_1.UI.Dropdowns] },
179 captionLayout === "dropdown" ||
180 captionLayout === "dropdown-months" ? (react_1.default.createElement(components.Dropdown, { "aria-label": labelMonthDropdown(), classNames: classNames, components: components, disabled: Boolean(props.disableNavigation), onChange: handleMonthChange, options: dropdownMonths, style: styles?.[UI_js_1.UI.Dropdown], value: calendarMonth.date.getMonth() })) : (react_1.default.createElement("span", { role: "status", "aria-live": "polite" }, formatMonthDropdown(calendarMonth.date.getMonth()))),
181 captionLayout === "dropdown" ||
182 captionLayout === "dropdown-years" ? (react_1.default.createElement(components.Dropdown, { "aria-label": labelYearDropdown(labelOptions), classNames: classNames, components: components, disabled: Boolean(props.disableNavigation), onChange: handleYearChange, options: dropdownYears, style: styles?.[UI_js_1.UI.Dropdown], value: calendarMonth.date.getFullYear() })) : (react_1.default.createElement("span", { role: "status", "aria-live": "polite" }, formatYearDropdown(calendarMonth.date.getFullYear()))))) : (react_1.default.createElement(components.CaptionLabel, { className: classNames[UI_js_1.UI.CaptionLabel], role: "status", "aria-live": "polite" }, formatCaption(calendarMonth.date, formatOptions, dateLib)))),
183 react_1.default.createElement(components.MonthGrid, { role: "grid", "aria-multiselectable": mode === "multiple" || mode === "range", "aria-label": labelGrid(calendarMonth.date, labelOptions, dateLib) ||
184 undefined, className: classNames[UI_js_1.UI.MonthGrid], style: styles?.[UI_js_1.UI.MonthGrid] },
185 !props.hideWeekdays && (react_1.default.createElement(components.Weekdays, { className: classNames[UI_js_1.UI.Weekdays], role: "row", style: styles?.[UI_js_1.UI.Weekdays] },
186 showWeekNumber && (react_1.default.createElement(components.WeekNumberHeader, { "aria-label": labelWeekNumberHeader(labelOptions), className: classNames[UI_js_1.UI.WeekNumberHeader], role: "columnheader", style: styles?.[UI_js_1.UI.WeekNumberHeader] }, formatWeekNumberHeader())),
187 weekdays.map((weekday, i) => (react_1.default.createElement(components.Weekday, { "aria-label": labelWeekday(weekday, labelOptions, dateLib), className: classNames[UI_js_1.UI.Weekday], key: i, role: "columnheader", style: styles?.[UI_js_1.UI.Weekday] }, formatWeekdayName(weekday, formatOptions, dateLib)))))),
188 react_1.default.createElement(components.Weeks, { className: classNames[UI_js_1.UI.Weeks], role: "rowgroup", style: styles?.[UI_js_1.UI.Weeks] }, calendarMonth.weeks.map((week, weekIndex) => {
189 return (react_1.default.createElement(components.Week, { className: classNames[UI_js_1.UI.Week], key: week.weekNumber, role: "row", style: styles?.[UI_js_1.UI.Week], week: week },
190 showWeekNumber && (react_1.default.createElement(components.WeekNumber, { week: week, role: "rowheader", style: styles?.[UI_js_1.UI.WeekNumber], "aria-label": labelWeekNumber(week.weekNumber, {
191 locale
192 }), className: classNames[UI_js_1.UI.WeekNumber] }, formatWeekNumber(week.weekNumber))),
193 week.days.map((day) => {
194 const { date } = day;
195 const modifiers = getModifiers(day);
196 modifiers[UI_js_1.DayFlag.focused] =
197 !modifiers.hidden &&
198 Boolean(focused?.isEqualTo(day));
199 modifiers[UI_js_1.SelectionState.selected] =
200 !modifiers.disabled &&
201 (isSelected?.(date) || modifiers.selected);
202 if ((0, typeguards_js_1.isDateRange)(selectedValue)) {
203 // add range modifiers
204 const { from, to } = selectedValue;
205 modifiers[UI_js_1.SelectionState.range_start] = Boolean(from && to && dateLib.isSameDay(date, from));
206 modifiers[UI_js_1.SelectionState.range_end] = Boolean(from && to && dateLib.isSameDay(date, to));
207 modifiers[UI_js_1.SelectionState.range_middle] =
208 (0, rangeIncludesDate_js_1.rangeIncludesDate)(selectedValue, date, true, dateLib);
209 }
210 const style = (0, getStyleForModifiers_js_1.getStyleForModifiers)(modifiers, styles, props.modifiersStyles);
211 const className = (0, getClassNamesForModifiers_js_1.getClassNamesForModifiers)(modifiers, classNames, props.modifiersClassNames);
212 const ariaLabel = !isInteractive
213 ? labelGridcell(date, modifiers, labelOptions, dateLib)
214 : undefined;
215 return (react_1.default.createElement(components.Day, { key: `${dateLib.format(date, "yyyy-MM-dd")}_${dateLib.format(day.displayMonth, "yyyy-MM")}`, day: day, modifiers: modifiers, role: "gridcell", className: className.join(" "), style: style, "aria-hidden": modifiers.hidden || undefined, "aria-selected": modifiers.selected || undefined, "aria-label": ariaLabel, "data-day": dateLib.format(date, "yyyy-MM-dd"), "data-month": day.outside
216 ? dateLib.format(date, "yyyy-MM")
217 : undefined, "data-selected": modifiers.selected || undefined, "data-disabled": modifiers.disabled || undefined, "data-hidden": modifiers.hidden || undefined, "data-outside": day.outside || undefined, "data-focused": modifiers.focused || undefined, "data-today": modifiers.today || undefined }, isInteractive ? (react_1.default.createElement(components.DayButton, { className: classNames[UI_js_1.UI.DayButton], style: styles?.[UI_js_1.UI.DayButton], day: day, modifiers: modifiers, disabled: modifiers.disabled || undefined, tabIndex: isFocusTarget(day) ? 0 : -1, "aria-label": labelDayButton(date, modifiers, labelOptions, dateLib), onClick: handleDayClick(day, modifiers), onBlur: handleDayBlur(day, modifiers), onFocus: handleDayFocus(day, modifiers), onKeyDown: handleDayKeyDown(day, modifiers), onMouseEnter: handleDayMouseEnter(day, modifiers), onMouseLeave: handleDayMouseLeave(day, modifiers) }, formatDay(date, formatOptions, dateLib))) : (formatDay(day.date, formatOptions, dateLib))));
218 })));
219 })))));
220 })),
221 props.footer && (react_1.default.createElement(components.Footer, { className: classNames[UI_js_1.UI.Footer], style: styles?.[UI_js_1.UI.Footer], role: "status", "aria-live": "polite" }, props.footer)))));
222}
223//# sourceMappingURL=DayPicker.js.map
\No newline at end of file