fix(ui): own the time picker so it follows the 24-hour preference

The native datetime-local / time picker renders its clock and AM/PM
toggle by the browser UI language, not the page lang or the app's
time-format preference — so an English-language browser showed AM/PM
even with 24-hour selected. Replace the native time control with an
app-controlled TimeField (text entry + a dependency-free popover of
hour/minute, plus AM/PM in 12-hour mode) whose hour cycle always tracks
the preference. DateTimeField now composes the native calendar (date)
with TimeField (time); the committed value stays a local
yyyy-MM-ddTHH:mm string, and min/max clamp the recombined value.

Also thread the time-format preference into the doctor-report PDF so its
generated-at timestamp honours it instead of the locale default.
This commit is contained in:
Marc-André Bombeck committed 2026-06-29 19:03:47 +02:00
1 parent 426b21497a
commit 7e901e674a
19 files changed
+826 -243

No files matched your search

@@ -1,19 +1,22 @@
/**
* `<DateTimeField>` contract (v1.21.0).
* `<DateTimeField>` contract (v1.25.4).
*
* Like `<DateField>`, the SSR-only convention (`renderToStaticMarkup`,
* The field now composes a native-calendar `<DateField>` (date part) with the
* app-controlled `<TimeField>` (time part) so the time picker always follows
* the user's hour-cycle preference rather than the browser UI language. Like
* `<DateField>`, the SSR-only convention (`renderToStaticMarkup`,
* `environment: "node"`) means the interactive picker path can't be driven
* here. What the static markup pins is the load-bearing contract:
*
* - the committed VALUE stays a local `yyyy-MM-ddTHH:mm` string on a hidden
* native datetime-local input, so this is a drop-in for `<DateTimeInput>`;
* - the visible overlay paints the date in the date-order preference and the
* time in the hour-cycle preference;
* - disabled / min / max / placeholder thread through;
* - height + target-size parity classes are present.
* input carrying `name`, so this is a drop-in for the old field;
* - the date overlay paints the date-order preference; the time overlay paints
* the hour-cycle preference;
* - disabled threads through; min / max gate the native calendar by date part;
* - height + target-size parity classes are present on both halves.
*
* Under SSR both preferences resolve AUTO (no `window`), so the overlay follows
* the locale: de → "31.12.2026 14:05" (24h), en → "12/31/2026 02:05 PM".
* Under SSR both preferences resolve AUTO (no `window`), so each half follows
* the locale: de → "31.12.2026" + "14:05" (24h), en → "12/31/2026" + 12h.
*/
import { describe, expect, it } from "vitest";
import { renderToStaticMarkup } from "react-dom/server";
@@ -28,41 +31,35 @@ function render(node: React.ReactNode, locale: "de" | "en" = "en"): string {
}
describe("<DateTimeField>", () => {
it("keeps the local datetime value on a hidden native datetime-local input", () => {
it("keeps the combined local datetime value on a hidden input carrying name", () => {
const html = render(
<DateTimeField id="ts" name="ts" value="2026-12-31T14:05" />,
);
expect(html).toMatch(
/<input[^>]*type="datetime-local"[^>]*value="2026-12-31T14:05"/,
/<input[^>]*type="hidden"[^>]*value="2026-12-31T14:05"/,
);
expect(html).toContain('name="ts"');
expect(html).toMatch(
/<input[^>]*type="datetime-local"[^>]*class="[^"]*sr-only/,
);
});
it("paints the AUTO-locale display (de → dd.MM.yyyy + 24h)", () => {
it("paints the AUTO-locale display (de → dd.MM.yyyy + 24h time)", () => {
const html = render(<DateTimeField value="2026-12-31T14:05" />, "de");
expect(html).toContain('value="31.12.2026 14:05"');
expect(html).toContain('value="31.12.2026"');
expect(html).toContain('value="14:05"');
});
it("paints the AUTO-locale display (en → MM/dd/yyyy + 12h)", () => {
it("paints the AUTO-locale display (en → MM/dd/yyyy + 12h time)", () => {
const html = render(<DateTimeField value="2026-12-31T14:05" />, "en");
expect(html).toContain('value="12/31/2026"');
// en-US default hour cycle is AM/PM.
expect(html).toMatch(/value="12\/31\/2026 02:05\s?PM"/);
expect(html).toMatch(/value="02:05\s?PM"/);
});
it("renders the supplied placeholder when empty", () => {
it("threads the supplied placeholder onto the date half", () => {
const html = render(<DateTimeField value="" placeholder="Pick a moment" />);
expect(html).toContain('placeholder="Pick a moment"');
});
it("derives a format-shaped placeholder when none is supplied (en)", () => {
const html = render(<DateTimeField value="" />, "en");
expect(html).toContain('placeholder="MM/DD/YYYY --:--"');
});
it("threads disabled, min and max onto the native input", () => {
it("gates the native calendar by the min/max date part and threads disabled", () => {
const html = render(
<DateTimeField
value="2026-12-31T14:05"
@@ -71,23 +68,20 @@ describe("<DateTimeField>", () => {
max="2030-12-31T23:59"
/>,
);
expect(html).toMatch(
/<input[^>]*type="datetime-local"[^>]*min="2020-01-01T00:00"/,
);
expect(html).toMatch(
/<input[^>]*type="datetime-local"[^>]*max="2030-12-31T23:59"/,
);
expect(html).toMatch(/<input[^>]*type="datetime-local"[^>]*disabled/);
expect(html).toMatch(/<input[^>]*type="date"[^>]*min="2020-01-01"/);
expect(html).toMatch(/<input[^>]*type="date"[^>]*max="2030-12-31"/);
expect(html).toMatch(/<input[^>]*type="date"[^>]*disabled/);
});
it("ships the WCAG target-size + height-parity classes", () => {
it("ships the WCAG target-size + height-parity classes on both halves", () => {
const html = render(<DateTimeField value="2026-12-31T14:05" />);
expect(html).toContain("min-h-11");
expect(html).toContain("sm:h-10");
});
it("exposes a labelled calendar picker affordance", () => {
it("exposes both the calendar and the time picker affordances", () => {
const html = render(<DateTimeField value="2026-12-31T14:05" />);
expect(html).toContain('aria-label="Open date picker"');
expect(html).toContain('aria-label="Open time picker"');
});
});
@@ -0,0 +1,123 @@
/**
* `<TimeField>` contract (v1.25.4).
*
* The app-controlled time input replaces the native `<input type="time">` so
* the hour cycle follows the user's preference, never the browser UI language.
* The committed VALUE is always a 24-hour `"HH:mm"` string (drop-in for the
* native input). These tests pin the pure conversion/parse helpers plus the
* SSR-rendered display/affordance contract.
*/
import { describe, expect, it } from "vitest";
import { renderToStaticMarkup } from "react-dom/server";
import { I18nProvider } from "@/lib/i18n/context";
import {
TimeField,
parseHhmm,
parseTypedTime,
formatHhmm,
to12hHour,
from12hHour,
prefersTwelveHour,
} from "../time-field";
function render(node: React.ReactNode, locale: "de" | "en" = "en"): string {
return renderToStaticMarkup(
<I18nProvider initialLocale={locale}>{node}</I18nProvider>,
);
}
describe("parseHhmm", () => {
it("splits a well-formed 24h string", () => {
expect(parseHhmm("14:05")).toEqual({ hour: 14, minute: 5 });
expect(parseHhmm("00:00")).toEqual({ hour: 0, minute: 0 });
expect(parseHhmm("23:59")).toEqual({ hour: 23, minute: 59 });
});
it("rejects malformed / out-of-range input", () => {
expect(parseHhmm("")).toBeNull();
expect(parseHhmm("24:00")).toBeNull();
expect(parseHhmm("12:60")).toBeNull();
expect(parseHhmm("9:5")).toBeNull();
});
});
describe("parseTypedTime", () => {
it("accepts colon, dot and bare-digit forms", () => {
expect(parseTypedTime("14:30")).toBe("14:30");
expect(parseTypedTime("14.30")).toBe("14:30");
expect(parseTypedTime("1430")).toBe("14:30");
expect(parseTypedTime("930")).toBe("09:30");
expect(parseTypedTime("9")).toBe("09:00");
});
it("honours an AM/PM affix", () => {
expect(parseTypedTime("2:30 pm")).toBe("14:30");
expect(parseTypedTime("2:30pm")).toBe("14:30");
expect(parseTypedTime("12:00 am")).toBe("00:00");
expect(parseTypedTime("12:00 pm")).toBe("12:00");
expect(parseTypedTime("11:15 pm")).toBe("23:15");
});
it("rejects impossible times", () => {
expect(parseTypedTime("")).toBeNull();
expect(parseTypedTime("25:00")).toBeNull();
expect(parseTypedTime("14:70")).toBeNull();
expect(parseTypedTime("13:00 pm")).toBeNull(); // 13 can't be a 12h hour
});
});
describe("12h <-> 24h conversion", () => {
it("maps 24h to its 12h clock cell", () => {
expect(to12hHour(0)).toBe(12);
expect(to12hHour(12)).toBe(12);
expect(to12hHour(13)).toBe(1);
expect(to12hHour(23)).toBe(11);
});
it("maps a 12h cell + period back to 24h", () => {
expect(from12hHour(12, false)).toBe(0); // 12 AM
expect(from12hHour(12, true)).toBe(12); // 12 PM
expect(from12hHour(1, true)).toBe(13); // 1 PM
expect(from12hHour(11, true)).toBe(23); // 11 PM
});
});
describe("prefersTwelveHour", () => {
it("follows the explicit preference", () => {
expect(prefersTwelveHour("H12", "de")).toBe(true);
expect(prefersTwelveHour("H24", "en")).toBe(false);
});
it("falls to the locale under AUTO (en → 12h, others → 24h)", () => {
expect(prefersTwelveHour("AUTO", "en")).toBe(true);
expect(prefersTwelveHour("AUTO", "de")).toBe(false);
});
});
describe("formatHhmm", () => {
it("renders 24h under H24 regardless of locale", () => {
expect(formatHhmm("14:05", "H24", "en")).toMatch(/^14:05$/);
expect(formatHhmm("14:05", "H24", "de")).toMatch(/^14:05$/);
});
it("renders AM/PM under H12", () => {
expect(formatHhmm("14:05", "H12", "de")).toMatch(/02:05\s?PM/);
});
it("returns empty string for malformed input", () => {
expect(formatHhmm("", "H24", "de")).toBe("");
expect(formatHhmm("nope", "H24", "de")).toBe("");
});
});
describe("<TimeField> SSR", () => {
it("paints the value in 24h under the de AUTO locale and exposes the picker affordance", () => {
const html = render(<TimeField value="14:05" />, "de");
expect(html).toContain('value="14:05"');
expect(html).toContain('data-slot="time-field-trigger"');
});
it("keeps the 24h value on a hidden mirror carrying name", () => {
const html = render(<TimeField id="t" name="t" value="08:30" />, "de");
expect(html).toMatch(/<input[^>]*type="hidden"[^>]*value="08:30"/);
expect(html).toContain('name="t"');
});
it("ships the height-parity classes", () => {
const html = render(<TimeField value="08:30" />, "de");
expect(html).toContain("min-h-11");
expect(html).toContain("sm:h-10");
});
});