Skip to content
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

Fix #2419 breaking with altInput and altFormat Enabled #2662

Merged
merged 1 commit into from
Mar 12, 2022
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
Fix #2419 breaking with altInput and altFormat Enabled
Fixes #2653, Fixes #2660, Refs. #2541, #2601
  • Loading branch information
mshibuya committed Mar 12, 2022
commit ea09ebd110c3fc1fcc713ff05f0663158dc50202
61 changes: 60 additions & 1 deletion __tests__/src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -751,6 +751,41 @@ describe("flatpickr", () => {
expect(fp.selectedDates[0]).toEqual(new Date("2020-01-22T00:00:00"));
expect(fp._input.value).toBe("22 January 2020");
});

it("should be updated correctly via mouse events when altInput is enabled #2653", () => {
createInstance({
allowInput: true,
altInput: true,
altFormat: "j F Y H:i",
defaultDate: "2020-01-22 03:04",
enableTime: true,
});

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

it("should trigger onChange only once", () => {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This test is for the issue mentioned here:
#2601 (comment)

let timesFired = 0;

createInstance({
allowInput: true,
altInput: true,
altFormat: "j F Y H:i",
enableTime: true,
onChange: () => timesFired++,
});

simulate("focus", fp._input);
fp._input.value = "22 January 2020 03:04";
simulate("mousedown", window.document.body, { which: 1 }, CustomEvent);
simulate("blur", fp._input);
expect(fp.isOpen).toBe(false);
expect(timesFired).toEqual(1);
});
});

it("onKeyDown", () => {
Expand Down Expand Up @@ -817,6 +852,29 @@ describe("flatpickr", () => {
expect(fp.currentYear).toBe(2016);
});

it("onKeyDown closes flatpickr on Enter when allowInput set to true", () => {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

createInstance({
enableTime: true,
allowInput: true,
altInput: true,
});

fp.jumpToDate("2016-2-1");

fp.open();
(fp.days.childNodes[15] as HTMLSpanElement).focus();

simulate(
"keydown",
fp._input,
{
keyCode: 13, // "Enter"
},
KeyboardEvent
);
expect(fp.isOpen).toEqual(false);
});

it("enabling dates by function", () => {
createInstance({
enable: [(d) => d.getDate() === 6, new Date()],
Expand Down Expand Up @@ -1830,10 +1888,11 @@ describe("flatpickr", () => {
it("doesn't misfire", () => {
let timesFired = 0;
const fp = createInstance({
allowInput: true,
onChange: () => timesFired++,
});
fp._input.focus();
document.body.focus();
simulate("blur", fp._input);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This needed to be changed, otherwise if I make an actual change onChange won't be fired.

expect(timesFired).toEqual(0);
});
});
Expand Down
27 changes: 9 additions & 18 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,17 +197,12 @@ function FlatpickrInstance(
timeWrapper(e);
}

const valueFromInput = self._input.value;
const dateFromInput = self.parseDate(valueFromInput);
const latestDate = self.latestSelectedDateObj;
if (valueFromInput && latestDate && dateFromInput?.getTime() !== latestDate?.getTime()) {
setDate(dateFromInput!);
} else {
setHoursFromInputs();
}
const prevValue = self._input.value;

setHoursFromInputs();
updateValue();

if (self._input.value !== valueFromInput) {
if (self._input.value !== prevValue) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

These changes are the revert of #2419.

self._debouncedChange();
}
}
Expand Down Expand Up @@ -1470,13 +1465,9 @@ function FlatpickrInstance(
~(e as any).path.indexOf(self.altInput)));

const lostFocus =
e.type === "blur"
? isInput &&
e.relatedTarget &&
!isCalendarElem(e.relatedTarget as HTMLElement)
: !isInput &&
!isCalendarElement &&
!isCalendarElem(e.relatedTarget as HTMLElement);
!isInput &&
!isCalendarElement &&
!isCalendarElem(e.relatedTarget as HTMLElement);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removing e.type === "blur" since it's confusing.
We used to accept blur events in the documentClick(), but we no longer after a7acd8f.


const isIgnored = !self.config.ignoredFocusElements.some((elem) =>
elem.contains(eventTarget as Node)
Expand All @@ -1486,7 +1477,7 @@ function FlatpickrInstance(
if (self.config.allowInput) {
self.setDate(
self._input.value,
true,
false,
self.config.altInput
? self.config.altFormat
: self.config.dateFormat
Expand All @@ -1502,7 +1493,7 @@ function FlatpickrInstance(
) {
updateTime();
}

self.close();

if (
Expand Down