UNPKG

3.68 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.useGetModifiers = useGetModifiers;
4const UI_js_1 = require("./UI.js");
5const dateMatchModifiers_js_1 = require("./utils/dateMatchModifiers.js");
6/**
7 * Return a function to get the modifiers for a given day.
8 *
9 * @private
10 */
11function useGetModifiers(days, props, dateLib) {
12 const { disabled, hidden, modifiers, showOutsideDays, today } = props;
13 const { isSameDay, isSameMonth, Date } = dateLib;
14 const internal = {
15 [UI_js_1.DayFlag.focused]: [],
16 [UI_js_1.DayFlag.outside]: [],
17 [UI_js_1.DayFlag.disabled]: [],
18 [UI_js_1.DayFlag.hidden]: [],
19 [UI_js_1.DayFlag.today]: []
20 };
21 const custom = {};
22 const selection = {
23 [UI_js_1.SelectionState.range_end]: [],
24 [UI_js_1.SelectionState.range_middle]: [],
25 [UI_js_1.SelectionState.range_start]: [],
26 [UI_js_1.SelectionState.selected]: []
27 };
28 for (const day of days) {
29 const { date, displayMonth } = day;
30 const isOutside = Boolean(displayMonth && !isSameMonth(date, displayMonth));
31 const isDisabled = Boolean(disabled && (0, dateMatchModifiers_js_1.dateMatchModifiers)(date, disabled, dateLib));
32 const isHidden = Boolean(hidden && (0, dateMatchModifiers_js_1.dateMatchModifiers)(date, hidden, dateLib)) ||
33 (!showOutsideDays && isOutside);
34 const isToday = isSameDay(date, today ?? new Date());
35 if (isOutside)
36 internal.outside.push(day);
37 if (isDisabled)
38 internal.disabled.push(day);
39 if (isHidden)
40 internal.hidden.push(day);
41 if (isToday)
42 internal.today.push(day);
43 // Add custom modifiers
44 if (modifiers) {
45 Object.keys(modifiers).forEach((name) => {
46 const modifierValue = modifiers?.[name];
47 const isMatch = modifierValue
48 ? (0, dateMatchModifiers_js_1.dateMatchModifiers)(date, modifierValue, dateLib)
49 : false;
50 if (!isMatch)
51 return;
52 if (custom[name]) {
53 custom[name].push(day);
54 }
55 else {
56 custom[name] = [day];
57 }
58 });
59 }
60 }
61 return (day) => {
62 // Initialize all the modifiers to false
63 const dayFlags = {
64 [UI_js_1.DayFlag.focused]: false,
65 [UI_js_1.DayFlag.disabled]: false,
66 [UI_js_1.DayFlag.hidden]: false,
67 [UI_js_1.DayFlag.outside]: false,
68 [UI_js_1.DayFlag.today]: false
69 };
70 const selectionStates = {
71 [UI_js_1.SelectionState.range_end]: false,
72 [UI_js_1.SelectionState.range_middle]: false,
73 [UI_js_1.SelectionState.range_start]: false,
74 [UI_js_1.SelectionState.selected]: false
75 };
76 const customModifiers = {};
77 // Find the modifiers for the given day
78 for (const name in internal) {
79 const days = internal[name];
80 dayFlags[name] = days.some((d) => d === day);
81 }
82 for (const name in selection) {
83 const days = selection[name];
84 selectionStates[name] = days.some((d) => d === day);
85 }
86 for (const name in custom) {
87 customModifiers[name] = custom[name].some((d) => d === day);
88 }
89 return {
90 ...selectionStates,
91 ...dayFlags,
92 // custom modifiers should override all the previous ones
93 ...customModifiers
94 };
95 };
96}
97//# sourceMappingURL=useGetModifiers.js.map
\No newline at end of file