1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.useCalendar = useCalendar;
|
4 | const getDates_js_1 = require("./helpers/getDates.js");
|
5 | const getDays_js_1 = require("./helpers/getDays.js");
|
6 | const getDisplayMonths_js_1 = require("./helpers/getDisplayMonths.js");
|
7 | const getInitialMonth_js_1 = require("./helpers/getInitialMonth.js");
|
8 | const getMonths_js_1 = require("./helpers/getMonths.js");
|
9 | const getNavMonth_js_1 = require("./helpers/getNavMonth.js");
|
10 | const getNextMonth_js_1 = require("./helpers/getNextMonth.js");
|
11 | const getPreviousMonth_js_1 = require("./helpers/getPreviousMonth.js");
|
12 | const getWeeks_js_1 = require("./helpers/getWeeks.js");
|
13 | const useControlledValue_js_1 = require("./helpers/useControlledValue.js");
|
14 |
|
15 | function 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 |
|
20 | const [firstMonth, setFirstMonth] = (0, useControlledValue_js_1.useControlledValue)(initialDisplayMonth, props.month ? startOfMonth(props.month) : undefined);
|
21 |
|
22 | const displayMonths = (0, getDisplayMonths_js_1.getDisplayMonths)(firstMonth, navEnd, props, dateLib);
|
23 |
|
24 | const dates = (0, getDates_js_1.getDates)(displayMonths, props.endMonth, props, dateLib);
|
25 |
|
26 | const months = (0, getMonths_js_1.getMonths)(displayMonths, dates, props, dateLib);
|
27 |
|
28 | const weeks = (0, getWeeks_js_1.getWeeks)(months);
|
29 |
|
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 |
|
41 | if (navStart && newMonth < startOfMonth(navStart)) {
|
42 | newMonth = startOfMonth(navStart);
|
43 | }
|
44 |
|
45 | if (navEnd && newMonth > startOfMonth(navEnd)) {
|
46 | newMonth = startOfMonth(navEnd);
|
47 | }
|
48 | setFirstMonth(newMonth);
|
49 | onMonthChange?.(newMonth);
|
50 | };
|
51 | const goToDay = (day) => {
|
52 |
|
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 |
|
\ | No newline at end of file |