Skip to content

Commit acfcdba

Browse files
authored
fix: reset form inputs by default when using remote form functions (#14322)
Fixes a weird inconsistency: Full page reloads clear the form fields, but when using JS to enhance it (which happens by default) it's no longer resetting. This fixes that. If you enhance you still can decide which way you want to go.
1 parent 6652939 commit acfcdba

File tree

3 files changed

+8
-2
lines changed

3 files changed

+8
-2
lines changed

.changeset/twelve-turkeys-reply.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/kit': patch
3+
---
4+
5+
fix: reset form inputs by default when using remote form functions

packages/kit/src/runtime/client/remote-functions/form.svelte.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ export function form(id) {
210210
};
211211
};
212212

213-
instance.onsubmit = form_onsubmit(({ submit }) => submit());
213+
instance.onsubmit = form_onsubmit(({ submit, form }) => submit().then(() => form.reset()));
214214

215215
/** @param {Parameters<RemoteForm<any>['buttonProps']['enhance']>[0]} callback */
216216
const form_action_onclick = (callback) => {
@@ -247,7 +247,7 @@ export function form(id) {
247247
type: 'submit',
248248
formmethod: 'POST',
249249
formaction: action,
250-
onclick: form_action_onclick(({ submit }) => submit())
250+
onclick: form_action_onclick(({ submit, form }) => submit().then(() => form.reset()))
251251
};
252252

253253
Object.defineProperty(button_props, 'enhance', {

packages/kit/test/apps/basics/test/test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1605,6 +1605,7 @@ test.describe('remote functions', () => {
16051605
await page.fill('#input-task', 'hi');
16061606
await page.click('#submit-btn-one');
16071607
await expect(page.locator('#form-result-1')).toHaveText('hi');
1608+
await expect(page.locator('#input-task')).toHaveValue('');
16081609
});
16091610

16101611
test('form error works', async ({ page }) => {

0 commit comments

Comments
 (0)