@uxf/datepicker 16.3.0 → 16.11.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.
@@ -1,5 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ /** @jest-environment jsdom */
3
4
  const react_1 = require("@testing-library/react");
4
5
  const react_2 = require("react");
5
6
  const use_hour_1 = require("./use-hour");
@@ -1,5 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ /** @jest-environment jsdom */
3
4
  const react_1 = require("@testing-library/react");
4
5
  const use_hours_1 = require("./use-hours");
5
6
  test("useHours", () => {
@@ -1,5 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ /** @jest-environment jsdom */
3
4
  const react_1 = require("@testing-library/react");
4
5
  const react_2 = require("react");
5
6
  const use_minute_1 = require("./use-minute");
@@ -1,5 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ /** @jest-environment jsdom */
3
4
  const react_1 = require("@testing-library/react");
4
5
  const use_minutes_1 = require("./use-minutes");
5
6
  test("useMinutes", () => {
@@ -1,5 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ /** @jest-environment jsdom */
3
4
  const react_1 = require("@testing-library/react");
4
5
  const dayjs_utils_1 = require("../utils/dayjs-utils");
5
6
  const use_month_1 = require("./use-month");
@@ -1,5 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ /** @jest-environment jsdom */
3
4
  const react_1 = require("@testing-library/react");
4
5
  const react_2 = require("react");
5
6
  const use_time_picker_1 = require("./use-time-picker");
@@ -1,5 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ /** @jest-environment jsdom */
3
4
  const react_1 = require("@testing-library/react");
4
5
  const dayjs_utils_1 = require("../utils/dayjs-utils");
5
6
  const use_year_1 = require("./use-year");
@@ -11,8 +11,8 @@ const swcOptions = require("../../jest.swc-options.js");
11
11
 
12
12
  /** @type {import('jest').Config} */
13
13
  module.exports = {
14
- // "node" instead of the jsdom that root jest.config.js sets — this config is standalone, not an
15
- // extension of it. DST tests must run in node environment: jsdom swallows timezone behaviour.
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.0",
3
+ "version": "16.11.0",
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.3.0",
23
- "@uxf/core-react": "16.3.0",
22
+ "@uxf/core": "16.11.0",
23
+ "@uxf/core-react": "16.11.0",
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.3.0",
38
- "@uxf/core-react": "16.3.0",
37
+ "@uxf/core": "16.11.0",
38
+ "@uxf/core-react": "16.11.0",
39
39
  "dayjs": "^1.11.23",
40
40
  "react": "19.3.1",
41
41
  "react-dom": "19.3.1"
@@ -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
- * Today as a DateString in UTC — matches the `new Date("Y-M-D")` convention
20
- * used throughout the package. Use this instead of `toDateString(new Date())`
21
- * whenever you need a stable "today" string.
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.
@@ -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
- * Today as a DateString in UTC — matches the `new Date("Y-M-D")` convention
40
- * used throughout the package. Use this instead of `toDateString(new Date())`
41
- * whenever you need a stable "today" string.
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 now = new Date();
45
- return (0, create_date_string_1.createDateString)(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate());
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)({