UNPKG

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