Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions homedocs/src/pages/docs/forms/date-field.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions homedocs/src/pages/docs/forms/date-time-field.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
46 changes: 46 additions & 0 deletions packages/cx/src/widgets/form/DateField.browser.spec.tsx
Original file line number Diff line number Diff line change
@@ -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<Model>();

// the month and year of the days of the month shown in the open calendar
function shownMonth() {
let day = document.querySelector<HTMLElement>(".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<string, any>) {
let store = new Store({ data });
let screen = await render(<cx>{field}</cx>, { 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(<DateField value={m.date} refDate={m.refDate} />, { 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(<DateField value={m.date} refDate="2024-03-15" />, { date: "2025-07-04" });
expect(shownMonth()).toBe("2025-7");
});

it("opens the calendar on the current month without refDate", async () => {
await open(<DateField value={m.date} />, { date: null });
let today = new Date();
expect(shownMonth()).toBe(`${today.getFullYear()}-${today.getMonth() + 1}`);
});
});
10 changes: 8 additions & 2 deletions packages/cx/src/widgets/form/DateTimeField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<string | Date>;

/** Date format used to display the selected date. */
format?: StringProp;

Expand Down Expand Up @@ -141,6 +147,7 @@ export class DateTimeField extends Field<DateTimeFieldConfig> {
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;
Expand Down Expand Up @@ -225,8 +232,6 @@ export class DateTimeField extends Field<DateTimeFieldConfig> {
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);
Expand Down Expand Up @@ -300,6 +305,7 @@ export class DateTimeField extends Field<DateTimeFieldConfig> {
data={instance.data}
picker={{
value: this.value,
refDate: this.refDate,
minValue: this.minValue,
maxValue: this.maxValue,
minExclusive: this.minExclusive,
Expand Down
Loading