-
Notifications
You must be signed in to change notification settings - Fork 927
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Input inside component loses focus if another slot changes #3448
Comments
I did some more testing and it seems that the factor is not the slot itself being updated, but the component having a for loop. For example, changing the above code to remove the slot also triggers the same issue: attr :field, :any, required: true
defp my_component(assigns) do
~H"""
<div>
<div :for={value <- @field.value || []}>
<div><%= value %></div>
</div>
<input id="search" type="search" name="value" phx-change="search" />
</div>
"""
end
...
<.my_component field={@form[:a]} /> But, if I actually remove the attr :field, :any, required: true
defp my_component(assigns) do
~H"""
<div>
<div :for={value <- @field.value || []}>
<div><%= value %></div>
</div>
</div>
"""
end
...
<.my_component field={@form[:a]} />
<input id="search" type="search" name="value" phx-change="search" /> In the end, it seems that if I have a So, this will lose focus: <div :for={value <- @field.value || []}>
<div><%= value %></div>
</div>
<input id="search" type="search" name="value" phx-change="search" /> But this: <div><%= inspect(@field.value) %></div>
<input id="search" type="search" name="value" phx-change="search" /> Or this: <div>
<div :for={value <- @field.value || []}>
<div><%= value %></div>
</div>
</div>
<input id="search" type="search" name="value" phx-change="search" /> Will not. Also, if you invert the order of the rendering (so the input renders first and then the div with the <input id="search" type="search" name="value" phx-change="search" />
<div :for={value <- @field.value || []}>
<div><%= value %></div>
</div>
|
Yes, this is expected. Morphdom tracks elements by their position in the DOM or by ID. Once you are dynamically adding children, then it can no longer match the element, so they are effectively destroyed and re-added. If you place the |
Thanks for the reply @josevalim , can you elaborate a little bit more about adding an ID? The input already has an ID, shouldn't that be enough to make Morphdom track it and keep the input focused? Also, at least when I look at the data coming back from the server, it never touches the input field anywhere (but I guess it does touch it internally because of the added sibling element on top. |
You are right, please ignore me. I completely mixed the checkboxes with the search. :) |
Hahaha no problem! So, does that means that this can potentially a bug? If you think this is a bug/issue with Morphdom and not with LV I can create an issue in their repo. |
Yes, this looks like a bug to me. And don’t mind creating an issue in morphdom, Chris is currently the only one committing there, so it doesn’t really matter. I’ll try looking into this later this week :) |
Environment
Actual behavior
Consider that I have a component that has a slot that can change, and in the same component I also have an text input.
If I send an update to the server to change that slot, and at the same time focus on that text input, when the request comes back to the client, the input will lose focus.
Here is an example that triggers the issue:
As you can see, I have two check-boxes that will dispatch an
input
event to the form and also focus on the#search
input.Also, the
my_component
component have a slot calledleft_content
that can change depending on the@form[:a]
value (<:left_content :for={value <- @form[:a].value || []}>
).How to reproduce the error:
liveSocket.enableLatencySim(1000)
(This is not mandatory, but it helps to visualize the issue better);#search
input;:left_content
will be renderized with the value of the clicked checkbox;#search
input will lose focus.Expected behavior
The
#search
input is not being re-renderized in any way, so adding or removing a slot that doesn't have anything to do with it shouldn't make it lose focus.The text was updated successfully, but these errors were encountered: