Hi everyone,
I’m building a search input form and I’m now facing following an issue, see below gif image.
What I want is, after click on fa-times
icon, the input form will be an empty.
My search component looks
<%= f = form_for :query_form, "#", [phx_change: :validate_query] %>
<p class="control has-icons-left has-icons-right">
<%= text_input f,
:query,
class: "input has-padding-left-65",
placeholder: "Search",
value: @query
%>
<span class="icon is-small is-left icon__search">
<i class="fas fa-search"></i>
</span>
<%= if @query != "" do %>
<span phx-click="clean_search"
class="icon is-small is-right icon__times"
>
<i class="fas fa-times has-cursor-pointer"></i>
</span>
<% end %>
</p>
</form>
and live.ex
#
defmodule ServerWeb.Patients.IndexLive do
@moduledoc false
use ServerWeb, :live
def mount(_session, socket) do
{:ok, assign(socket, query: "", found_patients: nil)}
end
def render(assigns), do: ServerWeb.PatientsView.render("patients.html", assigns)
def handle_event("clean_search", _params, socket) do
socket =
socket
|> assign(%{query_form: %{"query" => ""}})
|> assign(query: "")
{:noreply, socket}
end
def handle_event("validate_query", %{"query_form" => %{"query" => q}}, socket) do
socket =
socket
|> assign(%{query_form: %{"query" => q}})
|> assign(query: q)
{:noreply, socket}
end
end
Finding the solution, I came with a little fix. I had to add to the class at text_input
following function fix_reset(@query)
, i.e. class: "input has-padding-left-65 #{fix_reset(@query)}",
where the function is defined as
def fix_reset(data) when is_binary(data) do
data
|> md5sum()
|> Base.encode32(padding: false, case: :lower)
end
def fix_reset(nil) do
""
end
defp md5sum(data) when is_binary(data) do
:crypto.hash(:md5, data)
end
After that, the search input works as I wanted. See below:
Did I overlook something or is it a bug of Live View? Same behaviour I discovered also for textarea
…