Flatpickr close on any click when on Bootstrap Modal
When the Flatpickr is called from a Bootstrap (v4) modal it closes on any kind of click on it (day selection, month selection, year dropdown, hours, minutes etc). Even if click on white space inside the Flatpickr. That makes very hard to select any correct time or day.
Reproduction Link
https://jsfiddle.net/uvmqpcvq/2/
Environment
- flatpickr version used: 4.2.4
- Browser name and version: Chrome 63
- OS and version: macOS Sierra 10.12.6
It looks to work fine when it is just a single date being selected and the link above also works fine for me. But if I choose to set the mode to range does it do as described. https://jsfiddle.net/uvmqpcvq/3/
Not really, on a single date is fine, but the issue is when you also have to select time. it closes before you can select it. The issue is always the same, it closes after the first click.
I've got the same problem on Bootstrap v3.3.7.
I think it has something to do with the value of lostFocus in the documentClick function around line 1216. After clicking the calendar, lostFocus is True in my Bootstrap Modal code, but lostFocus is False in the DateTime example.
Update: isCalendarElement is False in my modal code and True in the example, which is causing lostFocus to be True in my modal code and False in the example.
This issue might be a duplicate of #1190.
Ugh.. I may just set the inline property to True to get around this.
I found a dirty fix that should solve it, at least for now. The problem that if you have more than one picker on the page you need to assign a different config object to each of them:
https://jsfiddle.net/uvmqpcvq/4/
A solution is adding a container around the input, with ID, and use appendTo config option.
appendTo: document.getElementById('picker-container')
This will move it side the modal end prevent the loss of the focus. However, the positioning is not correct, so a CSS fix is also required:
#picker-container .flatpickr-calendar {
top: 60px !important;
left: 0 !important;
}
#picker-container .flatpickr-calendar.arrowBottom {
top: auto !important;
bottom: 60px !important;
left: 0 !important;
}
Fixed in v4.3.0
Here's a fiddle using v4.2.4 - https://jsfiddle.net/wrsgns5h/ And v4.3.0 (fixed) https://jsfiddle.net/7d4fgwmq/1/
Hey!
Despite the appreciated progress, I'm still having an when issue when selecting time.
Changing the month works just fine, but both in your jsfiddle (https://jsfiddle.net/7d4fgwmq/1/) and in my code, flatpickr still closes immediately after clicking any of the time options.
@isaacrector Have you tried to set static: true, it solves my problem.
@isaacrector removing tabindex will also solve it.
@imxiaozhi It is the perfect solution. Thanks
@imxiaozhi thanks :)
Still getting this on 4.5.2 with Bootstrap 4. I tried removing the tabindex after initialising the flatpickr controls, but the problem remains.
static: true worked for me (perhaps incidentally on Bootstrap 3)
I seem to be having this problem with static: true in 4.5.2, vanilla JS.
Hey guys,
After some investigation, this looks like bad behavior on Bootstrap's part. flatpickr just does something like dayElement.focus() and hourElement.select() and bootstrap will fire some blur event - causing flatpickr to close.
Needless to say, fix incoming :)
Removing tabindex doesn't fix the problem for me, using Bootstrap 4.
Using static: true does though.
You can use static: true option for flatpickr.
Hello,
For the modal, static: true fix the problem for me with Bootstrap 4.2.1 and Flatpickr 4.5.2
static: true fixed it for us also
static: true just completely messes it up for me, making the picker invisible, save for a randomly placed vertical line. Has anyone found another workaround?
@cocowalla setting focus to false when initializing bootstrap should fix the problem better for you. Please let me know if it worked
@viorelsfetea do you mean there is a flatpickr config property called focus?
No, in Bootstrap: https://getbootstrap.com/docs/4.3/components/modal/#options
@viorelsfetea I can confirm that adding data-focus="false" to the modal actually works!
This has been an issue in our app for around a year, so many thanks for this! :grinning: :tada:
@cocowalla no worries. Sorry we don't have a fix for it yet - it's rather complicated. If you'd like, you can read a little bit more in the tickets quoted here https://github.com/twbs/bootstrap/issues/19850, as this is an issue that affects lots of other libraries too
The static: true solution will not work when using the rangePlugin inside a model, it will always open the picker under the first input..
@isaacrector removing tabindex will also solve it.
Thanks. Solved my problem.
I can confirm that removing tabindex="-1" from the modal works for this using flatpickr v4.5.2 and bs 4.4.1