DestroyedSoul

DestroyedSoul

How to DaisyUI modal + form?

Hello,

I am using Phoenix 1.8.x where the modal core component has been removed.
I try to use DaisyUI modal component like this:

<!-- Button that opens modal -->
  <.button onclick="add_item_modal.showModal()" variant="primary">
    Add item
  </.button>
<!-- The modal itself-->
<dialog id="add_item_modal" class="modal">
    <div class="modal-box">
      <button
        class="btn btn-sm btn-circle btn-ghost absolute right-2 top-2"
        onclick="add_item_modal.close()"
      >
        x
      </button>
      <h3 class="text-lg font-bold">Add item</h3>
      <.form
        for={@new_item_form}
        phx-change="change_new_item_name"
        phx-submit="add_new_item"
      >
        <.input field={@new_item_form[:name]} placeholder="Item name" />
        <.button
          type="submit"
          variant="primary"
          disabled={disable_add_item_button?(@new_item_form)}
        >
          Add
        </.button>
      </.form>
    </div>
  </dialog>

Within the modal there’s a form: a single input field where I want to type the name of the item. Add button is enabled or disabled based on whether the name is empty. Pressing close or Add buttons should close the modal.

It works just fine in terms of opening, displaying the form, closing. However, whenever I type a single character, the modal closes. Apparently, this happens because live view re-renders the UI and it does not keep track of the open state for modal.

To resolve this, I tried to implement a js hook, that would check open flag on mounted and updated, and reopen the modal if needed. This worked only to an extent: the modal got reopened, but the input field lost focus: i.e. after typing each character I had to click on the field again.

Could you please share best practices on how to work with forms inside modals?
Did I go the wrong way with the js hook altogether, or was that approach fixable?

Marked As Solved

LostKobrakai

LostKobrakai

LiveView 1.1 will allow for phx-mounted={JS.ignore_attributes(["open"])}, which should also resolve this.

Also Liked

sevenseacat

sevenseacat

Author of Ash Framework

Use a Liveview JS event to open the modal, instead of a pure JavaScript onclick event.

Last Post!

DestroyedSoul

DestroyedSoul

I upgraded LiveView to 1.1.0-rc.4, and JS.ignore_attributes solved the issue, indeed. Thanks a lot!

Where Next?

Popular in Questions Top

electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New

Other popular topics Top

minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 54260 488
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New

We're in Beta

About us Mission Statement