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.

Where Next?

Popular in Questions Top

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
logicmason
Hi there, I'm working through my first release with elixir/phoenix. I've built a release with distillery and found that it crashes when I...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
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
kostonstyle
Hi all I want to have a unix time, from the current time plus 1 hour. DateTime.now + 1 hour How to get it in elixir? Thanks
New
chewm
Hi guys, nice to meet you to the whole forum, I’m new here, I’m trying to configure visual studio code for elixir, right now the intellis...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
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

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
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
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 record...
New
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
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
Nvim
Elixir appears to be a superior language to Python. I don’t see any advantage of Python over Elixir. Are there any?
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

We're in Beta

About us Mission Statement