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 ?

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

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.

tnx @sfusato I responded on github.