Hi, I’m new to Elixir so just trying to find my feet. I have a modal (from core_components.ex) open with a form in it, when the form submits, I either want to update the state to show an error, or action the form and close the modal. I’m having a bit of trouble closing the modal from the handle_event callback and wondered if anyone could give any advice. The setup that I have is…
defmodule App.FormLive do
use App, :live_view
def handle_event("form-go", form, socket) do
# Do some validation
updated_form = case did_validate?(form) do
true ->
# Close the modal
# JS.hide_modal("form-modal")
do_some_stuff_with_name(form["name"])
%{"name" => ""}
end
{:noreply, assign(socket, :form, updated_form)}
end
end
Obviously, the hide_modal call fails, but that’s what I’d ideally like to do. Is that even possible?
If not, the other thing that I’ve been thinking about is managing the open state from the server and pushing that down in the state, but it feels like because core_components already provides most of the modal functionality it would be a duplication of effort.
Thanks in advance, I’ve been scratching my head on this for a while
Phoenix.LiveView.JS is a collection of functions that provides solutions to common client-side needs (like showing/hiding modals).
The first example in the docs goes over modals so hopefully that points you in the right direction.