i-n-g-m-a-r

i-n-g-m-a-r

What is the expected behaviour of "phx-blur" on an input?

Steps:

  • email input component is rendered for the first time, the email input field receives focus
  • I click somewhere outside of the component, NOT the button, the email input field loses focus (blur)
  • email input field does not have focus, blur event is in the past.
  • I click the next button, blur-handler receives %{"value" => "Next"}

Is this expected behaviour?

I would expect the value in the blur-handler always to be that of the email input field.
In my mind the value of the button should be completely unrelated to the “blur”, because the button “click” did not cause the “blur”.

Render component:

def render(assigns), do: ~L"""
  <div id="<%= assigns.id %>">
    <ul>
      <li><%= email_input(assigns) %></li>
      <li><%= next_button(assigns) %></li>
    </ul>
  </div>
"""

defp email_input(assigns), do:
  Phoenix.HTML.Tag.tag :input, [
    type: "email",
    name: "email",
    value: assigns.state.value,
    autofocus: true,
    "phx-blur": "blur",
    "phx-target": assigns.myself
  ]

defp next_button(assigns), do:
  Phoenix.HTML.Tag.tag :input, [
    type: "button",
    value: "Next",
    "phx-click": "next",
    "phx-target": assigns.myself
  ]

Blur handler:

def handle_event("blur", %{"value" => expect_email_input_value}, socket) do
  IO.inspect expect_email_input_value, label: "blur"  # <-- blur: "Next"
  {:noreply, socket}
end

How does the button value end up in my email input field blur handler ?

First 4 of 4 Posts Switch mode

i-n-g-m-a-r

i-n-g-m-a-r OP

Update, it’s not the blur, it’s very weird though.
Not sure whether the problem exists between my computer and chair.
I have stripped down my component to a working example, see below.

Result is: click, click, boom.
After the third click, as soon as render_error/1 returns a list item, the value of the email input is set to “Next”.
After the fourth click, the list item has been removed and the value is “” again.
The value setting in the state is always “”.

Console output:

"initialized: `%EmailInput{foobar: 0, value: \"\"}`"
"email_input: `%EmailInput{foobar: 0, value: \"\"}`"
"next clicked: `%EmailInput{foobar: 1, value: \"\"}`"
"email_input: `%EmailInput{foobar: 1, value: \"\"}`"
"next clicked: `%EmailInput{foobar: 2, value: \"\"}`"
"email_input: `%EmailInput{foobar: 2, value: \"\"}`"
"next clicked: `%EmailInput{foobar: 3, value: \"\"}`"
"Email input value is set to \"Next\": `%EmailInput{foobar: 3, value: \"\"}`"
"email_input: `%EmailInput{foobar: 3, value: \"\"}`"
"next clicked: `%EmailInput{foobar: 4, value: \"\"}`"
"email_input: `%EmailInput{foobar: 4, value: \"\"}`"
"next clicked: `%EmailInput{foobar: 5, value: \"\"}`"
"email_input: `%EmailInput{foobar: 5, value: \"\"}`"

This is the component:

defmodule EmailInput do
  use Phoenix.LiveComponent

  defstruct [:value, :foobar]

  def render(assigns) do
    ~L"""
      <div id="<%= assigns.id %>">
        <ul>
          <%= render_error(assigns) %>
          <li><%= email_input(assigns) %></li>
          <li><%= next_button(assigns) %></li>
        </ul>
      </div>
    """
  end

  def update(%{id: id, email: email} = _assigns, socket) do
    assigns = [
      id: id,
      state: %__MODULE__{value: email, foobar: 0}
    ]
    socket = assign(socket, assigns)
    IO.inspect "initialized: `#{inspect(socket.assigns.state)}`"
    {:ok, socket}
  end

  def handle_event("next", %{"value" => "Next"}, socket) do
    state  = socket.assigns.state
    foobar = state.foobar + 1
    socket = assign(socket, state: %{state | foobar: foobar})
    IO.inspect "next clicked: `#{inspect(socket.assigns.state)}`"
    {:noreply, socket}
  end

  ## private

  defp render_error(%{state: %{foobar: foobar}} = assigns) when foobar === 3 do
    # boom!
    IO.inspect "Email input value is set to \"Next\": `#{inspect(assigns.state)}`"
    Phoenix.HTML.Tag.content_tag :li, "Invalid"
  end

  defp render_error(_), do: []

  defp email_input(assigns) do
    IO.inspect "email_input: `#{inspect(assigns.state)}`"
    Phoenix.HTML.Tag.tag :input, [
      type: "email",
      name: "email",
      value: assigns.state.value
    ]
  end

  defp next_button(assigns), do:
    Phoenix.HTML.Tag.tag :input, [
      type: "button",
      value: "Next",
      "phx-click": "next",
      "phx-target": assigns.myself
    ]

end

sfusato

sfusato

It’s not set to “Next” actually. “Next” is hard-coded in the message, but not the output of the value:

IO.inspect("Email input value is set to \"Next\": `#{inspect(assigns.state)}`")

From the console output you copy-pasted above the value is still empty on your 3rd click:

"Email input value is set to \"Next\": `%EmailInput{foobar: 3, value: \"\"}`"

Value is still value: \"\". I’m not seeing it set as “Next” unless I’m missing something.

i-n-g-m-a-r

i-n-g-m-a-r OP

tnx @sfusato I responded on github.

— All posts loaded —

Where Next?

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
silverdr
Using Phoenix.LiveView.TagEngine as an EEx.Engine is deprecated! To compile HEEx, use Phoenix.LiveView.TagEngine.compile/2 instead. Sta...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
michallepicki
I am using Oban and occasionally, shortly after a deployment, a handful of jobs can fail because of dependency on other parts of the syst...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
type1fool
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
akoutmos
@hugobarauna and I (Alex Koutmos) have been hard at work on writing a book on Nerves that takes you from simply blinking LEDs to building...
New

We're in Beta

About us Mission Statement