diff --git a/homedocs/src/pages/docs/forms/date-field.mdx b/homedocs/src/pages/docs/forms/date-field.mdx index 981821e1f..24e2eb228 100644 --- a/homedocs/src/pages/docs/forms/date-field.mdx +++ b/homedocs/src/pages/docs/forms/date-field.mdx @@ -48,6 +48,7 @@ The `DateField` widget is used for selecting dates. It supports both text input | `minExclusive` | `boolean` | `false` | If `true`, the minimum date itself is not selectable | | `maxValue` | `string` | | Maximum selectable date | | `maxExclusive` | `boolean` | `false` | If `true`, the maximum date itself is not selectable | +| `refDate` | `string` | | If no date is selected, the calendar opens on the month of this date | | `disabledDaysOfWeek` | `number[]` | | Days of week that cannot be selected. Sunday is 0, Saturday is 6 | ### Validation diff --git a/homedocs/src/pages/docs/forms/date-time-field.mdx b/homedocs/src/pages/docs/forms/date-time-field.mdx index 017134452..e3d910c32 100644 --- a/homedocs/src/pages/docs/forms/date-time-field.mdx +++ b/homedocs/src/pages/docs/forms/date-time-field.mdx @@ -55,6 +55,7 @@ Combine `DateField` and `TimeField` to edit the same datetime value. Use the `pa | `minExclusive` | `boolean` | `false` | If `true`, the minimum value itself is not selectable | | `maxValue` | `string` | | Maximum selectable date/time | | `maxExclusive` | `boolean` | `false` | If `true`, the maximum value itself is not selectable | +| `refDate` | `string` | | If no date is selected, the calendar picker opens on the month of this date | | `disabledDaysOfWeek` | `number[]` | | Days of week that cannot be selected. Sunday is 0, Saturday is 6 | ### Picker Options diff --git a/packages/cx/src/widgets/form/DateField.browser.spec.tsx b/packages/cx/src/widgets/form/DateField.browser.spec.tsx new file mode 100644 index 000000000..8c2b325c3 --- /dev/null +++ b/packages/cx/src/widgets/form/DateField.browser.spec.tsx @@ -0,0 +1,46 @@ +import "../../index.scss"; +import { describe, expect, it } from "vitest"; +import { userEvent } from "vitest/browser"; +import { createModel } from "../../data/createAccessorModelProxy"; +import { Store } from "../../data/Store"; +import { DateField } from "./DateField"; +import { render } from "../../util/test/render"; + +interface Model { + date: string; + refDate: string; +} + +const m = createModel(); + +// the month and year of the days of the month shown in the open calendar +function shownMonth() { + let day = document.querySelector(".cxb-calendar td.cxe-calendar-day:not(.cxs-outside)"); + return day ? `${day.dataset.year}-${day.dataset.month}` : null; +} + +describe("DateField", () => { + async function open(field: any, data: Record) { + let store = new Store({ data }); + let screen = await render({field}, { store }); + await userEvent.click(screen.container.querySelector("input")!); + await expect.poll(shownMonth).not.toBeNull(); + return store; + } + + it("opens the calendar on the month of refDate if no date is selected (#549)", async () => { + await open(, { date: null, refDate: "2024-03-15" }); + expect(shownMonth()).toBe("2024-3"); + }); + + it("opens the calendar on the month of the selected date, also with refDate", async () => { + await open(, { date: "2025-07-04" }); + expect(shownMonth()).toBe("2025-7"); + }); + + it("opens the calendar on the current month without refDate", async () => { + await open(, { date: null }); + let today = new Date(); + expect(shownMonth()).toBe(`${today.getFullYear()}-${today.getMonth() + 1}`); + }); +}); diff --git a/packages/cx/src/widgets/form/DateTimeField.tsx b/packages/cx/src/widgets/form/DateTimeField.tsx index 3a6ecd404..73a4b07c4 100644 --- a/packages/cx/src/widgets/form/DateTimeField.tsx +++ b/packages/cx/src/widgets/form/DateTimeField.tsx @@ -57,6 +57,12 @@ export interface DateTimeFieldConfig extends FieldConfig { /** Set to `true` to disallow the `maxValue`. Default value is `false`. */ maxExclusive?: BooleanProp; + /** + * Reference date of the calendar picker. If no date is selected, the calendar opens on the month of this date + * instead of the current month, e.g. if the current month cannot be selected. + */ + refDate?: Prop; + /** Date format used to display the selected date. */ format?: StringProp; @@ -141,6 +147,7 @@ export class DateTimeField extends Field { declare public disabledDaysOfWeekErrorText?: string; declare public value?: unknown; declare public minValue?: unknown; + declare public refDate?: unknown; declare public maxValue?: unknown; declare public minExclusive?: boolean; declare public maxExclusive?: boolean; @@ -225,8 +232,6 @@ export class DateTimeField extends Field { data.date = date; } else data.formatted = ""; - if (data.refDate) data.refDate = zeroTime(parseDateInvariant(data.refDate)); - if (data.maxValue) data.maxValue = parseDateInvariant(data.maxValue); if (data.minValue) data.minValue = parseDateInvariant(data.minValue); @@ -300,6 +305,7 @@ export class DateTimeField extends Field { data={instance.data} picker={{ value: this.value, + refDate: this.refDate, minValue: this.minValue, maxValue: this.maxValue, minExclusive: this.minExclusive,