flowbite: Input tag value updated by Flowbite datepicker doesn't trigger phx-change="validate"
Describe the bug When I select a date from the datepicker, the selected date shows in the date input box as text, but the “validate” event is not fired. The following is my related code based on Flowbite documentation (https://flowbite.com/docs/getting-started/phoenix/)
Hooks.Datepicker = {
mounted() {
const datepickerEl = this.el;
new Datepicker(datepickerEl, {
autohide: true,
format: 'yyyy-mm-dd',
});
},
updated() {
this.mounted();
}
}
To Reproduce Steps to reproduce the behavior:
- Add the datepicker to a form that defines
phx-change="validate"in a Phoenix project based on the documentation (https://flowbite.com/docs/getting-started/phoenix/) - Click on a date on the datepicker
- Notice the “validate” event is not fired.
Expected behavior Step 3 of the Reproduce step should trigger “validate” event
Screenshots
Desktop (please complete the following information):
- OS: [e.g. iOS] Windows 11
- Browser [e.g. chrome, safari] Chrome, Firefox, Edge
- Version [e.g. 22]
Additional context
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 2
- Comments: 16 (9 by maintainers)
@jmnda-dev Just to share my findings. I tried addEventListener(“changeDate”…) like the following, and it does trigger
phx-change. However, it seems to cause LV to go into some kind of loop showing gradually more repetition of (phx-F5oyNZEPzdxLLwPl update: - {8: 1, 10: {…}}… ) as I click on the datepicker more.According to what Chris McCord said here (https://elixirforum.com/t/triggering-liveview-form-change-event-from-javascript/37073/2), this looks like it should work, but I can’t quite get it to work.
Any pointers will be appreciated. Thanks.
Hey everyone,
I’ve got some good news - since
v2.4.1the datepicker is now a core component of the Flowbite JS.That means that everything related to instances, initialization are now ported via the main Instance Manager.
For frameworks like Phoenix you no longer need to separately install the datepicker file/plugin:
https://flowbite.com/docs/components/datepicker/#javascript-behaviour
More on this here:
https://flowbite.com/docs/getting-started/phoenix/#datepicker-plugin
Cheers, Zoltan
One thing to point out in this thread. The date-picker integration guide is flawed: do not call
this.mounted();insideupdatedcallback, as this will initialize the component multiple times. This could work only if you changemountedto st:@puruzio I have created a demo repo here: https://github.com/jmnda-dev/liveview-flowbite-datepicker-demo
You will notice that the form on the
Postlisting page is not put in a modal, you are able to use the datepicker. However on thePostdetail page, the edit form is in a modal. If you click on theDate Publishedinput, the datepicker is not showing probably because thez-indexof the datepicker is lower than that of the modal and other elements on the page. So on the edit form modal on the detail page, click on theDate Publishedinput. the datepicker won’t show, then in your browser inspector search for the element with this this classdatepicker datepicker-dropdown dropdown absolute top-0 left-0 z-20 pt-2 active block datepicker-orient-top datepicker-orient-leftthen adjust itsz-index(set it higher) and you will see it will show up and you can select dates and the change events are pushed to the server.EDIT: Commented from wrong account
Hmm 🤔, I will have a look once I settle down