UNPKG

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