Skip to content

Commit

Permalink
fix(input-date-picker, date-picker): ensure min/max can be unset (#9406)
Browse files Browse the repository at this point in the history
**Related Issue:** #7889

## Summary

Fix issue where `maxAsDate` and `minAsDate` were not being updated if
`min` or `max` were falsy.
  • Loading branch information
benelan authored May 25, 2024
1 parent 429a86a commit 89b0bfe
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,31 @@ describe("calcite-date-picker", () => {
expect(minDateAsTime).toEqual(new Date(minDateString).getTime());
});

it("unsetting min/max updates internally", async () => {
const page = await newE2EPage();
await page.emulateTimezone("America/Los_Angeles");
await page.setContent(
html`<calcite-date-picker value="2022-11-20" min="2022-11-15" max="2022-11-25"></calcite-date-picker>`,
);

const element = await page.find("calcite-date-picker");

element.setProperty("min", null);
element.setProperty("max", null);
await page.waitForChanges();

expect(await element.getProperty("minAsDate")).toBe(null);
expect(await element.getProperty("maxAsDate")).toBe(null);

const dateBeyondMax = "2022-11-26";
await setActiveDate(page, dateBeyondMax);
expect(await getActiveDate(page)).toEqual(new Date(dateBeyondMax).toISOString());

const dateBeforeMin = "2022-11-14";
await setActiveDate(page, dateBeforeMin);
expect(await getActiveDate(page)).toEqual(new Date(dateBeforeMin).toISOString());
});

it("passes down the default year prop to child date-picker-month-header", async () => {
const page = await newE2EPage();
await page.setContent(html`<calcite-date-picker value="2000-11-27" active></calcite-date-picker>`);
Expand All @@ -332,14 +357,6 @@ describe("calcite-date-picker", () => {
});

describe("ArrowKeys and PageKeys", () => {
async function setActiveDate(page: E2EPage, date: string): Promise<void> {
await page.evaluate((date) => {
const datePicker = document.querySelector("calcite-date-picker");
datePicker.activeDate = new Date(date);
}, date);
await page.waitForChanges();
}

it("should be able to navigate between months and select date using arrow keys and page keys", async () => {
const page = await newE2EPage();
await page.setContent(html`<calcite-date-picker></calcite-date-picker>`);
Expand Down Expand Up @@ -549,3 +566,18 @@ describe("calcite-date-picker", () => {
expect(await datePicker.getProperty("value")).toEqual(["2020-09-15", "2020-09-30"]);
});
});

async function setActiveDate(page: E2EPage, date: string): Promise<void> {
await page.evaluate((date) => {
const datePicker = document.querySelector("calcite-date-picker");
datePicker.activeDate = new Date(date);
}, date);
await page.waitForChanges();
}

async function getActiveDate(page: E2EPage): Promise<string> {
return await page.evaluate(() => {
const datePicker = document.querySelector("calcite-date-picker");
return datePicker.activeDate.toISOString();
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -113,19 +113,15 @@ export class DatePicker implements LocalizedComponent, LoadableComponent, T9nCom

@Watch("min")
onMinChanged(min: string): void {
if (min) {
this.minAsDate = dateFromISO(min);
}
this.minAsDate = dateFromISO(min);
}

/** Specifies the latest allowed date (`"yyyy-mm-dd"`). */
@Prop({ reflect: true }) max: string;

@Watch("max")
onMaxChanged(max: string): void {
if (max) {
this.maxAsDate = dateFromISO(max);
}
this.maxAsDate = dateFromISO(max);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -750,6 +750,27 @@ describe("calcite-input-date-picker", () => {
expect(minDateAsTime).toEqual(new Date(minDateString).getTime());
});

it("unsetting min/max updates internally", async () => {
const page = await newE2EPage();
await page.emulateTimezone("America/Los_Angeles");
await page.setContent(
html`<calcite-input-date-picker
value="2022-11-27"
min="2022-11-15"
max="2024-11-15"
></calcite-input-date-picker>`,
);

const element = await page.find("calcite-input-date-picker");

element.setProperty("min", null);
element.setProperty("max", null);
await page.waitForChanges();

expect(await element.getProperty("minAsDate")).toBe(null);
expect(await element.getProperty("maxAsDate")).toBe(null);
});

describe("owns a floating-ui", () => {
floatingUIOwner(
`<calcite-input-date-picker value="2022-11-27" min="2022-11-15" max="2024-11-15"></calcite-input-date-picker>`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,23 +243,19 @@ export class InputDatePicker
@Prop({ mutable: true }) maxAsDate: Date;

/** Specifies the earliest allowed date ("yyyy-mm-dd"). */
@Prop() min: string;
@Prop({ reflect: true }) min: string;

@Watch("min")
onMinChanged(min: string): void {
if (min) {
this.minAsDate = dateFromISO(min);
}
this.minAsDate = dateFromISO(min);
}

/** Specifies the latest allowed date ("yyyy-mm-dd"). */
@Prop() max: string;
@Prop({ reflect: true }) max: string;

@Watch("max")
onMaxChanged(max: string): void {
if (max) {
this.maxAsDate = dateFromISO(max);
}
this.maxAsDate = dateFromISO(max);
}

/** When `true`, displays the `calcite-date-picker` component. */
Expand Down

0 comments on commit 89b0bfe

Please sign in to comment.