Skip to content

fix(input-date-picker): allow clearing invalid date value #11937

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -1083,24 +1083,105 @@ describe("calcite-input-date-picker", () => {
});
});

it("should reset input value", async () => {
const page = await newE2EPage();
const expectedValue = "2022-10-01";
const expectedInputValue = "10/1/2022";
describe("clearing input value", () => {
describe("default", () => {
it("should clear with valid value", async () => {
const inputValue = "10/1/2022";
const expectedValue = "2022-10-01";
const page = await newE2EPage();
await page.setContent(html`<calcite-input-date-picker></calcite-input-date-picker>`);
const inputDatePicker = await page.find("calcite-input-date-picker");
const input = await page.find("calcite-input-date-picker >>> calcite-input-text");

await page.setContent(html` <calcite-input-date-picker value="${expectedValue}"></calcite-input-date-picker>`);
await inputDatePicker.callMethod("setFocus");
await page.waitForChanges();
await inputDatePicker.type(inputValue);
await inputDatePicker.press("Enter");
await page.waitForChanges();

const inputDatePickerEl = await page.find("calcite-input-date-picker");
const input = await page.find("calcite-input-date-picker >>> calcite-input-text");
expect(await inputDatePicker.getProperty("value")).toBe(expectedValue);
expect(await input.getProperty("value")).toBe(inputValue);

expect(await inputDatePickerEl.getProperty("value")).toEqual(expectedValue);
expect(await input.getProperty("value")).toEqual(expectedInputValue);
inputDatePicker.setProperty("value", "");
await page.waitForChanges();

inputDatePickerEl.setProperty("value", "");
await page.waitForChanges();
expect(await inputDatePicker.getProperty("value")).toBe("");
expect(await input.getProperty("value")).toBe("");
});

it("should clear with invalid value", async () => {
const inputValue = "13/37";
const page = await newE2EPage();
await page.setContent(html`<calcite-input-date-picker></calcite-input-date-picker>`);
const inputDatePicker = await page.find("calcite-input-date-picker");
const input = await page.find("calcite-input-date-picker >>> calcite-input-text");
await inputDatePicker.callMethod("setFocus");
await inputDatePicker.type(inputValue);
await inputDatePicker.press("Escape");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: Don't think this is required. Applies to all other tests.

await inputDatePicker.press("Tab");

expect(await inputDatePicker.getProperty("value")).toBe("");
expect(await input.getProperty("value")).toBe(inputValue);

inputDatePicker.setProperty("value", "");
await page.waitForChanges();
expect(await input.getProperty("value")).toBe("");

expect(await inputDatePicker.getProperty("value")).toBe("");
expect(await input.getProperty("value")).toBe("");
});
});

describe("range", () => {
it("should clear with valid value", async () => {
const inputValue = ["10/1/2022", "10/31/2022"];
const expectedValue = ["2022-10-01", "2022-10-31"];
const page = await newE2EPage();
await page.setContent(html` <calcite-input-date-picker range></calcite-input-date-picker>`);
const inputDatePicker = await page.find("calcite-input-date-picker");
const input = await page.find("calcite-input-date-picker >>> calcite-input-text");

expect(await inputDatePickerEl.getProperty("value")).toEqual("");
expect(await input.getProperty("value")).toEqual("");
await inputDatePicker.callMethod("setFocus");
await inputDatePicker.type(inputValue[0]);
await inputDatePicker.press("Escape");
await inputDatePicker.press("Tab");
await inputDatePicker.type(inputValue[1]);
await inputDatePicker.press("Enter");

expect(await inputDatePicker.getProperty("value")).toEqual(expectedValue);
expect(await input.getProperty("value")).toBe(inputValue[0]);

inputDatePicker.setProperty("value", "");
await page.waitForChanges();

expect(await inputDatePicker.getProperty("value")).toBe("");
expect(await input.getProperty("value")).toBe("");
});

it("should clear with invalid value", async () => {
const inputValue = ["13/37", "13/37"];
const page = await newE2EPage();
await page.setContent(html`<calcite-input-date-picker range></calcite-input-date-picker>`);
const inputDatePicker = await page.find("calcite-input-date-picker");
const input = await page.find("calcite-input-date-picker >>> calcite-input-text");

await inputDatePicker.callMethod("setFocus");
await inputDatePicker.type(inputValue[0]);
await inputDatePicker.press("Escape");
await inputDatePicker.press("Tab");
await inputDatePicker.type(inputValue[1]);
await inputDatePicker.press("Enter");

expect(await inputDatePicker.getProperty("value")).toBe("");
expect(await input.getProperty("value")).toBe(inputValue[0]);

inputDatePicker.setProperty("value", "");
await page.waitForChanges();

expect(await inputDatePicker.getProperty("value")).toBe("");
expect(await input.getProperty("value")).toBe("");
});
});
});

it("should sync its date-pickers when updated programmatically after a user modifies the range", async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -323,8 +323,10 @@ export class InputDatePicker
}

set value(value: string | string[]) {
const oldValue = this._value;
if (value !== oldValue) {
const valueChanged = value !== this._value;
const invalidValueCleared = value === "" && this.startInput?.value !== "";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the user skip setting startInput & set an invalid value in endInput value, clearing those values wouldn't work.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great catch. Fixed!


if (valueChanged || invalidValueCleared) {
this._value = value;
this.valueWatcher(value);
}
Expand Down