Skip to content

feat: take form resets into account for two way bindings #10617

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

Merged
merged 10 commits into from
Mar 22, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/silly-ways-wash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"svelte": patch
---

feat: take form resets into account for two way bindings
28 changes: 28 additions & 0 deletions packages/svelte/src/internal/client/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -1010,6 +1010,26 @@ export function selected(dom) {
});
}

/**
*
* @param {HTMLInputElement | HTMLSelectElement} element
* @param {(value: unknown) => void} update
*/
function listen_to_form_reset(element, update) {
// Inside effect to ensure the element is connected to the DOM
effect(() => {
const form = element.form;
if (form) {
const handler = () => {
// TODO defaultValue handling needs more thought
update(element.defaultValue || '');
};
form.addEventListener('reset', handler);
return () => form.removeEventListener('reset', handler);
}
});
}

/**
* @param {HTMLInputElement} dom
* @param {() => unknown} get_value
Expand Down Expand Up @@ -1056,6 +1076,8 @@ export function bind_value(dom, get_value, update) {

dom.value = stringify(value);
});

listen_to_form_reset(dom, update);
}

/**
Expand Down Expand Up @@ -1109,6 +1131,8 @@ export function bind_select_value(dom, get_value, update) {
dom.__value = value;
mounting = false;
});

listen_to_form_reset(dom, update);
}

/**
Expand Down Expand Up @@ -1210,6 +1234,8 @@ export function bind_group(group, group_index, dom, get_value, update) {
}
};
});

listen_to_form_reset(dom, update);
}

/**
Expand All @@ -1231,6 +1257,8 @@ export function bind_checked(dom, get_value, update) {
const value = get_value();
dom.checked = Boolean(value);
});

listen_to_form_reset(dom, update);
}

/**
Expand Down