Skip to content

Commit bc7d36e

Browse files
committed
sinple function + fix for radio
1 parent be90d2e commit bc7d36e

File tree

2 files changed

+29
-12
lines changed

2 files changed

+29
-12
lines changed

packages/svelte/src/internal/client/operations.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { run_all } from '../common.js';
21
import { current_hydration_fragment, get_hydration_fragment } from './hydration.js';
32
import { get_descriptor } from './utils.js';
43

@@ -86,19 +85,20 @@ export function init_operations() {
8685
element_prototype.__className = '';
8786

8887
// @ts-expect-error
89-
HTMLInputElement.prototype.__binds = undefined;
88+
HTMLInputElement.prototype.__bind = undefined;
9089
// @ts-expect-error
91-
HTMLSelectElement.prototype.__binds = undefined;
90+
HTMLSelectElement.prototype.__bind = undefined;
9291
// @ts-expect-error
93-
HTMLTextAreaElement.prototype.__binds = undefined;
92+
HTMLTextAreaElement.prototype.__bind = undefined;
9493

9594
// On form's reset, invoke bindings on elements
9695
document.body.addEventListener('reset', (evt)=> {
9796
requestAnimationFrame(() => {
98-
if (evt.defaultPrevented) return;
99-
for (const e of (/**@type {HTMLFormElement} */(evt.target)).elements) {
100-
// @ts-expect-error
101-
if (e.__binds) run_all(e.__binds);
97+
if (!evt.defaultPrevented) {
98+
for (const e of (/**@type {HTMLFormElement} */(evt.target)).elements) {
99+
// @ts-expect-error
100+
e.__bind && e.__bind();
101+
}
102102
}
103103
});
104104
});

packages/svelte/src/internal/client/render.js

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,15 +66,23 @@ const all_registerd_events = new Set();
6666
const root_event_handles = new Set();
6767

6868
/**
69-
* Add the function on the __binds attribute of the element
69+
* Add the function on the __bind attribute of the element
7070
* This allow to handle form's reset correctly
7171
* @param {Element} dom
72-
* @param {(this:Element)=>void} fn
72+
* @param {()=>void} fn
7373
*/
7474
function binds(dom, fn) {
7575
// @ts-ignore
76-
(dom.__binds ||= [])
77-
.push(fn);
76+
if (dom.__bind) {
77+
// special case for checkbox that can have multiple bind (group & checked for example)
78+
// @ts-ignore
79+
const prev = dom.__bind;
80+
// @ts-ignore
81+
dom.__bind = () => { prev(); fn(); }
82+
} else {
83+
// @ts-ignore
84+
dom.__bind = fn;
85+
}
7886
return fn;
7987
}
8088

@@ -1066,6 +1074,15 @@ export function bind_group(group, group_index, dom, get_value, update) {
10661074
let value = dom.__value;
10671075
if (is_checkbox) {
10681076
value = get_binding_group_value(binding_group, value, dom.checked);
1077+
} else if (!dom.checked) {
1078+
value = null;
1079+
if (dom.form && dom.name) {
1080+
const item = dom.form.elements.namedItem(dom.name);
1081+
if (item) {
1082+
// @ts-ignore
1083+
value = item.value;
1084+
}
1085+
}
10691086
}
10701087
update(value);
10711088
}));

0 commit comments

Comments
 (0)