@uxf/datepicker 16.10.0 → 16.13.1
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.
- package/README.md +56 -43
- package/hooks/use-date-picker-navigation.test.js +1 -0
- package/hooks/use-date-picker.d.ts +4 -1
- package/hooks/use-date-picker.js +84 -78
- package/hooks/use-date-picker.test.js +34 -0
- package/hooks/use-date-range-picker.d.ts +4 -1
- package/hooks/use-date-range-picker.js +80 -74
- package/hooks/use-date-range-picker.test.js +40 -0
- package/hooks/use-day.d.ts +3 -1
- package/hooks/use-day.js +73 -69
- package/hooks/use-day.test.js +28 -0
- package/hooks/use-decade.test.js +1 -0
- package/hooks/use-hour.test.js +1 -0
- package/hooks/use-hours.test.js +1 -0
- package/hooks/use-minute.test.js +1 -0
- package/hooks/use-minutes.test.js +1 -0
- package/hooks/use-month.test.js +1 -0
- package/hooks/use-time-picker.test.js +1 -0
- package/hooks/use-year.test.js +1 -0
- package/jest.dst.config.js +2 -2
- package/package.json +5 -5
- package/utils/date-string.d.ts +5 -4
- package/utils/date-string.js +37 -6
- package/utils/date-string.test.js +28 -0
- package/utils/dst-robustness.test.js +10 -0
package/hooks/use-hour.test.js
CHANGED
package/hooks/use-hours.test.js
CHANGED
package/hooks/use-minute.test.js
CHANGED
package/hooks/use-month.test.js
CHANGED
package/hooks/use-year.test.js
CHANGED
package/jest.dst.config.js
CHANGED
|
@@ -11,8 +11,8 @@ const swcOptions = require("../../jest.swc-options.js");
|
|
|
11
11
|
|
|
12
12
|
/** @type {import('jest').Config} */
|
|
13
13
|
module.exports = {
|
|
14
|
-
//
|
|
15
|
-
//
|
|
14
|
+
// Set here although root jest.config.js uses "node" too: this config is standalone, not an extension
|
|
15
|
+
// of it. DST tests must run in node environment: jsdom swallows timezone behaviour.
|
|
16
16
|
testEnvironment: "node",
|
|
17
17
|
globalSetup: "<rootDir>/jest.dst.global-setup.js",
|
|
18
18
|
rootDir: ".",
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@uxf/datepicker",
|
|
3
|
-
"version": "16.
|
|
3
|
+
"version": "16.13.1",
|
|
4
4
|
"description": "A set of React hooks to create an awesome datepicker.",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -19,8 +19,8 @@
|
|
|
19
19
|
"homepage": "https://fe.uxf.dev/docs/datepicker",
|
|
20
20
|
"license": "MIT",
|
|
21
21
|
"peerDependencies": {
|
|
22
|
-
"@uxf/core": "16.
|
|
23
|
-
"@uxf/core-react": "16.
|
|
22
|
+
"@uxf/core": "16.13.1",
|
|
23
|
+
"@uxf/core-react": "16.13.1",
|
|
24
24
|
"dayjs": "^1.11.23",
|
|
25
25
|
"next": ">=16.4.3",
|
|
26
26
|
"react": "^19.3.0",
|
|
@@ -34,8 +34,8 @@
|
|
|
34
34
|
"devDependencies": {
|
|
35
35
|
"@types/react": "19.3.0",
|
|
36
36
|
"@types/react-dom": "19.3.0",
|
|
37
|
-
"@uxf/core": "16.
|
|
38
|
-
"@uxf/core-react": "16.
|
|
37
|
+
"@uxf/core": "16.13.1",
|
|
38
|
+
"@uxf/core-react": "16.13.1",
|
|
39
39
|
"dayjs": "^1.11.23",
|
|
40
40
|
"react": "19.3.1",
|
|
41
41
|
"react-dom": "19.3.1"
|
package/utils/date-string.d.ts
CHANGED
|
@@ -16,11 +16,12 @@ export declare function toDateString(date: Date): DateString;
|
|
|
16
16
|
*/
|
|
17
17
|
export declare function fromDateString(s: DateString): Date;
|
|
18
18
|
/**
|
|
19
|
-
*
|
|
20
|
-
*
|
|
21
|
-
*
|
|
19
|
+
* The calendar day of `now` in `timeZone` (an IANA name such as "Europe/Prague"), or in the device's
|
|
20
|
+
* time zone when none is given or the name is unknown. Never the UTC day: between midnight and the UTC
|
|
21
|
+
* offset that would be yesterday east of UTC and tomorrow west of it. Not `dayjsEnWithTz`, which
|
|
22
|
+
* defaults to UTC and throws on an unknown zone.
|
|
22
23
|
*/
|
|
23
|
-
export declare function todayDateString(): DateString;
|
|
24
|
+
export declare function todayDateString(now?: number, timeZone?: string): DateString;
|
|
24
25
|
/**
|
|
25
26
|
* Add n calendar months to a DateString, snapping to the 1st of the resulting month.
|
|
26
27
|
* Month arithmetic is purely numeric — no DST exposure.
|
package/utils/date-string.js
CHANGED
|
@@ -35,14 +35,45 @@ function toDateString(date) {
|
|
|
35
35
|
function fromDateString(s) {
|
|
36
36
|
return new Date(s);
|
|
37
37
|
}
|
|
38
|
+
function deviceDateString(now) {
|
|
39
|
+
const date = new Date(now);
|
|
40
|
+
return (0, create_date_string_1.createDateString)(date.getFullYear(), date.getMonth(), date.getDate());
|
|
41
|
+
}
|
|
42
|
+
// every day cell asks for today, so each zone gets one formatter; null marks an unknown zone
|
|
43
|
+
const formatters = new Map();
|
|
44
|
+
// a server may meet zones from request data, so the cache must not grow without bound
|
|
45
|
+
const MAX_FORMATTERS = 50;
|
|
46
|
+
function getFormatter(timeZone) {
|
|
47
|
+
var _a;
|
|
48
|
+
if (!formatters.has(timeZone)) {
|
|
49
|
+
if (formatters.size >= MAX_FORMATTERS) {
|
|
50
|
+
formatters.clear();
|
|
51
|
+
}
|
|
52
|
+
try {
|
|
53
|
+
formatters.set(timeZone, new Intl.DateTimeFormat("en-US", { timeZone, year: "numeric", month: "numeric", day: "numeric" }));
|
|
54
|
+
}
|
|
55
|
+
catch {
|
|
56
|
+
// an unknown or empty name (a typo, "" from a CMS field) throws a RangeError, which would break the render
|
|
57
|
+
// eslint-disable-next-line no-console
|
|
58
|
+
console.warn(`@uxf/datepicker: unknown time zone ${JSON.stringify(timeZone)}, today is counted in the device's.`);
|
|
59
|
+
formatters.set(timeZone, null);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
return (_a = formatters.get(timeZone)) !== null && _a !== void 0 ? _a : null;
|
|
63
|
+
}
|
|
38
64
|
/**
|
|
39
|
-
*
|
|
40
|
-
*
|
|
41
|
-
*
|
|
65
|
+
* The calendar day of `now` in `timeZone` (an IANA name such as "Europe/Prague"), or in the device's
|
|
66
|
+
* time zone when none is given or the name is unknown. Never the UTC day: between midnight and the UTC
|
|
67
|
+
* offset that would be yesterday east of UTC and tomorrow west of it. Not `dayjsEnWithTz`, which
|
|
68
|
+
* defaults to UTC and throws on an unknown zone.
|
|
42
69
|
*/
|
|
43
|
-
function todayDateString() {
|
|
44
|
-
const
|
|
45
|
-
|
|
70
|
+
function todayDateString(now = Date.now(), timeZone) {
|
|
71
|
+
const formatter = timeZone === undefined ? null : getFormatter(timeZone);
|
|
72
|
+
if (formatter === null) {
|
|
73
|
+
return deviceDateString(now);
|
|
74
|
+
}
|
|
75
|
+
const parts = Object.fromEntries(formatter.formatToParts(now).map((part) => [part.type, Number(part.value)]));
|
|
76
|
+
return (0, create_date_string_1.createDateString)(parts.year, parts.month - 1, parts.day);
|
|
46
77
|
}
|
|
47
78
|
/**
|
|
48
79
|
* Add n calendar months to a DateString, snapping to the 1st of the resulting month.
|
|
@@ -61,3 +61,31 @@ test("addMonths goes back with negative months", () => {
|
|
|
61
61
|
test("addMonths with n=0 returns first of same month", () => {
|
|
62
62
|
expect((0, date_string_1.addMonths)("2026-04-15", 0)).toBe("2026-04-01");
|
|
63
63
|
});
|
|
64
|
+
describe("todayDateString", () => {
|
|
65
|
+
// 0:30 in Prague is still the previous day in UTC
|
|
66
|
+
const PRAGUE_HALF_PAST_MIDNIGHT = Date.parse("2026-11-20T23:30:00Z");
|
|
67
|
+
test.each([
|
|
68
|
+
["Europe/Prague", "2026-11-21"],
|
|
69
|
+
["America/Los_Angeles", "2026-11-20"],
|
|
70
|
+
["Asia/Tokyo", "2026-11-21"],
|
|
71
|
+
["UTC", "2026-11-20"],
|
|
72
|
+
])("is the calendar day in %s", (timeZone, expected) => {
|
|
73
|
+
expect((0, date_string_1.todayDateString)(PRAGUE_HALF_PAST_MIDNIGHT, timeZone)).toBe(expected);
|
|
74
|
+
});
|
|
75
|
+
test("counts summer time", () => {
|
|
76
|
+
expect((0, date_string_1.todayDateString)(Date.parse("2026-07-20T22:30:00Z"), "Europe/Prague")).toBe("2026-07-21");
|
|
77
|
+
});
|
|
78
|
+
test.each(["", "Europe/Praha"])("falls back to the device's day for the unknown time zone %j", (timeZone) => {
|
|
79
|
+
const warn = jest.spyOn(console, "warn").mockImplementation(() => undefined);
|
|
80
|
+
expect((0, date_string_1.todayDateString)(PRAGUE_HALF_PAST_MIDNIGHT, timeZone)).toBe((0, date_string_1.todayDateString)(PRAGUE_HALF_PAST_MIDNIGHT));
|
|
81
|
+
warn.mockRestore();
|
|
82
|
+
});
|
|
83
|
+
test("warns about an unknown time zone once, not on every call", () => {
|
|
84
|
+
const warn = jest.spyOn(console, "warn").mockImplementation(() => undefined);
|
|
85
|
+
(0, date_string_1.todayDateString)(PRAGUE_HALF_PAST_MIDNIGHT, "Unknown/Warned_Once");
|
|
86
|
+
(0, date_string_1.todayDateString)(PRAGUE_HALF_PAST_MIDNIGHT, "Unknown/Warned_Once");
|
|
87
|
+
expect(warn).toHaveBeenCalledTimes(1);
|
|
88
|
+
expect(warn).toHaveBeenCalledWith(expect.stringContaining('"Unknown/Warned_Once"'));
|
|
89
|
+
warn.mockRestore();
|
|
90
|
+
});
|
|
91
|
+
});
|
|
@@ -291,6 +291,16 @@ describe("getCurrentYearMonthAndDate: returns local calendar month in UTC+ timez
|
|
|
291
291
|
expect(result.month).toBe(expectedMonth);
|
|
292
292
|
});
|
|
293
293
|
});
|
|
294
|
+
describe("todayDateString: the device's calendar day, not the UTC one", () => {
|
|
295
|
+
test("matches the local calendar at a time when it differs from UTC", () => {
|
|
296
|
+
// 22:30 UTC is the next day east of UTC, 3:30 UTC the previous day west of it
|
|
297
|
+
for (const iso of ["2026-11-20T22:30:00Z", "2026-11-21T03:30:00Z"]) {
|
|
298
|
+
const now = new Date(iso);
|
|
299
|
+
const local = `${now.getFullYear()}-${String(now.getMonth() + 1).padStart(2, "0")}-${String(now.getDate()).padStart(2, "0")}`;
|
|
300
|
+
expect((0, date_string_1.todayDateString)(now.getTime())).toBe(local);
|
|
301
|
+
}
|
|
302
|
+
});
|
|
303
|
+
});
|
|
294
304
|
describe("Range boundary checks", () => {
|
|
295
305
|
test("canSelectRange: startDate on minBookingDate boundary is valid", () => {
|
|
296
306
|
expect((0, can_select_range_1.canSelectRange)({
|