1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.useCalendar = useCalendar;
|
4 | const react_1 = require("react");
|
5 | const getDates_js_1 = require("./helpers/getDates.js");
|
6 | const getDays_js_1 = require("./helpers/getDays.js");
|
7 | const getDisplayMonths_js_1 = require("./helpers/getDisplayMonths.js");
|
8 | const getInitialMonth_js_1 = require("./helpers/getInitialMonth.js");
|
9 | const getMonths_js_1 = require("./helpers/getMonths.js");
|
10 | const getNavMonth_js_1 = require("./helpers/getNavMonth.js");
|
11 | const getNextMonth_js_1 = require("./helpers/getNextMonth.js");
|
12 | const getPreviousMonth_js_1 = require("./helpers/getPreviousMonth.js");
|
13 | const getWeeks_js_1 = require("./helpers/getWeeks.js");
|
14 |
|
15 | function 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 |
|
21 | (0, react_1.useEffect)(() => {
|
22 | const initialDisplayMonth = (0, getInitialMonth_js_1.getInitialMonth)(props, dateLib);
|
23 | setFirstMonth(initialDisplayMonth);
|
24 |
|
25 | }, [props.month]);
|
26 |
|
27 | (0, react_1.useEffect)(() => {
|
28 |
|
29 |
|
30 | const initialDisplayMonth = (0, getInitialMonth_js_1.getInitialMonth)(props, dateLib);
|
31 | setFirstMonth(initialDisplayMonth);
|
32 |
|
33 | }, [props.startMonth, props.endMonth]);
|
34 |
|
35 | const displayMonths = (0, getDisplayMonths_js_1.getDisplayMonths)(firstMonth, navEnd, props, dateLib);
|
36 |
|
37 | const dates = (0, getDates_js_1.getDates)(displayMonths, props.endMonth ? endOfMonth(props.endMonth) : undefined, props, dateLib);
|
38 |
|
39 | const months = (0, getMonths_js_1.getMonths)(displayMonths, dates, props, dateLib);
|
40 |
|
41 | const weeks = (0, getWeeks_js_1.getWeeks)(months);
|
42 |
|
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 |
|
54 | if (navStart && newMonth < startOfMonth(navStart)) {
|
55 | newMonth = startOfMonth(navStart);
|
56 | }
|
57 |
|
58 | if (navEnd && newMonth > startOfMonth(navEnd)) {
|
59 | newMonth = startOfMonth(navEnd);
|
60 | }
|
61 | setFirstMonth(newMonth);
|
62 | onMonthChange?.(newMonth);
|
63 | };
|
64 | const goToDay = (day) => {
|
65 |
|
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 |
|
\ | No newline at end of file |