fix(demos): inherit ConfigProvider locale and restore the format-options popover width

HD-03 scope, all against heroui ce90ea3 cn sources:

- calendar-disabled/read-only: restore the today() default value and the
  baseline description lines (日历已禁用 / 日历为只读) inside the
  flex flex-col items-center wrapper
- calendar-controlled: restore the second button row (设为今天/设为圣诞节/
  清空) and the baseline feedback wording (已选日期:…(未选))
- locale inheritance: add a public useLocale() accessor (the components'
  ConfigProvider → en-US/ltr chain) and use it for startOfWeek in
  calendar-controlled, isWeekend in booking-calendar/unavailable-dates and
  the DateFormatter in date-range-picker-format-options instead of the
  hardcoded 'en-US' / 'zh-CN' literals
- date-range-picker-format-options: restore the pinned w-full popover class;
  the pinned source positions the popover against the initial containing
  block (full viewport width — verified on the reference docs at desktop and
  narrow), while Reka's shrink-to-fit positioning wrapper collapses the same
  class. A scoped adapter rule (.hv-date-range-picker__popover.w-full →
  100vw, layering-safe like the dropdown static-wrapper adapter) restores
  the baseline effect: viewport-wide popover, 252px calendar, stretched
  time rows, identical boundary clamping

- tests: locale dynamic-switching regressions covering zh-CN/en-US for week
  start, weekend rules (with ar-EG flipping the weekend for a observable
  rule change) and range formatting; full suite 110 files / 1627 tests,
  typecheck, build:docs and the date-range-picker / api-root-aliases /
  calendar tarball contracts pass
This commit is contained in:
maoyingjie
2026-10-28 16:35:26 +08:00
parent 0b89f02745
commit 2e3fdd4800
15 changed files with 382 additions and 147 deletions
+125 -1
View File
@@ -1,11 +1,31 @@
import { afterEach, expect, it, vi } from "vitest";
import { mount, flushPromises } from "@vue/test-utils";
import { defineComponent, h, type Component } from "vue";
import { defineComponent, h, ref, type Component, type Ref } from "vue";
import {
getDayOfWeek,
getLocalTimeZone,
isWeekend,
startOfMonth,
startOfWeek,
today,
} from "@internationalized/date";
import { ConfigProvider } from "../packages/vue/src";
const demos = import.meta.glob("../apps/docs/demos/calendar-*.vue", {
eager: true,
import: "default",
}) as Record<string, Component>;
const wrappers: ReturnType<typeof mount>[] = [];
function mountInLocale(locale: Ref<string>, name: string) {
const demo = demos[`../apps/docs/demos/${name}.vue`];
const w = mount(
defineComponent({
render: () => h(ConfigProvider, { locale: locale.value }, () => h(demo!)),
}),
{ attachTo: document.body },
);
wrappers.push(w);
return w;
}
afterEach(() => {
wrappers.splice(0).forEach((w) => w.unmount());
document.body.innerHTML = "";
@@ -30,3 +50,107 @@ it("mounts all nineteen baseline demos together without duplicate IDs or Vue war
expect(new Set(ids).size).toBe(ids.length);
expect(warn).not.toHaveBeenCalled();
});
it("the 本周 jump uses the active ConfigProvider locale and follows runtime switches", async () => {
const locale = ref("en-US");
mountInLocale(locale, "calendar-controlled");
await flushPromises();
const weekButton = () =>
[...document.querySelectorAll("button")].find(
(b) => b.textContent?.trim() === "本周",
)!;
const picked = () =>
document.body.textContent!.match(/已选日期:(\d{4}-\d{2}-\d{2})/)?.[1];
weekButton().click();
await flushPromises();
const enStart = startOfWeek(today(getLocalTimeZone()), "en-US").toString();
expect(enStart).toBe(
startOfWeek(today(getLocalTimeZone()), "en-US").toString(),
);
expect(picked()).toBe(enStart);
locale.value = "zh-CN";
await flushPromises();
weekButton().click();
await flushPromises();
const zhStart = startOfWeek(today(getLocalTimeZone()), "zh-CN").toString();
// The two locales disagree on the first day of the week (Sunday vs Monday),
// so the same action must land on different days after the switch.
expect(zhStart).not.toBe(enStart);
expect(picked()).toBe(zhStart);
});
it("weekend disabling follows the active locale, including weekend-rule changes", async () => {
const base = startOfWeek(today(getLocalTimeZone()), "en-US");
const day = (offset: number) => base.add({ days: offset }).toString();
const friday = day(5),
saturday = day(6),
sunday = day(0);
const flags = () =>
Object.fromEntries(
[friday, saturday, sunday].map((date) => [
date,
!!document
.querySelector(`[data-date="${date}"]`)
?.getAttribute("data-unavailable"),
]),
);
const locale = ref("en-US");
mountInLocale(locale, "calendar-unavailable-dates");
await flushPromises();
// en-US/zh-CN share the Sat–Sun weekend (CLDR); the flags must equal the
// locale rule either way.
expect(flags()).toEqual({
[friday]: isWeekend(base.add({ days: 5 }), "en-US"),
[saturday]: isWeekend(base.add({ days: 6 }), "en-US"),
[sunday]: isWeekend(base.add({ days: 0 }), "en-US"),
});
expect(flags()[saturday]).toBe(true);
expect(flags()[sunday]).toBe(true);
expect(flags()[friday]).toBe(false);
locale.value = "zh-CN";
await flushPromises();
expect(flags()).toEqual({
[friday]: isWeekend(base.add({ days: 5 }), "zh-CN"),
[saturday]: isWeekend(base.add({ days: 6 }), "zh-CN"),
[sunday]: isWeekend(base.add({ days: 0 }), "zh-CN"),
});
// ar-EG has a Friday–Saturday weekend: the same cells must flip.
locale.value = "ar-EG";
await flushPromises();
expect(flags()).toEqual({
[friday]: true,
[saturday]: true,
[sunday]: false,
});
});
it("the booking calendar combines locale weekends with the booked days", async () => {
const booked = [5, 6, 12, 13, 14, 20];
const monthStart = startOfMonth(today(getLocalTimeZone()));
const days = Array.from({ length: 31 }, (_, i) =>
monthStart.add({ days: i }),
).filter((d) => d.month === monthStart.month);
const iso = (d: (typeof days)[number]) => d.toString();
const unavailable = (d: (typeof days)[number]) =>
!!document
.querySelector(`[data-date="${iso(d)}"]`)
?.getAttribute("data-unavailable");
const sunday = days.find(
(d) => !booked.includes(d.day) && getDayOfWeek(d, "en-US", "sun") === 0,
)!;
const arWorkingDay = days.find(
(d) => booked.includes(d.day) && !isWeekend(d, "ar-EG"),
)!;
const locale = ref("en-US");
mountInLocale(locale, "calendar-booking-calendar");
await flushPromises();
// Booked days stay unavailable whatever the weekend rule is.
expect(unavailable(arWorkingDay)).toBe(true);
// The locale weekend applies on top of the booked days.
expect(unavailable(sunday)).toBe(true);
locale.value = "ar-EG";
await flushPromises();
// Sunday becomes a working day under ar-EG and is not booked here.
expect(unavailable(sunday)).toBe(false);
expect(unavailable(arWorkingDay)).toBe(true);
});
+45 -1
View File
@@ -1,7 +1,14 @@
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { mount, flushPromises } from "@vue/test-utils";
import { defineComponent, h, ref } from "vue";
import { readFileSync } from "node:fs";
import { getLocalTimeZone, today } from "@internationalized/date";
import {
DateFormatter,
getLocalTimeZone,
parseZonedDateTime,
today,
} from "@internationalized/date";
import { ConfigProvider } from "../packages/vue/src";
const demos = import.meta.glob("../apps/docs/demos/date-range-picker-*.vue", {
eager: true,
import: "default",
@@ -300,4 +307,41 @@ describe("DateRangePicker documentation", () => {
expect(validation.checkValidity()).toBe(true);
expect(w.find('[data-slot="field-error"]').exists()).toBe(false);
});
it("format options formats the range with the active locale and follows runtime switches", async () => {
const tz = getLocalTimeZone();
const start = parseZonedDateTime(`2026-02-03T08:45:00[${tz}]`).toDate();
const end = parseZonedDateTime(`2026-02-10T18:45:00[${tz}]`).toDate();
const formatted = (loc: string) =>
new DateFormatter(loc, {
day: "numeric",
month: "short",
year: "numeric",
}).formatRange(start, end);
expect(formatted("zh-CN")).not.toBe(formatted("en-US"));
const locale = ref("zh-CN");
const demo =
demos["../apps/docs/demos/date-range-picker-format-options.vue"];
const w = mount(
defineComponent({
render: () =>
h(ConfigProvider, { locale: locale.value }, () => h(demo as never)),
}),
{ attachTo: document.body },
);
wrappers.push(w);
await flushPromises();
await open(w);
expect(document.body.textContent).toContain("已选:");
expect(document.body.textContent).toContain(formatted("zh-CN"));
locale.value = "en-US";
await flushPromises();
expect(document.body.textContent).toContain(formatted("en-US"));
expect(document.body.textContent).not.toContain(formatted("zh-CN"));
// The same range renders with the new locale again after a switch back.
locale.value = "zh-CN";
await flushPromises();
expect(document.body.textContent).toContain(formatted("zh-CN"));
expect(document.body.textContent).not.toContain(formatted("en-US"));
});
});
+2
View File
@@ -481,6 +481,7 @@
".hv-date-range-picker__trigger-indicator",
".hv-date-range-picker__range-separator",
".hv-date-range-picker__popover",
".hv-date-range-picker__popover.w-full",
"--reka-popper-transform-origin",
"data-exiting",
"data-endpoint"
@@ -584,6 +585,7 @@
},
"api-root-aliases": {
"exports": [
"useLocale",
"Accordion",
"AccordionRoot",
"Alert",