@pushwoosh/dumb-components 1.1.216 → 1.1.217
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.
|
@@ -24,7 +24,10 @@ export const CalendarMonthSlider = ({
|
|
|
24
24
|
renderAfter,
|
|
25
25
|
...monthProps
|
|
26
26
|
}) => {
|
|
27
|
-
const [anchor, setAnchor] = useState(
|
|
27
|
+
const [anchor, setAnchor] = useState(() => ({
|
|
28
|
+
...startMonth,
|
|
29
|
+
day: 1
|
|
30
|
+
}));
|
|
28
31
|
const [inTransition, setInTransition] = useState(false);
|
|
29
32
|
const [index, setIndex] = useState(0);
|
|
30
33
|
const [panels, setPanels] = useState(() => makeBaseline(anchor, visibleCount));
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import { addPeriod, setPeriodStruct } from '../helpers';
|
|
2
|
+
describe('addPeriod', () => {
|
|
3
|
+
it('clamps the day when the target month is shorter', () => {
|
|
4
|
+
expect(addPeriod({
|
|
5
|
+
year: 2026,
|
|
6
|
+
month: 8,
|
|
7
|
+
day: 31
|
|
8
|
+
}, 'month', 1)).toEqual({
|
|
9
|
+
year: 2026,
|
|
10
|
+
month: 9,
|
|
11
|
+
day: 30,
|
|
12
|
+
hour: 0,
|
|
13
|
+
minute: 0,
|
|
14
|
+
second: 0,
|
|
15
|
+
millisecond: 0
|
|
16
|
+
});
|
|
17
|
+
expect(addPeriod({
|
|
18
|
+
year: 2026,
|
|
19
|
+
month: 3,
|
|
20
|
+
day: 31
|
|
21
|
+
}, 'month', -1)).toEqual({
|
|
22
|
+
year: 2026,
|
|
23
|
+
month: 2,
|
|
24
|
+
day: 28,
|
|
25
|
+
hour: 0,
|
|
26
|
+
minute: 0,
|
|
27
|
+
second: 0,
|
|
28
|
+
millisecond: 0
|
|
29
|
+
});
|
|
30
|
+
expect(addPeriod({
|
|
31
|
+
year: 2024,
|
|
32
|
+
month: 2,
|
|
33
|
+
day: 29
|
|
34
|
+
}, 'year', 1)).toEqual({
|
|
35
|
+
year: 2025,
|
|
36
|
+
month: 2,
|
|
37
|
+
day: 28,
|
|
38
|
+
hour: 0,
|
|
39
|
+
minute: 0,
|
|
40
|
+
second: 0,
|
|
41
|
+
millisecond: 0
|
|
42
|
+
});
|
|
43
|
+
});
|
|
44
|
+
it('crosses year boundaries in both directions', () => {
|
|
45
|
+
expect(addPeriod({
|
|
46
|
+
year: 2026,
|
|
47
|
+
month: 12,
|
|
48
|
+
day: 15
|
|
49
|
+
}, 'month', 1)).toMatchObject({
|
|
50
|
+
year: 2027,
|
|
51
|
+
month: 1,
|
|
52
|
+
day: 15
|
|
53
|
+
});
|
|
54
|
+
expect(addPeriod({
|
|
55
|
+
year: 2026,
|
|
56
|
+
month: 1,
|
|
57
|
+
day: 15
|
|
58
|
+
}, 'month', -1)).toMatchObject({
|
|
59
|
+
year: 2025,
|
|
60
|
+
month: 12,
|
|
61
|
+
day: 15
|
|
62
|
+
});
|
|
63
|
+
expect(addPeriod({
|
|
64
|
+
year: 2026,
|
|
65
|
+
month: 5,
|
|
66
|
+
day: 10
|
|
67
|
+
}, 'month', -17)).toMatchObject({
|
|
68
|
+
year: 2024,
|
|
69
|
+
month: 12,
|
|
70
|
+
day: 10
|
|
71
|
+
});
|
|
72
|
+
});
|
|
73
|
+
it('keeps the time of day on month steps', () => {
|
|
74
|
+
expect(addPeriod({
|
|
75
|
+
year: 2026,
|
|
76
|
+
month: 1,
|
|
77
|
+
day: 31,
|
|
78
|
+
hour: 13,
|
|
79
|
+
minute: 45,
|
|
80
|
+
second: 7,
|
|
81
|
+
millisecond: 0
|
|
82
|
+
}, 'month', 1)).toMatchObject({
|
|
83
|
+
year: 2026,
|
|
84
|
+
month: 2,
|
|
85
|
+
day: 28,
|
|
86
|
+
hour: 13,
|
|
87
|
+
minute: 45,
|
|
88
|
+
second: 7
|
|
89
|
+
});
|
|
90
|
+
});
|
|
91
|
+
it('still rolls over on day steps', () => {
|
|
92
|
+
expect(addPeriod({
|
|
93
|
+
year: 2026,
|
|
94
|
+
month: 8,
|
|
95
|
+
day: 31
|
|
96
|
+
}, 'day', 1)).toMatchObject({
|
|
97
|
+
year: 2026,
|
|
98
|
+
month: 9,
|
|
99
|
+
day: 1
|
|
100
|
+
});
|
|
101
|
+
});
|
|
102
|
+
});
|
|
103
|
+
describe('setPeriodStruct', () => {
|
|
104
|
+
it('clamps the day when setting a shorter month', () => {
|
|
105
|
+
expect(setPeriodStruct({
|
|
106
|
+
year: 2026,
|
|
107
|
+
month: 1,
|
|
108
|
+
day: 31
|
|
109
|
+
}, 'month', 2)).toMatchObject({
|
|
110
|
+
year: 2026,
|
|
111
|
+
month: 2,
|
|
112
|
+
day: 28
|
|
113
|
+
});
|
|
114
|
+
expect(setPeriodStruct({
|
|
115
|
+
year: 2024,
|
|
116
|
+
month: 2,
|
|
117
|
+
day: 29
|
|
118
|
+
}, 'year', 2025)).toMatchObject({
|
|
119
|
+
year: 2025,
|
|
120
|
+
month: 2,
|
|
121
|
+
day: 28
|
|
122
|
+
});
|
|
123
|
+
});
|
|
124
|
+
});
|
package/Calendar/helpers.d.ts
CHANGED
|
@@ -6,7 +6,7 @@ export declare const createUTCDate: (year: number, month?: number, day?: number,
|
|
|
6
6
|
export declare const parseStringToDate: (date: string) => Date;
|
|
7
7
|
export declare const formatTwoDigitNumber: (value: number) => string;
|
|
8
8
|
export declare const formatStruct: (ds: DateStruct, template: string) => string;
|
|
9
|
-
/** Returns a new struct with `value` units of `period` added (negative to subtract)
|
|
9
|
+
/** Returns a new struct with `value` units of `period` added (negative to subtract). */
|
|
10
10
|
export declare const addPeriod: <T extends DateTimeStruct | DateStruct>(ds: T, period: Period, value: number) => T;
|
|
11
11
|
/** Narrows a {@link DateTimeStruct} to a date-only {@link DateStruct} (drops the time fields). */
|
|
12
12
|
export declare const pickDateStruct: (date: DateTimeStruct) => DateStruct;
|
package/Calendar/helpers.js
CHANGED
|
@@ -32,9 +32,23 @@ export const formatStruct = (ds, template) => {
|
|
|
32
32
|
return value !== undefined ? formatTwoDigitNumber(value) : match;
|
|
33
33
|
});
|
|
34
34
|
};
|
|
35
|
-
|
|
35
|
+
const clampDayToMonth = (year, month, day) => Math.min(day, new Date(Date.UTC(year, month, 0)).getUTCDate());
|
|
36
|
+
/** Moves a struct to the absolute month `monthIndex` (`year * 12 + month - 1`), clamping the day to that month. */
|
|
37
|
+
const moveToMonth = (ds, monthIndex) => {
|
|
38
|
+
const year = Math.floor(monthIndex / 12);
|
|
39
|
+
const month = monthIndex - year * 12 + 1;
|
|
40
|
+
const dts = ds;
|
|
41
|
+
const date = createUTCDate(year, month, clampDayToMonth(year, month, ds.day), dts.hour, dts.minute, dts.second, dts.millisecond);
|
|
42
|
+
return utcDateToStruct(date);
|
|
43
|
+
};
|
|
44
|
+
/** Returns a new struct with `value` units of `period` added (negative to subtract). */
|
|
36
45
|
export const addPeriod = (ds, period, value) => {
|
|
37
|
-
|
|
46
|
+
// Month/year steps clamp the day instead of letting Date overflow: Aug 31 + 1 month is Sep 30, not Oct 1.
|
|
47
|
+
if (period === 'month' || period === 'year') {
|
|
48
|
+
return moveToMonth(ds, ds.year * 12 + ds.month - 1 + (period === 'month' ? value : value * 12));
|
|
49
|
+
}
|
|
50
|
+
const dts = ds;
|
|
51
|
+
const date = createUTCDate(ds.year, ds.month, ds.day, dts.hour, dts.minute, dts.second, dts.millisecond);
|
|
38
52
|
const method = periodMethodMap[period];
|
|
39
53
|
date[`setUTC${method}`](date[`getUTC${method}`]() + value);
|
|
40
54
|
return utcDateToStruct(date);
|
|
@@ -48,9 +62,16 @@ export const pickDateStruct = date => {
|
|
|
48
62
|
};
|
|
49
63
|
};
|
|
50
64
|
export const setPeriodStruct = (ds, period, value) => {
|
|
51
|
-
|
|
65
|
+
if (period === 'month') {
|
|
66
|
+
return moveToMonth(ds, ds.year * 12 + value - 1);
|
|
67
|
+
}
|
|
68
|
+
if (period === 'year') {
|
|
69
|
+
return moveToMonth(ds, value * 12 + ds.month - 1);
|
|
70
|
+
}
|
|
71
|
+
const dts = ds;
|
|
72
|
+
const date = createUTCDate(ds.year, ds.month, ds.day, dts.hour, dts.minute, dts.second, dts.millisecond);
|
|
52
73
|
const method = periodMethodMap[period];
|
|
53
|
-
date[`setUTC${method}`](
|
|
74
|
+
date[`setUTC${method}`](value);
|
|
54
75
|
return utcDateToStruct(date);
|
|
55
76
|
};
|
|
56
77
|
export const extendPeriodStruct = (struct, extend) => {
|