Skip to content

fix: Form reset should trigger bindings #9930

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

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
19 changes: 19 additions & 0 deletions packages/svelte/src/internal/client/operations.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,25 @@ export function init_operations() {
// @ts-expect-error
element_prototype.__className = '';

// @ts-expect-error
HTMLInputElement.prototype.__bind = undefined;
// @ts-expect-error
HTMLSelectElement.prototype.__bind = undefined;
// @ts-expect-error
HTMLTextAreaElement.prototype.__bind = undefined;

// On form's reset, invoke bindings on elements
document.body.addEventListener('reset', (evt) => {
requestAnimationFrame(() => {
if (!evt.defaultPrevented) {
for (const e of /**@type {HTMLFormElement} */ (evt.target).elements) {
// @ts-expect-error
e.__bind && e.__bind();
}
}
});
});

first_child_get = /** @type {(this: Node) => ChildNode | null} */ (
// @ts-ignore
get_descriptor(node_prototype, 'firstChild').get
Expand Down
111 changes: 78 additions & 33 deletions packages/svelte/src/internal/client/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,30 @@ const all_registerd_events = new Set();
/** @type {Set<(events: Array<string>) => void>} */
const root_event_handles = new Set();

/**
* Add the function on the __bind attribute of the element
* This allow to handle form's reset correctly
* @param {Element} dom
* @param {()=>void} fn
*/
function binds(dom, fn) {
// @ts-ignore
if (dom.__bind) {
// special case for checkbox that can have multiple bind (group & checked for example)
// @ts-ignore
const prev = dom.__bind;
// @ts-ignore
dom.__bind = () => {
prev();
fn();
};
} else {
// @ts-ignore
dom.__bind = fn;
}
return fn;
}

/** @returns {Text} */
export function empty() {
return document.createTextNode('');
Expand Down Expand Up @@ -946,15 +970,17 @@ export function selected(dom) {
* @returns {void}
*/
export function bind_value(dom, get_value, update) {
dom.addEventListener('input', () => {
/** @type {any} */
let value = dom.value;
if (is_numberlike_input(dom)) {
value = to_number(value);
}
update(value);
});

dom.addEventListener(
'input',
binds(dom, () => {
/** @type {any} */
let value = dom.value;
if (is_numberlike_input(dom)) {
value = to_number(value);
}
update(value);
})
);
render_effect(() => {
const value = get_value();
// @ts-ignore
Expand Down Expand Up @@ -992,18 +1018,21 @@ function to_number(value) {
*/
export function bind_select_value(dom, get_value, update) {
let mounting = true;
dom.addEventListener('change', () => {
/** @type {unknown} */
let value;
if (dom.multiple) {
value = [].map.call(dom.querySelectorAll(':checked'), get_option_value);
} else {
/** @type {HTMLOptionElement | null} */
const selected_option = dom.querySelector(':checked');
value = selected_option && get_option_value(selected_option);
}
update(value);
});
dom.addEventListener(
'change',
binds(dom, () => {
/** @type {unknown} */
let value;
if (dom.multiple) {
value = [].map.call(dom.querySelectorAll(':checked'), get_option_value);
} else {
/** @type {HTMLOptionElement | null} */
const selected_option = dom.querySelector(':checked');
value = selected_option && get_option_value(selected_option);
}
update(value);
})
);
// Needs to be an effect, not a render_effect, so that in case of each loops the logic runs after the each block has updated
effect(() => {
let value = get_value();
Expand Down Expand Up @@ -1094,14 +1123,27 @@ export function bind_group(group, group_index, dom, get_value, update) {
}
}
binding_group.push(dom);
dom.addEventListener('change', () => {
// @ts-ignore
let value = dom.__value;
if (is_checkbox) {
value = get_binding_group_value(binding_group, value, dom.checked);
}
update(value);
});
dom.addEventListener(
'change',
binds(dom, () => {
// @ts-ignore
let value = dom.__value;
if (is_checkbox) {
value = get_binding_group_value(binding_group, value, dom.checked);
} else if (!dom.checked) {
// on form reset, we need to check selected item
value = null;
if (dom.form && dom.name) {
const item = dom.form.elements.namedItem(dom.name);
if (item) {
// @ts-ignore
value = item.value;
}
}
}
update(value);
})
);
render_effect(() => {
let value = get_value();
if (is_checkbox) {
Expand Down Expand Up @@ -1130,10 +1172,13 @@ export function bind_group(group, group_index, dom, get_value, update) {
* @returns {void}
*/
export function bind_checked(dom, get_value, update) {
dom.addEventListener('change', () => {
const value = dom.checked;
update(value);
});
dom.addEventListener(
'change',
binds(dom, () => {
const value = dom.checked;
update(value);
})
);
// eslint-disable-next-line eqeqeq
if (get_value() == undefined) {
update(false);
Expand Down