Skip to content

Commit

Permalink
Fix flatpickr#2541 breaking with altInput enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
mshibuya committed Jan 10, 2022
1 parent 674006d commit e8f2421
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 28 deletions.
91 changes: 67 additions & 24 deletions __tests__/src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -632,36 +632,79 @@ describe("flatpickr", () => {
expect(fp.config.enable?.indexOf(null as any)).toBe(-1);
});

it("documentClick", () => {
createInstance({
mode: "range",
describe("documentClick", () => {
it("should close popup", () => {
createInstance({
mode: "range",
});

simulate("focus", fp._input, { which: 1, bubbles: true }, CustomEvent);
fp._input.focus();

expect(fp.isOpen).toBe(true);
simulate("mousedown", window.document.body, { which: 1 }, CustomEvent);
fp._input.blur();

expect(fp.isOpen).toBe(false);
expect(fp.calendarContainer.classList.contains("open")).toBe(false);

expect(fp.selectedDates.length).toBe(0);
});

simulate("focus", fp._input, { which: 1, bubbles: true }, CustomEvent);
fp._input.focus();
it("should set and clear the selectedDates", () => {
createInstance({
mode: "range",
});

expect(fp.isOpen).toBe(true);
simulate("mousedown", window.document.body, { which: 1 }, CustomEvent);
fp._input.blur();
simulate("focus", fp._input);
simulate(
"click",
fp.days.childNodes[15],
{ which: 1, bubbles: true },
CustomEvent
);
expect(fp.selectedDates.length).toBe(1);

expect(fp.isOpen).toBe(false);
expect(fp.calendarContainer.classList.contains("open")).toBe(false);
fp.isOpen = true;
simulate("mousedown", window.document.body, { which: 1 }, CustomEvent);
expect(fp.isOpen).toBe(false);
expect(fp.selectedDates.length).toBe(0);
expect(fp._input.value).toBe("");
});

expect(fp.selectedDates.length).toBe(0);
simulate("focus", fp._input);
simulate(
"click",
fp.days.childNodes[15],
{ which: 1, bubbles: true },
CustomEvent
);
expect(fp.selectedDates.length).toBe(1);
it("should revert invalid date before closing #2089", () => {
// To supress console.warn
jest.spyOn(console, "warn").mockImplementation(() => {});

fp.isOpen = true;
simulate("mousedown", window.document.body, { which: 1 }, CustomEvent);
expect(fp.isOpen).toBe(false);
expect(fp.selectedDates.length).toBe(0);
expect(fp._input.value).toBe("");
createInstance({
allowInput: true,
defaultDate: ["2020-01-22", "2020-01-28"],
mode: "range",
});

simulate("focus", fp._input);
fp._input.value = "test";
simulate("mousedown", window.document.body, { which: 1 }, CustomEvent);
expect(fp.isOpen).toBe(false);
expect(fp.selectedDates.length).toBe(0);
expect(fp._input.value).toBe("");
});

it("should use altFormat when altInput is enabled", () => {
createInstance({
allowInput: true,
altInput: true,
altFormat: "j F Y",
defaultDate: "2020-01-22",
});

expect(fp.selectedDates[0]).toEqual(new Date("2020-01-22T00:00:00"));
simulate("focus", fp._input);
simulate("mousedown", window.document.body, { which: 1 }, CustomEvent);
expect(fp.isOpen).toBe(false);
expect(fp.selectedDates[0]).toEqual(new Date("2020-01-22T00:00:00"));
expect(fp._input.value).toBe("22 January 2020");
});
});

it("onKeyDown", () => {
Expand Down
9 changes: 5 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1479,14 +1479,15 @@ function FlatpickrInstance(

if (lostFocus && isIgnored) {
if (self.config.allowInput) {
self.setDate(self._input.value,
true,
eventTarget === self.altInput
self.setDate(
self._input.value,
true,
self.config.altInput
? self.config.altFormat
: self.config.dateFormat
);
}

if (
self.timeContainer !== undefined &&
self.minuteElement !== undefined &&
Expand Down

0 comments on commit e8f2421

Please sign in to comment.