You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm running into an issue where I'm using a stimulus controller to submit a form based on an input's blur event, and then replacing that form using turbo frames. The problem is when the element is replaced, it refocuses the input that was just blurred.
What appears to be happening is:
Input is blurred, triggering a stimulus action (form submission)
A POST request is submitted via stimulus using fetch (requesting a turbo frame response)
The server responds with a turbo frame
The original dom element is replaced/updated (I've tested both replace and update, both have the same effect)
The form input is re-focused
Note: on the screen on which the answers/_form partial is rendered, many instances of this partial are rendered, which means there are many stimulus controllers, forms and turbo frames being update. In other words, there is one html form per Answer instance, if that makes sense. I don't see why that would cause this behavior, but thought I'd mention it.
In this video, I'm tabbing out of a field to trigger the blur event, but same thing happens for other methods of blurring.
Test.video.mp4
Here's what I'm doing:
# answer_controller.jsimport{Controller}from"@hotwired/stimulus";import{post}from"@rails/request.js";exportdefaultclassextendsController{connect(){this.form=this.element.closest("form");}submit(e){constbody=JSON.stringify({value: this.element.value,});// Set the responseKind to turbo-stream to properly wire up the turbo streams stuff in the controller.post(this.form.action,{ body,responseKind: "turbo-stream"}).then((response)=>{// handle response here}).catch((error)=>{// handle error here});}}
I believe there is a conceptual gap here. Visually, it's appears as if nothing happened, but what's really happening is that the old DOM node is ditched entirely and a new one is put in place. There is no "input that was just blurred" because the whole thing was replaced. In other words, you're expecting Turbo to remember the position of something after it's being "destroyed".
What you probably want in this case is to use morphing. Give this whole page a read and you may find a way to fix your issue.
This seems to not be an issue with the library. I recommend closing this issue and asking for support in the community forum at https://discuss.hotwired.dev
I get what you're saying, but the input is blurred -- that's what kicks off the submit that creates a turbo response that replaces the element. I don't think I'm expecting turbo to remember UI state (I've seen a few issues related to that, this seems like the opposite) -- in effect I'm wanting turbo to respect the DOM event that already happened. It seems like what's happening is it's resetting the DOM state to try to keep what it thinks the state was before it did the replace.
Page morphing seems to be thing with whole page refreshes -- I'm updating a specific part of the dom with turbo frames in response to an xhr request.
I'm running into an issue where I'm using a stimulus controller to submit a form based on an input's blur event, and then replacing that form using turbo frames. The problem is when the element is replaced, it refocuses the input that was just blurred.
What appears to be happening is:
fetch
(requesting a turbo frame response)replace
andupdate
, both have the same effect)Note: on the screen on which the
answers/_form
partial is rendered, many instances of this partial are rendered, which means there are many stimulus controllers, forms and turbo frames being update. In other words, there is one html form perAnswer
instance, if that makes sense. I don't see why that would cause this behavior, but thought I'd mention it.In this video, I'm tabbing out of a field to trigger the blur event, but same thing happens for other methods of blurring.
Test.video.mp4
Here's what I'm doing:
The text was updated successfully, but these errors were encountered: