UNPKG

3.87 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.useCalendar = useCalendar;
4const react_1 = require("react");
5const getDates_js_1 = require("./helpers/getDates.js");
6const getDays_js_1 = require("./helpers/getDays.js");
7const getDisplayMonths_js_1 = require("./helpers/getDisplayMonths.js");
8const getInitialMonth_js_1 = require("./helpers/getInitialMonth.js");
9const getMonths_js_1 = require("./helpers/getMonths.js");
10const getNavMonth_js_1 = require("./helpers/getNavMonth.js");
11const getNextMonth_js_1 = require("./helpers/getNextMonth.js");
12const getPreviousMonth_js_1 = require("./helpers/getPreviousMonth.js");
13const getWeeks_js_1 = require("./helpers/getWeeks.js");
14/** @private */
15function useCalendar(props, dateLib) {
16 const [navStart, navEnd] = (0, getNavMonth_js_1.getNavMonths)(props, dateLib);
17 const { startOfMonth, endOfMonth } = dateLib;
18 const initialMonth = (0, getInitialMonth_js_1.getInitialMonth)(props, dateLib);
19 const [firstMonth, setFirstMonth] = (0, react_1.useState)(initialMonth);
20 // Update the displayed month if `month` changes
21 (0, react_1.useEffect)(() => {
22 const initialDisplayMonth = (0, getInitialMonth_js_1.getInitialMonth)(props, dateLib);
23 setFirstMonth(initialDisplayMonth);
24 // eslint-disable-next-line react-hooks/exhaustive-deps
25 }, [props.month]);
26 // Update the displayed month if start/end month changes
27 (0, react_1.useEffect)(() => {
28 // TOFIX: this effect should do nothing if the current firstMonth is between
29 // startMonth and endMonth
30 const initialDisplayMonth = (0, getInitialMonth_js_1.getInitialMonth)(props, dateLib);
31 setFirstMonth(initialDisplayMonth);
32 // eslint-disable-next-line react-hooks/exhaustive-deps
33 }, [props.startMonth, props.endMonth]);
34 /** The months displayed in the calendar. */
35 const displayMonths = (0, getDisplayMonths_js_1.getDisplayMonths)(firstMonth, navEnd, props, dateLib);
36 /** The dates displayed in the calendar. */
37 const dates = (0, getDates_js_1.getDates)(displayMonths, props.endMonth ? endOfMonth(props.endMonth) : undefined, props, dateLib);
38 /** The Months displayed in the calendar. */
39 const months = (0, getMonths_js_1.getMonths)(displayMonths, dates, props, dateLib);
40 /** The Weeks displayed in the calendar. */
41 const weeks = (0, getWeeks_js_1.getWeeks)(months);
42 /** The Days displayed in the calendar. */
43 const days = (0, getDays_js_1.getDays)(months);
44 const previousMonth = (0, getPreviousMonth_js_1.getPreviousMonth)(firstMonth, navStart, props, dateLib);
45 const nextMonth = (0, getNextMonth_js_1.getNextMonth)(firstMonth, navEnd, props, dateLib);
46 const { disableNavigation, onMonthChange } = props;
47 const isDayInCalendar = (day) => weeks.some((week) => week.days.some((d) => d.isEqualTo(day)));
48 const goToMonth = (date) => {
49 if (disableNavigation) {
50 return;
51 }
52 let newMonth = startOfMonth(date);
53 // if month is before start, use the first month instead
54 if (navStart && newMonth < startOfMonth(navStart)) {
55 newMonth = startOfMonth(navStart);
56 }
57 // if month is after endMonth, use the last month instead
58 if (navEnd && newMonth > startOfMonth(navEnd)) {
59 newMonth = startOfMonth(navEnd);
60 }
61 setFirstMonth(newMonth);
62 onMonthChange?.(newMonth);
63 };
64 const goToDay = (day) => {
65 // is this check necessary?
66 if (isDayInCalendar(day)) {
67 return;
68 }
69 goToMonth(day.date);
70 };
71 const calendar = {
72 months,
73 weeks,
74 days,
75 navStart,
76 navEnd,
77 previousMonth,
78 nextMonth,
79 goToMonth,
80 goToDay
81 };
82 return calendar;
83}
84//# sourceMappingURL=useCalendar.js.map
\No newline at end of file