@daryasidorovich/modsen-datepicker 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (183) hide show
  1. package/.babelrc.json +7 -0
  2. package/.github/workflows/chromatic.yml +25 -0
  3. package/.github/workflows/ci.yml +56 -0
  4. package/.husky/commit-msg +1 -0
  5. package/.husky/pre-commit +1 -0
  6. package/.prettierignore +16 -0
  7. package/.prettierrc +6 -0
  8. package/.storybook/main.js +93 -0
  9. package/.storybook/preview-head.html +6 -0
  10. package/.storybook/preview.js +26 -0
  11. package/.storybook/preview.tsx +18 -0
  12. package/README.md +302 -0
  13. package/commitlint.config.js +23 -0
  14. package/dist/components/BaseCalendar/index.d.ts +2 -0
  15. package/dist/components/CalendarGrid/CalendarGridWithTasks/Tooltip/index.d.ts +8 -0
  16. package/dist/components/CalendarGrid/CalendarGridWithTasks/index.d.ts +2 -0
  17. package/dist/components/CalendarGrid/CalendarGridWithoutTasks/CalendarGridWithoutTasks.test.d.ts +1 -0
  18. package/dist/components/CalendarGrid/CalendarGridWithoutTasks/index.d.ts +4 -0
  19. package/dist/components/CalendarGrid/index.d.ts +2 -0
  20. package/dist/components/CalendarGrid/types.d.ts +21 -0
  21. package/dist/components/Clear/index.d.ts +5 -0
  22. package/dist/components/DatePicker/index.d.ts +5 -0
  23. package/dist/components/Day/index.d.ts +16 -0
  24. package/dist/components/Input/index.d.ts +10 -0
  25. package/dist/components/Modal/CrudModal/index.d.ts +2 -0
  26. package/dist/components/Modal/index.d.ts +12 -0
  27. package/dist/components/Modal/types.d.ts +12 -0
  28. package/dist/components/Month/index.d.ts +7 -0
  29. package/dist/components/RangePicker/index.d.ts +6 -0
  30. package/dist/components/TasksManager/AddModal/index.d.ts +6 -0
  31. package/dist/components/TasksManager/DeleteModal/index.d.ts +6 -0
  32. package/dist/components/TasksManager/EditModal/index.d.ts +7 -0
  33. package/dist/components/TasksManager/index.d.ts +9 -0
  34. package/dist/components/Week/index.d.ts +17 -0
  35. package/dist/components/WeekDay/index.d.ts +7 -0
  36. package/dist/constants/dateMask.d.ts +21 -0
  37. package/dist/constants/firstDayOfWeek.d.ts +5 -0
  38. package/dist/constants/holidays.d.ts +1 -0
  39. package/dist/constants/modes.d.ts +5 -0
  40. package/dist/constants/monthsNames.d.ts +1 -0
  41. package/dist/constants/weekdaysNames.d.ts +1 -0
  42. package/dist/hooks/useCalendar.d.ts +7 -0
  43. package/dist/hooks/useLockBodyScroll.d.ts +2 -0
  44. package/dist/hooks/useModal.d.ts +13 -0
  45. package/dist/hooks/useTooltip.d.ts +9 -0
  46. package/dist/hooks/useValidate.d.ts +11 -0
  47. package/dist/index.cjs +17250 -0
  48. package/dist/index.cjs.map +1 -0
  49. package/dist/index.d.ts +11 -0
  50. package/dist/index.js +17233 -0
  51. package/dist/index.js.map +1 -0
  52. package/dist/services/calendarService.d.ts +19 -0
  53. package/dist/services/decorators/withHolidays.d.ts +2 -0
  54. package/dist/services/decorators/withHolidays.test.d.ts +1 -0
  55. package/dist/services/decorators/withMinMax.d.ts +2 -0
  56. package/dist/services/decorators/withMinMax.test.d.ts +1 -0
  57. package/dist/services/holidayService.d.ts +5 -0
  58. package/dist/setupTests.d.ts +1 -0
  59. package/dist/store/localStorage.d.ts +3 -0
  60. package/dist/store/tasksStore.d.ts +8 -0
  61. package/dist/store/tasksStore.test.d.ts +1 -0
  62. package/dist/store/types.d.ts +18 -0
  63. package/dist/utils/calendar.d.ts +7 -0
  64. package/dist/utils/calendar.test.d.ts +1 -0
  65. package/dist/utils/controlDate.d.ts +1 -0
  66. package/dist/utils/dateChecks.d.ts +10 -0
  67. package/dist/utils/dateChecks.test.d.ts +1 -0
  68. package/dist/utils/formatDate.d.ts +3 -0
  69. package/dist/utils/formatDate.test.d.ts +1 -0
  70. package/dist/utils/schema.d.ts +6 -0
  71. package/dist/utils/schema.test.d.ts +1 -0
  72. package/dist/utils/weekdayOrder.d.ts +1 -0
  73. package/dist/utils/weekdayOrder.test.d.ts +1 -0
  74. package/docs/base-calendar.png +0 -0
  75. package/docs/dark-theme.png +0 -0
  76. package/docs/datepicker.png +0 -0
  77. package/docs/rangepicker-with-error.png +0 -0
  78. package/docs/rangepicker.png +0 -0
  79. package/eslint.config.ts +80 -0
  80. package/jest.config.json +21 -0
  81. package/package.json +106 -0
  82. package/rollup.config.js +65 -0
  83. package/src/assets/actions/Delete.svg +4 -0
  84. package/src/assets/actions/Edit.svg +3 -0
  85. package/src/assets/arrows/Next.svg +4 -0
  86. package/src/assets/arrows/Prev.svg +4 -0
  87. package/src/assets/input/Calendar.svg +3 -0
  88. package/src/assets/input/Clear.svg +3 -0
  89. package/src/components/BaseCalendar/BaseCalendar.stories.tsx +77 -0
  90. package/src/components/BaseCalendar/index.tsx +63 -0
  91. package/src/components/BaseCalendar/styles.module.scss +15 -0
  92. package/src/components/CalendarGrid/CalendarGrid.stories.tsx +136 -0
  93. package/src/components/CalendarGrid/CalendarGridWithTasks/Tooltip/index.tsx +27 -0
  94. package/src/components/CalendarGrid/CalendarGridWithTasks/Tooltip/styles.module.scss +9 -0
  95. package/src/components/CalendarGrid/CalendarGridWithTasks/index.tsx +51 -0
  96. package/src/components/CalendarGrid/CalendarGridWithoutTasks/CalendarGridWithoutTasks.test.tsx +223 -0
  97. package/src/components/CalendarGrid/CalendarGridWithoutTasks/index.tsx +36 -0
  98. package/src/components/CalendarGrid/CalendarGridWithoutTasks/styles.module.scss +11 -0
  99. package/src/components/CalendarGrid/index.tsx +19 -0
  100. package/src/components/CalendarGrid/types.ts +26 -0
  101. package/src/components/Clear/Clear.stories.tsx +16 -0
  102. package/src/components/Clear/index.tsx +12 -0
  103. package/src/components/Clear/styles.module.scss +34 -0
  104. package/src/components/DatePicker/DatePicker.stories.tsx +127 -0
  105. package/src/components/DatePicker/index.tsx +72 -0
  106. package/src/components/DatePicker/styles.module.scss +10 -0
  107. package/src/components/Day/Day.stories.tsx +75 -0
  108. package/src/components/Day/index.tsx +95 -0
  109. package/src/components/Day/styles.module.scss +97 -0
  110. package/src/components/Input/Input.stories.tsx +37 -0
  111. package/src/components/Input/index.tsx +64 -0
  112. package/src/components/Input/styles.module.scss +79 -0
  113. package/src/components/Modal/CrudModal/index.tsx +43 -0
  114. package/src/components/Modal/CrudModal/styles.module.scss +44 -0
  115. package/src/components/Modal/index.tsx +66 -0
  116. package/src/components/Modal/styles.module.scss +40 -0
  117. package/src/components/Modal/types.ts +13 -0
  118. package/src/components/Month/Month.stories.tsx +19 -0
  119. package/src/components/Month/index.tsx +31 -0
  120. package/src/components/Month/styles.module.scss +30 -0
  121. package/src/components/RangePicker/RangePicker.stories.tsx +134 -0
  122. package/src/components/RangePicker/index.tsx +144 -0
  123. package/src/components/RangePicker/styles.module.scss +19 -0
  124. package/src/components/TasksManager/AddModal/index.tsx +62 -0
  125. package/src/components/TasksManager/DeleteModal/index.tsx +22 -0
  126. package/src/components/TasksManager/EditModal/index.tsx +96 -0
  127. package/src/components/TasksManager/index.tsx +112 -0
  128. package/src/components/TasksManager/styles.module.scss +62 -0
  129. package/src/components/Week/Week.stories.tsx +49 -0
  130. package/src/components/Week/index.tsx +68 -0
  131. package/src/components/Week/styles.module.scss +7 -0
  132. package/src/components/WeekDay/WeekDay.stories.tsx +38 -0
  133. package/src/components/WeekDay/index.tsx +35 -0
  134. package/src/components/WeekDay/styles.module.scss +19 -0
  135. package/src/constants/dateMask.ts +23 -0
  136. package/src/constants/firstDayOfWeek.ts +7 -0
  137. package/src/constants/holidays.ts +32 -0
  138. package/src/constants/modes.ts +6 -0
  139. package/src/constants/monthsNames.ts +14 -0
  140. package/src/constants/weekdaysNames.ts +1 -0
  141. package/src/hooks/useCalendar.ts +62 -0
  142. package/src/hooks/useLockBodyScroll.ts +19 -0
  143. package/src/hooks/useModal.ts +45 -0
  144. package/src/hooks/useTooltip.ts +41 -0
  145. package/src/hooks/useValidate.ts +80 -0
  146. package/src/index.ts +15 -0
  147. package/src/services/calendarService.ts +62 -0
  148. package/src/services/decorators/withHolidays.test.ts +64 -0
  149. package/src/services/decorators/withHolidays.ts +12 -0
  150. package/src/services/decorators/withMinMax.test.ts +77 -0
  151. package/src/services/decorators/withMinMax.ts +19 -0
  152. package/src/services/holidayService.ts +56 -0
  153. package/src/setupTests.ts +1 -0
  154. package/src/store/localStorage.ts +33 -0
  155. package/src/store/tasksStore.test.tsx +208 -0
  156. package/src/store/tasksStore.tsx +146 -0
  157. package/src/store/types.ts +22 -0
  158. package/src/styles/_border.scss +9 -0
  159. package/src/styles/_colors.scss +53 -0
  160. package/src/styles/_gap.scss +4 -0
  161. package/src/styles/_globals.scss +61 -0
  162. package/src/styles/_grid.scss +9 -0
  163. package/src/styles/_mixins.scss +6 -0
  164. package/src/styles/_reset.scss +98 -0
  165. package/src/styles/_theme-mixins.scss +51 -0
  166. package/src/styles/_typography.scss +10 -0
  167. package/src/styles/_variables.scss +8 -0
  168. package/src/styles/main.scss +3 -0
  169. package/src/types/types.d.ts +15 -0
  170. package/src/utils/calendar.test.ts +154 -0
  171. package/src/utils/calendar.ts +82 -0
  172. package/src/utils/controlDate.ts +3 -0
  173. package/src/utils/dateChecks.test.ts +216 -0
  174. package/src/utils/dateChecks.ts +75 -0
  175. package/src/utils/formatDate.test.ts +57 -0
  176. package/src/utils/formatDate.ts +27 -0
  177. package/src/utils/schema.test.ts +40 -0
  178. package/src/utils/schema.ts +8 -0
  179. package/src/utils/weekdayOrder.test.ts +11 -0
  180. package/src/utils/weekdayOrder.ts +7 -0
  181. package/tsconfig.json +31 -0
  182. package/vitest.config.ts +38 -0
  183. package/vitest.shims.d.ts +1 -0
@@ -0,0 +1,15 @@
1
+ declare module '*.module.css' {
2
+ const classes: { [key: string]: string };
3
+ export default classes;
4
+ }
5
+
6
+ declare module '*.module.scss' {
7
+ const classes: { [key: string]: string };
8
+ export default classes;
9
+ }
10
+
11
+ declare module '*.svg?react' {
12
+ import React from 'react';
13
+ const SVG: React.FC<React.SVGProps<SVGSVGElement>>;
14
+ export default SVG;
15
+ }
@@ -0,0 +1,154 @@
1
+ import {
2
+ getDaysInMonth,
3
+ getFirstDayOfMonth,
4
+ getLastDayOfMonth,
5
+ getMonthDays,
6
+ getWeekDayIndex,
7
+ getWeekDays,
8
+ getWeeksFromDays,
9
+ } from './calendar';
10
+
11
+ describe('getFirstDayOfMonth:', () => {
12
+ it('returns the first day of month', () => {
13
+ const result = getFirstDayOfMonth(new Date(2026, 8, 18));
14
+ expect(result.getMonth()).toBe(8);
15
+ expect(result.getDate()).toBe(1);
16
+ });
17
+ });
18
+
19
+ describe('getLastDayOfMonth:', () => {
20
+ it('returns the last day of month', () => {
21
+ const result = getLastDayOfMonth(new Date(2026, 0, 10));
22
+ expect(result.getDate()).toBe(31);
23
+ expect(result.getMonth()).toBe(0);
24
+ });
25
+
26
+ it('handles February in a leap year', () => {
27
+ const result = getLastDayOfMonth(new Date(2024, 1, 1));
28
+ expect(result.getDate()).toBe(29);
29
+ });
30
+
31
+ it('handles February in a non-leap year', () => {
32
+ const result = getLastDayOfMonth(new Date(2025, 1, 1));
33
+ expect(result.getDate()).toBe(28);
34
+ });
35
+ });
36
+
37
+ describe('getDaysInMonth:', () => {
38
+ it('returns count of days in the month', () => {
39
+ expect(getDaysInMonth(new Date(2026, 8, 1))).toBe(30);
40
+ expect(getDaysInMonth(new Date(2026, 7, 1))).toBe(31);
41
+ expect(getDaysInMonth(new Date(2026, 6, 1))).toBe(31);
42
+ expect(getDaysInMonth(new Date(2023, 5, 1))).toBe(30);
43
+ });
44
+ });
45
+
46
+ describe('getWeekDayIndex:', () => {
47
+ it('return 0 for Sunday if the first of week is 0', () => {
48
+ const sunday = new Date(2026, 8, 6);
49
+ expect(getWeekDayIndex(sunday, 0)).toBe(0);
50
+ });
51
+
52
+ it('return 1 for Monday if the first of week is 0', () => {
53
+ const monday = new Date(2026, 8, 7);
54
+ expect(getWeekDayIndex(monday, 0)).toBe(1);
55
+ });
56
+
57
+ it('return 6 for Sunday if the first of week is 1', () => {
58
+ const sunday = new Date(2026, 8, 6);
59
+ expect(getWeekDayIndex(sunday, 1)).toBe(6);
60
+ });
61
+
62
+ it('return 0 for Monday if the first of week is 1', () => {
63
+ const monday = new Date(2026, 8, 7);
64
+ expect(getWeekDayIndex(monday, 1)).toBe(0);
65
+ });
66
+
67
+ it('uses firstDayOfWeek = 1 by default', () => {
68
+ const monday = new Date(2026, 8, 7);
69
+ expect(getWeekDayIndex(monday)).toBe(0);
70
+ });
71
+ });
72
+
73
+ describe('getMonthDays:', () => {
74
+ it('returns a grid with a step of 7', () => {
75
+ const result = getMonthDays(new Date(2026, 8, 1));
76
+ expect(result.length % 7).toBe(0);
77
+ });
78
+
79
+ it('includes all month days', () => {
80
+ const result = getMonthDays(new Date(2026, 8, 1));
81
+ const daysInCurrentMonth = result.filter(
82
+ (day) => day.getMonth() === 8 && day.getFullYear() === 2026
83
+ );
84
+ expect(daysInCurrentMonth.length).toBe(30);
85
+ });
86
+
87
+ it('the first day of a grid is Monday if firstDayOfWeek = 1', () => {
88
+ const result = getMonthDays(new Date(2026, 8, 1), 1);
89
+ expect(result[0].getDay()).toBe(1);
90
+ });
91
+
92
+ it('the first day of a grid is Sunday if firstDayOfWeek = 0', () => {
93
+ const result = getMonthDays(new Date(2026, 8, 1), 0);
94
+ expect(result[0].getDay()).toBe(0);
95
+ });
96
+
97
+ it('correctly adds days of the previous and the next monthes', () => {
98
+ const result = getMonthDays(new Date(2026, 7, 1), 1);
99
+ expect(result[0].getMonth()).toBe(6);
100
+ expect(result[4].getMonth()).toBe(6);
101
+ expect(result[result.length - 1].getMonth()).toBe(8);
102
+ expect(result[result.length - 6].getMonth()).toBe(8);
103
+ });
104
+ });
105
+
106
+ describe('getWeeksFromDays:', () => {
107
+ const mockDays: Date[] = Array.from(
108
+ { length: 30 },
109
+ (_, i) => new Date(2026, 8, i + 1)
110
+ );
111
+
112
+ it('splits the array of days into weeks of 7', () => {
113
+ const weeks = getWeeksFromDays(mockDays, 7);
114
+ expect(weeks.length).toBe(5);
115
+ expect(weeks[0].length).toBe(7);
116
+ expect(weeks[3].length).toBe(7);
117
+ });
118
+
119
+ it('splits the array of days into weeks of 5', () => {
120
+ const weeks = getWeeksFromDays(mockDays, 5);
121
+ weeks.forEach((week) => expect(week.length).toBe(5));
122
+ });
123
+
124
+ it('handles correctly the last incomplete week', () => {
125
+ const weeks = getWeeksFromDays(mockDays);
126
+ expect(weeks[0].length).toBe(7);
127
+ expect(weeks[4].length).toBe(2);
128
+ });
129
+
130
+ it('returns emply array for an empty entry array', () => {
131
+ expect(getWeeksFromDays([], 7)).toEqual([]);
132
+ });
133
+ });
134
+
135
+ describe('getWeekDays:', () => {
136
+ it('return 7 days', () => {
137
+ const result = getWeekDays(new Date(2026, 8, 16));
138
+ expect(result.length).toBe(7);
139
+ });
140
+
141
+ it('starts from Monday if firstDayOfWeek = 1', () => {
142
+ const wednesday = new Date(2026, 8, 16);
143
+ const result = getWeekDays(wednesday, 1);
144
+ expect(result[0].getDay()).toBe(1);
145
+ expect(result[0].getDate()).toBe(14);
146
+ });
147
+
148
+ it('starts from Sunday if firstDayOfWeek = 0', () => {
149
+ const wednesday = new Date(2026, 8, 16);
150
+ const result = getWeekDays(wednesday, 0);
151
+ expect(result[0].getDay()).toBe(0);
152
+ expect(result[0].getDate()).toBe(13);
153
+ });
154
+ });
@@ -0,0 +1,82 @@
1
+ export const getFirstDayOfMonth = (date: Date): Date => {
2
+ return new Date(date.getFullYear(), date.getMonth(), 1);
3
+ };
4
+
5
+ export const getLastDayOfMonth = (date: Date): Date => {
6
+ return new Date(date.getFullYear(), date.getMonth() + 1, 0);
7
+ };
8
+
9
+ export const getDaysInMonth = (date: Date): number => {
10
+ return getLastDayOfMonth(date).getDate();
11
+ };
12
+
13
+ export const getWeekDayIndex = (
14
+ date: Date,
15
+ firstDayOfWeek: 0 | 1 = 1
16
+ ): number => {
17
+ let index = date.getDay();
18
+ if (firstDayOfWeek === 1) {
19
+ index = index === 0 ? 6 : index - 1;
20
+ }
21
+ return index;
22
+ };
23
+
24
+ export const getMonthDays = (date: Date, firstDayOfWeek: 0 | 1 = 1): Date[] => {
25
+ const year = date.getFullYear();
26
+ const month = date.getMonth();
27
+
28
+ const firstDay = getFirstDayOfMonth(date);
29
+ const daysInMonth = getDaysInMonth(date);
30
+ const firstDayIndex = getWeekDayIndex(firstDay, firstDayOfWeek);
31
+
32
+ const totalDays = Math.ceil((firstDayIndex + daysInMonth) / 7) * 7;
33
+ const days: Date[] = [];
34
+
35
+ for (let i = 0; i < firstDayIndex; i++) {
36
+ days.push(new Date(year, month, 1 - firstDayIndex + i));
37
+ }
38
+
39
+ for (let i = 1; i <= daysInMonth; i++) {
40
+ days.push(new Date(year, month, i));
41
+ }
42
+
43
+ const remaining = totalDays - days.length;
44
+ for (let i = 1; i <= remaining; i++) {
45
+ days.push(new Date(year, month + 1, i));
46
+ }
47
+
48
+ return days;
49
+ };
50
+
51
+ export const getWeeksFromDays = (
52
+ days: Date[],
53
+ weekLength: 5 | 7 = 7
54
+ ): Date[][] => {
55
+ const weeks: Date[][] = [];
56
+ for (let i = 0; i < days.length; i += weekLength) {
57
+ weeks.push(days.slice(i, i + weekLength));
58
+ }
59
+ return weeks;
60
+ };
61
+
62
+ export const getWeekDays = (date: Date, firstDayOfWeek: 0 | 1 = 1): Date[] => {
63
+ const year = date.getFullYear();
64
+ const month = date.getMonth();
65
+ const day = date.getDate();
66
+
67
+ const currentDayOfWeek = date.getDay();
68
+
69
+ const offset =
70
+ firstDayOfWeek === 1 ? (currentDayOfWeek + 6) % 7 : currentDayOfWeek;
71
+
72
+ const startDate = new Date(year, month, day - offset);
73
+
74
+ const weekDays: Date[] = [];
75
+ for (let i = 0; i < 7; i++) {
76
+ const newDate = new Date(startDate);
77
+ newDate.setDate(startDate.getDate() + i);
78
+ weekDays.push(newDate);
79
+ }
80
+
81
+ return weekDays;
82
+ };
@@ -0,0 +1,3 @@
1
+ export const setControlDate = (date: Date | undefined) => {
2
+ return date ? new Date(date) : undefined;
3
+ };
@@ -0,0 +1,216 @@
1
+ import {
2
+ isCurrentMonth,
3
+ isDisabled,
4
+ isInRange,
5
+ isRangeEnd,
6
+ isRangeStart,
7
+ isSameDay,
8
+ isSelected,
9
+ isToday,
10
+ isWeekend,
11
+ } from './dateChecks';
12
+
13
+ describe('dateChecks:', () => {
14
+ describe('isSameDay:', () => {
15
+ it('returns true for same day', () => {
16
+ const date1 = new Date(2026, 7, 25);
17
+ const date2 = new Date(2026, 7, 25);
18
+ expect(isSameDay(date1, date2)).toBe(true);
19
+ });
20
+
21
+ it('returns false for different days', () => {
22
+ const date1 = new Date(2026, 7, 25);
23
+ const date2 = new Date(2026, 7, 26);
24
+ expect(isSameDay(date1, date2)).toBe(false);
25
+ });
26
+
27
+ it('returns false for different months', () => {
28
+ const date1 = new Date(2026, 7, 25);
29
+ const date2 = new Date(2026, 8, 25);
30
+ expect(isSameDay(date1, date2)).toBe(false);
31
+ });
32
+
33
+ it('returns false for different years', () => {
34
+ const date1 = new Date(2026, 7, 25);
35
+ const date2 = new Date(2027, 7, 25);
36
+ expect(isSameDay(date1, date2)).toBe(false);
37
+ });
38
+ });
39
+
40
+ describe('isCurrentMonth:', () => {
41
+ it('returns true for same month and year', () => {
42
+ const date = new Date(2026, 7, 25);
43
+ const monthDate = new Date(2026, 7, 1);
44
+ expect(isCurrentMonth(date, monthDate)).toBe(true);
45
+ });
46
+
47
+ it('returns false for different month', () => {
48
+ const date = new Date(2026, 9, 1);
49
+ const monthDate = new Date(2026, 8, 1);
50
+ expect(isCurrentMonth(date, monthDate)).toBe(false);
51
+ });
52
+
53
+ it('returns false for different year', () => {
54
+ const date = new Date(2026, 9, 25);
55
+ const monthDate = new Date(2027, 9, 1);
56
+ expect(isCurrentMonth(date, monthDate)).toBe(false);
57
+ });
58
+ });
59
+
60
+ describe('isWeekend:', () => {
61
+ it('returns true for Sunday', () => {
62
+ const sunday = new Date(2026, 8, 6);
63
+ expect(isWeekend(sunday)).toBe(true);
64
+ });
65
+
66
+ it('returns true for Saturday', () => {
67
+ const saturday = new Date(2026, 8, 5);
68
+ expect(isWeekend(saturday)).toBe(true);
69
+ });
70
+
71
+ it('returns false for Wednesday', () => {
72
+ const wednesday = new Date(2026, 8, 9);
73
+ expect(isWeekend(wednesday)).toBe(false);
74
+ });
75
+ });
76
+
77
+ describe('isToday', () => {
78
+ it('returns true for today', () => {
79
+ const today = new Date();
80
+ expect(isToday(today)).toBe(true);
81
+ });
82
+
83
+ it('returns false for random date', () => {
84
+ const random = new Date(2026, 2, 9);
85
+ expect(isToday(random)).toBe(false);
86
+ });
87
+ });
88
+
89
+ describe('isSelected:', () => {
90
+ it('returns true when date is selected', () => {
91
+ const date = new Date(2026, 7, 25);
92
+ const selectedDate = new Date(2026, 7, 25);
93
+ expect(isSelected(date, selectedDate)).toBe(true);
94
+ });
95
+
96
+ it('returns false when date is not selected', () => {
97
+ const date = new Date(2026, 7, 25);
98
+ const selectedDate = new Date(2026, 7, 26);
99
+ expect(isSelected(date, selectedDate)).toBe(false);
100
+ });
101
+
102
+ it('returns false when selectedDate is undefined', () => {
103
+ const date = new Date(2026, 7, 25);
104
+ expect(isSelected(date, undefined)).toBe(false);
105
+ });
106
+ });
107
+
108
+ describe('isDisabled', () => {
109
+ it('returns true when date is before minDate', () => {
110
+ const date = new Date(2026, 7, 25);
111
+ const minDate = new Date(2026, 7, 26);
112
+ expect(isDisabled(date, minDate)).toBe(true);
113
+ });
114
+
115
+ it('returns true when date is after maxDate', () => {
116
+ const date = new Date(2026, 7, 25);
117
+ const maxDate = new Date(2026, 7, 24);
118
+ expect(isDisabled(date, null, maxDate)).toBe(true);
119
+ });
120
+
121
+ it('returns false when date is within range', () => {
122
+ const date = new Date(2026, 7, 25);
123
+ const minDate = new Date(2026, 7, 20);
124
+ const maxDate = new Date(2026, 7, 30);
125
+ expect(isDisabled(date, minDate, maxDate)).toBe(false);
126
+ });
127
+
128
+ it('returns false when minDate and maxDate are null', () => {
129
+ const date = new Date(2026, 7, 25);
130
+ expect(isDisabled(date, null, null)).toBe(false);
131
+ });
132
+
133
+ it('returns false when date equals minDate', () => {
134
+ const date = new Date(2026, 7, 25);
135
+ const minDate = new Date(2026, 7, 25);
136
+ expect(isDisabled(date, minDate, null)).toBe(false);
137
+ });
138
+
139
+ it('returns false when date equals maxDate', () => {
140
+ const date = new Date(2026, 7, 25);
141
+ const maxDate = new Date(2026, 7, 25);
142
+ expect(isDisabled(date, null, maxDate)).toBe(false);
143
+ });
144
+ });
145
+
146
+ describe('isInRange:', () => {
147
+ it('returns true when date is between start and end', () => {
148
+ const date = new Date(2026, 7, 25);
149
+ const start = new Date(2026, 7, 20);
150
+ const end = new Date(2026, 7, 30);
151
+ expect(isInRange(date, start, end)).toBe(true);
152
+ });
153
+
154
+ it('returns false when date equals start', () => {
155
+ const date = new Date(2026, 7, 20);
156
+ const start = new Date(2026, 7, 20);
157
+ const end = new Date(2026, 7, 30);
158
+ expect(isInRange(date, start, end)).toBe(false);
159
+ });
160
+
161
+ it('returns false when date equals end', () => {
162
+ const date = new Date(2026, 7, 30);
163
+ const start = new Date(2026, 7, 20);
164
+ const end = new Date(2026, 7, 30);
165
+ expect(isInRange(date, start, end)).toBe(false);
166
+ });
167
+
168
+ it('returns false when start is undefined', () => {
169
+ const date = new Date(2026, 7, 25);
170
+ expect(isInRange(date, undefined, new Date(2026, 7, 30))).toBe(false);
171
+ });
172
+
173
+ it('returns false when end is undefined', () => {
174
+ const date = new Date(2026, 7, 25);
175
+ expect(isInRange(date, new Date(2026, 7, 20), undefined)).toBe(false);
176
+ });
177
+ });
178
+
179
+ describe('isRangeStart', () => {
180
+ it('returns true when date equals start', () => {
181
+ const date = new Date(2026, 7, 25);
182
+ const start = new Date(2026, 7, 25);
183
+ expect(isRangeStart(date, start)).toBe(true);
184
+ });
185
+
186
+ it('returns false when date does not equal start', () => {
187
+ const date = new Date(2026, 7, 25);
188
+ const start = new Date(2026, 7, 26);
189
+ expect(isRangeStart(date, start)).toBe(false);
190
+ });
191
+
192
+ it('returns false when start is undefined', () => {
193
+ const date = new Date(2026, 7, 25);
194
+ expect(isRangeStart(date, undefined)).toBe(false);
195
+ });
196
+ });
197
+
198
+ describe('isRangeEnd:', () => {
199
+ it('returns true when date equals end', () => {
200
+ const date = new Date(2026, 7, 25);
201
+ const end = new Date(2026, 7, 25);
202
+ expect(isRangeEnd(date, end)).toBe(true);
203
+ });
204
+
205
+ it('returns false when date does not equal end', () => {
206
+ const date = new Date(2026, 7, 25);
207
+ const end = new Date(2026, 7, 26);
208
+ expect(isRangeEnd(date, end)).toBe(false);
209
+ });
210
+
211
+ it('returns false when end is undefined', () => {
212
+ const date = new Date(2026, 7, 25);
213
+ expect(isRangeEnd(date, undefined)).toBe(false);
214
+ });
215
+ });
216
+ });
@@ -0,0 +1,75 @@
1
+ import { formatDateForInput } from './formatDate';
2
+
3
+ export const isSameDay = (date1: Date, date2: Date) => {
4
+ return (
5
+ date1.getFullYear() === date2.getFullYear() &&
6
+ date1.getMonth() === date2.getMonth() &&
7
+ date1.getDate() === date2.getDate()
8
+ );
9
+ };
10
+
11
+ export const isCurrentMonth = (date: Date, monthDate: Date) => {
12
+ return (
13
+ date.getMonth() === monthDate.getMonth() &&
14
+ date.getFullYear() === monthDate.getFullYear()
15
+ );
16
+ };
17
+
18
+ export const isWeekend = (date: Date) => {
19
+ return date.getDay() === 0 || date.getDay() === 6;
20
+ };
21
+
22
+ export const isToday = (date: Date) => {
23
+ return isSameDay(date, new Date());
24
+ };
25
+
26
+ export const isSelected = (date: Date, selectedDate?: Date) => {
27
+ if (!selectedDate) return false;
28
+ return isSameDay(date, selectedDate);
29
+ };
30
+
31
+ export const isDisabled = (
32
+ date: Date,
33
+ minDate?: Date | null,
34
+ maxDate?: Date | null
35
+ ): boolean => {
36
+ if (minDate && date < minDate) return true;
37
+ if (maxDate && date > maxDate) return true;
38
+ return false;
39
+ };
40
+
41
+ export const isInRange = (date: Date, start?: Date, end?: Date) => {
42
+ if (!start || !end) return false;
43
+
44
+ const dateStart = new Date(
45
+ start.getFullYear(),
46
+ start.getMonth(),
47
+ start.getDate()
48
+ );
49
+ const dateEnd = new Date(end.getFullYear(), end.getMonth(), end.getDate());
50
+ const dateToCheck = new Date(
51
+ date.getFullYear(),
52
+ date.getMonth(),
53
+ date.getDate()
54
+ );
55
+
56
+ return dateToCheck > dateStart && dateToCheck < dateEnd;
57
+ };
58
+
59
+ export const isRangeStart = (date: Date, start?: Date) => {
60
+ if (!start) return false;
61
+ return isSameDay(date, start);
62
+ };
63
+
64
+ export const isRangeEnd = (date: Date, end?: Date) => {
65
+ if (!end) return false;
66
+ return isSameDay(date, end);
67
+ };
68
+
69
+ export const getRangeError = (date: Date, startDate: Date): string => {
70
+ if (startDate && date < startDate) {
71
+ return `Date must be after ${formatDateForInput(startDate)}`;
72
+ }
73
+
74
+ return '';
75
+ };
@@ -0,0 +1,57 @@
1
+ import { formatDate, formatDateForInput, parseDate } from './formatDate';
2
+
3
+ describe('formatting dates:', () => {
4
+ test('formatDate:', () => {
5
+ const date1 = new Date(2026, 0, 3);
6
+ const date2 = new Date(2024, 5, 15);
7
+ const date3 = new Date(2000, 11, 31);
8
+ const date4 = new Date(1999, 8, 9);
9
+ const date5 = new Date(2025, 2, 1);
10
+
11
+ expect(formatDate(date1)).toBe('2026-01-03');
12
+ expect(formatDate(date2)).toBe('2024-06-15');
13
+ expect(formatDate(date3)).toBe('2000-12-31');
14
+ expect(formatDate(date4)).toBe('1999-09-09');
15
+ expect(formatDate(date5)).toBe('2025-03-01');
16
+ });
17
+
18
+ test('formatDateForInput:', () => {
19
+ const date1 = new Date(2026, 0, 3);
20
+ const date2 = new Date(2024, 5, 15);
21
+ const date3 = new Date(2000, 11, 31);
22
+ const date4 = new Date(1999, 8, 9);
23
+ const date5 = new Date(2025, 2, 1);
24
+
25
+ expect(formatDateForInput(date1)).toBe('03/01/2026');
26
+ expect(formatDateForInput(date2)).toBe('15/06/2024');
27
+ expect(formatDateForInput(date3)).toBe('31/12/2000');
28
+ expect(formatDateForInput(date4)).toBe('09/09/1999');
29
+ expect(formatDateForInput(date5)).toBe('01/03/2025');
30
+ });
31
+
32
+ describe('parseDate:', () => {
33
+ test('returns parsed date', () => {
34
+ const date1 = '03/03/2026';
35
+ const date2 = '15/06/2024';
36
+ const date3 = '31/12/2000';
37
+ const date4 = '09/09/1999';
38
+ const date5 = '01/03/2025';
39
+
40
+ expect(parseDate(date1)).toEqual(new Date(2026, 2, 3));
41
+ expect(parseDate(date2)).toEqual(new Date(2024, 5, 15));
42
+ expect(parseDate(date3)).toEqual(new Date(2000, 11, 31));
43
+ expect(parseDate(date4)).toEqual(new Date(1999, 8, 9));
44
+ expect(parseDate(date5)).toEqual(new Date(2025, 2, 1));
45
+ });
46
+
47
+ test('returns null id date is invalid', () => {
48
+ const date1 = '31/02/2026';
49
+ const date2 = '00/06/2024';
50
+ const date3 = '31/14/2000';
51
+
52
+ expect(parseDate(date1)).toBeNull();
53
+ expect(parseDate(date2)).toBeNull();
54
+ expect(parseDate(date3)).toBeNull();
55
+ });
56
+ });
57
+ });
@@ -0,0 +1,27 @@
1
+ export const formatDate = (date: Date): string => {
2
+ const year = date.getFullYear();
3
+ const month = String(date.getMonth() + 1).padStart(2, '0');
4
+ const day = String(date.getDate()).padStart(2, '0');
5
+ return `${year}-${month}-${day}`;
6
+ };
7
+
8
+ export const formatDateForInput = (date: Date): string => {
9
+ const year = date.getFullYear();
10
+ const month = String(date.getMonth() + 1).padStart(2, '0');
11
+ const day = String(date.getDate()).padStart(2, '0');
12
+ return `${day}/${month}/${year}`;
13
+ };
14
+
15
+ export const parseDate = (value: string): Date | null => {
16
+ if (!value.trim()) return null;
17
+
18
+ const [day, month, year] = value.split('/').map(Number);
19
+ const parsed = new Date(year, month - 1, day);
20
+
21
+ const isValid =
22
+ parsed.getFullYear() === year &&
23
+ parsed.getMonth() === month - 1 &&
24
+ parsed.getDate() === day;
25
+
26
+ return isValid ? parsed : null;
27
+ };
@@ -0,0 +1,40 @@
1
+ import z from 'zod';
2
+
3
+ import { formSchema } from './schema';
4
+
5
+ describe('formSchema:', () => {
6
+ it('successfully validates correct data', () => {
7
+ const validData = { title: 'Run 100 km', date: '2026-03-11' };
8
+ const result = formSchema.safeParse(validData);
9
+
10
+ expect(result.success).toBe(true);
11
+ if (result.success) {
12
+ expect(result.data).toEqual(validData);
13
+ }
14
+ });
15
+
16
+ it('returns an error if title is shorter than 3 characters', () => {
17
+ const result = formSchema.safeParse({ title: 'Ru', date: '2026-03-11' });
18
+
19
+ expect(result.success).toBe(false);
20
+ if (!result.success) {
21
+ const errors = z.treeifyError(result.error).properties;
22
+ expect(errors?.title?.errors).toContain(
23
+ 'Title must be at least 3 characters'
24
+ );
25
+ }
26
+ });
27
+
28
+ it('returns an error if date format is incorrect', () => {
29
+ const result = formSchema.safeParse({
30
+ title: 'Run 100 km',
31
+ date: '2026.01.13',
32
+ });
33
+
34
+ expect(result.success).toBe(false);
35
+ if (!result.success) {
36
+ const errors = z.treeifyError(result.error).properties;
37
+ expect(errors?.date?.errors).toContain('Incorrect date format');
38
+ }
39
+ });
40
+ });
@@ -0,0 +1,8 @@
1
+ import * as z from 'zod';
2
+
3
+ export const formSchema = z.object({
4
+ title: z.string().min(3, 'Title must be at least 3 characters'),
5
+ date: z.string().regex(/^\d{4}-\d{2}-\d{2}$/, 'Incorrect date format'),
6
+ });
7
+
8
+ export type FormData = z.infer<typeof formSchema>;
@@ -0,0 +1,11 @@
1
+ import { getOrderedDays } from './weekdayOrder';
2
+
3
+ describe('getOrderedDays:', () => {
4
+ test('returns indexes for Sunday', () => {
5
+ expect(getOrderedDays(0)).toEqual([0, 1, 2, 3, 4, 5, 6]);
6
+ });
7
+
8
+ test('returns indexes for Monday', () => {
9
+ expect(getOrderedDays(1)).toEqual([1, 2, 3, 4, 5, 6, 0]);
10
+ });
11
+ });