1 | import { DayFlag, SelectionState } from "./UI.js";
|
2 | import { CalendarDay } from "./classes/index.js";
|
3 | import type { DateLib, DayPickerProps, Modifiers } from "./types/index.js";
|
4 | import { dateMatchModifiers } from "./utils/dateMatchModifiers.js";
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 | export function useGetModifiers(
|
12 | days: CalendarDay[],
|
13 | props: DayPickerProps,
|
14 | dateLib: DateLib
|
15 | ) {
|
16 | const { disabled, hidden, modifiers, showOutsideDays, today } = props;
|
17 |
|
18 | const { isSameDay, isSameMonth, Date } = dateLib;
|
19 |
|
20 | const internal: Record<DayFlag, CalendarDay[]> = {
|
21 | [DayFlag.focused]: [],
|
22 | [DayFlag.outside]: [],
|
23 | [DayFlag.disabled]: [],
|
24 | [DayFlag.hidden]: [],
|
25 | [DayFlag.today]: []
|
26 | };
|
27 |
|
28 | const custom: Record<string, CalendarDay[]> = {};
|
29 |
|
30 | const selection: Record<SelectionState, CalendarDay[]> = {
|
31 | [SelectionState.range_end]: [],
|
32 | [SelectionState.range_middle]: [],
|
33 | [SelectionState.range_start]: [],
|
34 | [SelectionState.selected]: []
|
35 | };
|
36 |
|
37 | for (const day of days) {
|
38 | const { date, displayMonth } = day;
|
39 |
|
40 | const isOutside = Boolean(displayMonth && !isSameMonth(date, displayMonth));
|
41 |
|
42 | const isDisabled = Boolean(
|
43 | disabled && dateMatchModifiers(date, disabled, dateLib)
|
44 | );
|
45 |
|
46 | const isHidden =
|
47 | Boolean(hidden && dateMatchModifiers(date, hidden, dateLib)) ||
|
48 | (!showOutsideDays && isOutside);
|
49 |
|
50 | const isToday = isSameDay(date, today ?? new Date());
|
51 |
|
52 | if (isOutside) internal.outside.push(day);
|
53 | if (isDisabled) internal.disabled.push(day);
|
54 | if (isHidden) internal.hidden.push(day);
|
55 | if (isToday) internal.today.push(day);
|
56 |
|
57 |
|
58 | if (modifiers) {
|
59 | Object.keys(modifiers).forEach((name) => {
|
60 | const modifierValue = modifiers?.[name];
|
61 | const isMatch = modifierValue
|
62 | ? dateMatchModifiers(date, modifierValue, dateLib)
|
63 | : false;
|
64 | if (!isMatch) return;
|
65 | if (custom[name]) {
|
66 | custom[name].push(day);
|
67 | } else {
|
68 | custom[name] = [day];
|
69 | }
|
70 | });
|
71 | }
|
72 | }
|
73 |
|
74 | return (day: CalendarDay) => {
|
75 |
|
76 | const dayFlags: Record<DayFlag, boolean> = {
|
77 | [DayFlag.focused]: false,
|
78 | [DayFlag.disabled]: false,
|
79 | [DayFlag.hidden]: false,
|
80 | [DayFlag.outside]: false,
|
81 | [DayFlag.today]: false
|
82 | };
|
83 | const selectionStates: Record<SelectionState, boolean> = {
|
84 | [SelectionState.range_end]: false,
|
85 | [SelectionState.range_middle]: false,
|
86 | [SelectionState.range_start]: false,
|
87 | [SelectionState.selected]: false
|
88 | };
|
89 | const customModifiers: Modifiers = {};
|
90 |
|
91 |
|
92 | for (const name in internal) {
|
93 | const days = internal[name as DayFlag];
|
94 | dayFlags[name as DayFlag] = days.some((d) => d === day);
|
95 | }
|
96 | for (const name in selection) {
|
97 | const days = selection[name as SelectionState];
|
98 | selectionStates[name as SelectionState] = days.some((d) => d === day);
|
99 | }
|
100 | for (const name in custom) {
|
101 | customModifiers[name] = custom[name].some((d) => d === day);
|
102 | }
|
103 |
|
104 | return {
|
105 | ...selectionStates,
|
106 | ...dayFlags,
|
107 |
|
108 | ...customModifiers
|
109 | } as Modifiers;
|
110 | };
|
111 | }
|