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:

  1. 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/)
  2. Click on a date on the datepicker
  3. 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)

Most upvoted comments

@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.

Hooks.Datepicker = {
  mounted() {
      const datepickerEl = this.el;
      new Datepicker(datepickerEl, {
        autohide: true,
        format: 'yyyy-mm-dd',
      });

      this.el.addEventListener("changeDate", e => {
        let event = new Event("change", {bubbles: true});
        this.el.dispatchEvent(event);
      })

Hey everyone,

I’ve got some good news - since v2.4.1 the 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(); inside updated callback, as this will initialize the component multiple times. This could work only if you change mounted to st:

 mounted() {
      if(this.initialized) return;
      this.initialized = true;

      const datepickerEl = this.el;
      new Datepicker(datepickerEl, {
        autohide: true,
        format: 'yyyy-mm-dd',
      });
  },

@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 Post listing page is not put in a modal, you are able to use the datepicker. However on the Post detail page, the edit form is in a modal. If you click on the Date Published input, the datepicker is not showing probably because the z-index of 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 the Date Published input. the datepicker won’t show, then in your browser inspector search for the element with this this class datepicker datepicker-dropdown dropdown absolute top-0 left-0 z-20 pt-2 active block datepicker-orient-top datepicker-orient-left then adjust its z-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

@jmnda-dev I see you have contributed a lot to this project for Phoenix Liveview integration. Can you please shed some lights on what my issue might be caused by? Thanks.

Hmm 🤔, I will have a look once I settle down