Skip to content

Commit 00c1800

Browse files
committed
slightly different approach
1 parent a825d45 commit 00c1800

File tree

3 files changed

+35
-4
lines changed

3 files changed

+35
-4
lines changed

packages/svelte/src/internal/client/dom/elements/bindings/input.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { DEV } from 'esm-env';
22
import { render_effect } from '../../../reactivity/effects.js';
33
import { stringify } from '../../../render.js';
4+
import { listen_to_event_and_reset_event } from './shared.js';
45

56
/**
67
* @param {HTMLInputElement} input
@@ -9,7 +10,7 @@ import { stringify } from '../../../render.js';
910
* @returns {void}
1011
*/
1112
export function bind_value(input, get_value, update) {
12-
input.addEventListener('input', () => {
13+
listen_to_event_and_reset_event(input, 'input', () => {
1314
if (DEV && input.type === 'checkbox') {
1415
throw new Error(
1516
'Using bind:value together with a checkbox input is not allowed. Use bind:checked instead'
@@ -72,7 +73,7 @@ export function bind_group(inputs, group_index, input, get_value, update) {
7273

7374
binding_group.push(input);
7475

75-
input.addEventListener('change', () => {
76+
listen_to_event_and_reset_event(input, 'change', () => {
7677
// @ts-ignore
7778
var value = input.__value;
7879

@@ -114,7 +115,7 @@ export function bind_group(inputs, group_index, input, get_value, update) {
114115
* @returns {void}
115116
*/
116117
export function bind_checked(input, get_value, update) {
117-
input.addEventListener('change', () => {
118+
listen_to_event_and_reset_event(input, 'change', () => {
118119
var value = input.checked;
119120
update(value);
120121
});

packages/svelte/src/internal/client/dom/elements/bindings/select.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { effect } from '../../../reactivity/effects.js';
2+
import { listen_to_event_and_reset_event } from './shared.js';
23

34
/**
45
* Selects the correct option(s) (depending on whether this is a multiple select)
@@ -58,7 +59,7 @@ export function selected(option) {
5859
export function bind_select_value(select, get_value, update) {
5960
var mounting = true;
6061

61-
select.addEventListener('change', () => {
62+
listen_to_event_and_reset_event(select, 'change', () => {
6263
/** @type {unknown} */
6364
var value;
6465

packages/svelte/src/internal/client/dom/elements/bindings/shared.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,32 @@ export function listen(target, events, handler, call_handler_immediately = true)
2525
};
2626
});
2727
}
28+
29+
let listening_to_form_reset = false;
30+
31+
/**
32+
* Listen to the given event, and then instantiate a global form reset listener if not already done,
33+
* to notify all bindings when the form is reset
34+
* @param {HTMLElement} element
35+
* @param {string} event
36+
* @param {() => void} handler
37+
*/
38+
export function listen_to_event_and_reset_event(element, event, handler) {
39+
element.addEventListener(event, handler);
40+
// @ts-expect-error
41+
element.__on_reset = handler;
42+
43+
if (!listening_to_form_reset) {
44+
listening_to_form_reset = true;
45+
document.addEventListener('reset', (evt) => {
46+
requestAnimationFrame(() => {
47+
if (!evt.defaultPrevented) {
48+
for (const e of /**@type {HTMLFormElement} */ (evt.target).elements) {
49+
// @ts-expect-error
50+
e.__on_reset?.();
51+
}
52+
}
53+
});
54+
});
55+
}
56+
}

0 commit comments

Comments
 (0)