svelte: Resetting html form doesn't trigger input bindings
I’m honestly not sure if this is something that’s even possible to fix, but I just noticed that when using a <button type="reset"> on a form value bindings aren’t changed accordingly.
REPL: https://svelte.dev/repl?version=3.1.0&gist=5d0ced9c95afbdac3491bfb69bc591aa
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Reactions: 11
- Comments: 16 (10 by maintainers)
Commits related to this issue
- feat: take form resets into account for two way bindings When resetting a form, the value of the inputs within it get out of sync with the bound value of those inputs. This PR introduces a reset list... — committed to sveltejs/svelte by dummdidumm 4 months ago
- feat: take form resets into account for two way bindings (#10617) * feat: take form resets into account for two way bindings When resetting a form, the value of the inputs within it get out of syn... — committed to sveltejs/svelte by dummdidumm 3 months ago
FWIW Vue doesn’t trigger the binding update with
v-modeltoo (Example). I think it’s fine that Svelte doesn’t support this either, as it preserves the easier mental model of “on input events” only (anything external is out of reach).I did made a REPL with a
fixFormResetaction that can be used on aninputto emulate the input event on form reset. Another way is to invert this logic, to be able to use the action on aforminstead.Chiming in with my use case:
I was writing a Markdown editor, very much like GitHub’s comment box that I’m typing this in right now, where the user could preview their rendered Markdown. This component is very simple, with a textarea whose
valueis bound to a variable that is fed intoSvelteMarkdownwhen the user clicks on the preview button. Of course I used<form use:enhance>to get a progressively enhanced form that works without JS. The problem is that after I submit the form, the textarea gets cleared, but the bound value does not, meaning that the rendered Markdown is still visible in the preview tab.I worked around this by setting a listener on the reset event for the textarea’s parent form that reset the bound value like:
textarea.form.onreset = () => (value = ''). Wasn’t the ugliest thing ever but…would have been nice to not have to do that.Also got bit by this. I’m having to explicitly set the bound variables to nothing to reset the form. I have a form that’s conditionally shown, and I’m guessing that even though the form is reset before hiding, when it’s re-shown the form values are populated from the bound variables…
I don’t think this should be closed, even though it’s understandably not a priority.
No