-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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", () => { | ||
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", () => { | ||
|
@@ -817,6 +852,29 @@ describe("flatpickr", () => { | |
expect(fp.currentYear).toBe(2016); | ||
}); | ||
|
||
it("onKeyDown closes flatpickr on Enter when allowInput set to true", () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test is to ensure that this fix is covered: |
||
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()], | ||
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This needed to be changed, otherwise if I make an actual change |
||
expect(timesFired).toEqual(0); | ||
}); | ||
}); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These changes are the revert of #2419. |
||
self._debouncedChange(); | ||
} | ||
} | ||
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Removing |
||
|
||
const isIgnored = !self.config.ignoredFocusElements.some((elem) => | ||
elem.contains(eventTarget as Node) | ||
|
@@ -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 | ||
|
@@ -1502,7 +1493,7 @@ function FlatpickrInstance( | |
) { | ||
updateTime(); | ||
} | ||
|
||
self.close(); | ||
|
||
if ( | ||
|
There was a problem hiding this comment.
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)