Keep cursor position and focus on input field while typing after a backend request


I have a list of log entries served by the backend. I want to add a filtering feature which allows to restrict results while typing, requesting the new filtered list to the backend passing the typed filter string.

The problem is that when the result is returned and the View is updated, the inpunt field lose focus and this force the user to click on the input field to continue typing the filter. This is not very smooth.

Is there a way to avoid (o restore) the focus and the cursor position on the input field after a view repaint?

1 Like

If you wrap your Elm Html in Html.keyed, it will avoid overwriting the DOM every time your view function is called.

To absoluately ensure this works, you may need to add Html.keyed all the way up to the root of your Html. Often just wrapping the input and everything at its level is fine.

A fairly recent discussion on the subject:

You can try it out by adding Html.keyed into this Ellie:

Thanks, I was able to make it work in the Ellie. In my case it seems a little more complex. I try to investigate with your suggestion.

You might need to put more Html.keyed in?

So cover the input control itself, anything else that is at the same level inside whatever is the parent of the input control, and similarly all the way up to the root of your Html.

I am using ElmUI and ELMUI Table. I have tried to put Element.Keyed everywhere with no success, at the moment.

It would be easier to help if you could share an example so we can reproduce the problem.

My guess is, that the input field is inside the view where the new data from the server is rendered. So separating the data for the input and the response data should solve the problem.

Unfortunately at the moment I don’t have the code at hand.

The part of the view for log entries is an ElmUI Column with three rows:

  • first row containing the filter input
  • second row containing the log entries list
  • the third row containing a link to load more log entries

It seems that the entire ElmUI column is removed. I removed the second row in Elm but the problem persists. So is not because of the list of entries being updated.
I added DOM breakpoints (removal, subtree modification, attributes modification) in each row but no break occurs.
Adding a DOM removal breakpoint in che ElmUI Column node, breaks so is the column which is removed.

But why?

Okay. I have found the issue. Nothing to do with keyed elements.
The problem is mine. I’m using a loading view while loading data but obviously this view ‘erases’ the previous view every time. I need to manage the loading phase in a different way.


This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.