-
Notifications
You must be signed in to change notification settings - Fork 439
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
Morphing: impossible to "clear" a form due to ignoreActiveValue: true #1194
Comments
There were two major motivations behind adding In #1141, test coverage was added for maintaining focus while filling in and submitting a form. That's useful for typeahead style searches, but also for searches where you submit through enter. Another motivation that wasn't codified in a test involves wanting to avoid potential confusion or disorientation caused by resetting the form control with focus when receiving a broadcast refresh. Imagine starting to fill out a form on a page, receiving a broadcast If we were to rollback the addition of The other scenario could be much more surprising and tricky to code around. A global Would it make sense to add support for controlling this behavior with I have a sense that the fact that Idiomorph handles morphing is to be treated like a private implementation detail. I wonder what kind of configuration attribute would be appropriate. |
@afcapel @jorgemanrubia have you encountered this scenario yet? |
@weaverryan in the short term, does this help your current issue? addEventListener("turbo:submit-end", ({ detail: { success }, target }) => {
if (success) {
target.reset()
}
}) |
@seanpdoyle we found another issue with the I think it would be safer to revert the change for now. I think it's too much of an assumption to think that you never-ever wants to change the active element. In the scenarios we've managed, using |
Closes [hotwired#1194][] Rolls back [hotwired#1141][] Don't pass the `ignoreActiveValue: true` option when Morphing. To restore that behavior, applications can set `[data-turbo-permanent]` when form control receives focus (through a `focusin` event listener), then remove it if necessary when the form control loses focus (through a `focusout` event listener). [hotwired#1141]: hotwired#1141 [hotwired#1194]: hotwired#1194
I've opened #1195. Maybe something like this could address the problem of receiving a broadcast and losing form control value: addEventListener("turbo:before-stream-render", ({ detail: { newStream } }) => {
const { activeElement } = document
if (newStream.action === "refresh" && activeElement instanceof HTMLInputElement) {
const cancelValueMorph = (event) => {
if (event.detail.attributeName === "value") {
event.preventDefault()
}
}
const removeListenerOnBlur = () => activeElement.removeEventListener(cancelValueMorph)
activeElement.addEventListener("turbo:before-morph-attribute", cancelValueMorph)
activeElement.addEventListener("blur", removeListenerOnBlur, { once: true })
}
}) That example's a bit naive, since That feels like something we could bake-into Turbo itself, either as out of the box behavior or something to configure. |
Sorry, to make sure I understand the situation correctly, you have a
Can you tell me more about this? I assume you're talking about keeping the focus on the active input through the morphing process (e.g. don't lose the cursor or cursor position in the Notice that hitting enter to "morph" keeps the cursor, even if you change its position. With a typeahead, as long as the new HTML that we're morphing into has the correct/updated Cheers! |
Yes, definitely. That's a great point. Traversing the related |
You're right, that work around might've just been a quirk of the Turbo test harness server always returning static HTML that doesn't incorporate any server state into the HTML. |
I haven't been using the A) A Maybe this distinction helps clarify a solution.
This might be true, but another problem just occurred to me. After #1195, I think we ALMOST have A) User types We solve this in LiveComponents, but in a fairly heavy-handed way that probably doesn't apply to Turbo. We track input changes that have occurred since the Ajax request started, then "preserve" those changes during morphing. I'm not certain what the correct path is for Turbo. |
…erryan) This PR was merged into the 2.x branch. Discussion ---------- [Live] Reverting ignoreActiveValue: true in Idiomorph | Q | A | ------------- | --- | Bug fix? | yes/no | New feature? | yes/no <!-- please update src/**/CHANGELOG.md files --> | Issues | Fix #... <!-- prefix each issue number with "Fix #", no need to create an issue if none exist, explain below instead --> | License | MIT This setting was recently reverted in Turbo (hotwired/turbo#1195). By setting this to true, it makes it impossible to update the active input's value with a new value from the server (see hotwired/turbo#1194). I had used this to fix a cursor position bug. We now fix it in a different way. Cheers! Commits ------- a15740e [Live] Reverting ignoreActiveValue: true in Idiomorph
Hi there!
In #1141, Idiomorph was changed to use
ignoreActiveValue: true
. That maybe makes sense... but it prevents a form from being reset/cleared.For example, in the following form, after submitting, the page redirects to the same URL, but with an empty form. The behavior varies based on whether an input is active or not. In the video:
:00-:04
) hitting enter while inside the<input>
results in that field NOT being cleared (wrong behavior).:05-:10
) clicking the submit button makes it theactiveElement
and then all fields are cleared.turbo-form-reset.mp4
I'm not sure how to handle this. In #1141, @seanpdoyle added
ignoreActiveValue: true
to support things like auto-submitting forms while typing into an input. What's not clear in that example is this: wasignoreActiveValue: true
added to preserve the value of the input or something with its active state? To ask differently: did the submitted form HTML used for morphing in that example contain the updatedvalue="another search term"
attribute value on the<input type="search">
? If not, perhaps it should have... and the example was flawed (i.e. if the new<input type="search">
does not have a value, then the value should be cleared)? If yes, wasignoreActiveValue: true
done to preserve some sort of focus or active state problem?Thanks!
The text was updated successfully, but these errors were encountered: