Skip to content

Commit 57d57e3

Browse files
committed
handle reset case specifically: use different props/queries in that case
1 parent 6ea0425 commit 57d57e3

File tree

3 files changed

+15
-13
lines changed

3 files changed

+15
-13
lines changed

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,21 @@ import { is_runes, untrack } from '../../../runtime.js';
1616
export function bind_value(input, get, set = get) {
1717
var runes = is_runes();
1818

19-
listen_to_event_and_reset_event(input, 'input', () => {
19+
listen_to_event_and_reset_event(input, 'input', (is_reset) => {
2020
if (DEV && input.type === 'checkbox') {
2121
// TODO should this happen in prod too?
2222
e.bind_invalid_checkbox_value();
2323
}
2424

25-
/** @type {unknown} */
26-
var value = is_numberlike_input(input) ? to_number(input.value) : input.value;
25+
/** @type {any} */
26+
var value = is_reset ? input.defaultValue : input.value;
27+
value = is_numberlike_input(input) ? to_number(value) : value;
2728
set(value);
2829

2930
// In runes mode, respect any validation in accessors (doesn't apply in legacy mode,
3031
// because we use mutable state which ensures the render effect always runs)
3132
if (runes && value !== (value = get())) {
32-
// @ts-expect-error the value is coerced on assignment
33+
// the value is coerced on assignment
3334
input.value = value ?? '';
3435
}
3536
});
@@ -179,8 +180,8 @@ export function bind_group(inputs, group_index, input, get, set = get) {
179180
* @returns {void}
180181
*/
181182
export function bind_checked(input, get, set = get) {
182-
listen_to_event_and_reset_event(input, 'change', () => {
183-
var value = input.checked;
183+
listen_to_event_and_reset_event(input, 'change', (is_reset) => {
184+
var value = is_reset ? input.defaultChecked : input.checked;
184185
set(value);
185186
});
186187

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,15 +80,16 @@ export function init_select(select, get_value) {
8080
export function bind_select_value(select, get, set = get) {
8181
var mounting = true;
8282

83-
listen_to_event_and_reset_event(select, 'change', () => {
83+
listen_to_event_and_reset_event(select, 'change', (is_reset) => {
84+
var query = is_reset ? '[selected]' : ':checked';
8485
/** @type {unknown} */
8586
var value;
8687

8788
if (select.multiple) {
88-
value = [].map.call(select.querySelectorAll(':checked'), get_option_value);
89+
value = [].map.call(select.querySelectorAll(query), get_option_value);
8990
} else {
9091
/** @type {HTMLOptionElement | null} */
91-
var selected_option = select.querySelector(':checked');
92+
var selected_option = select.querySelector(query);
9293
value = selected_option && get_option_value(selected_option);
9394
}
9495

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ export function without_reactive_context(fn) {
5353
* to notify all bindings when the form is reset
5454
* @param {HTMLElement} element
5555
* @param {string} event
56-
* @param {() => void} handler
57-
* @param {() => void} [on_reset]
56+
* @param {(is_reset?: true) => void} handler
57+
* @param {(is_reset?: true) => void} [on_reset]
5858
*/
5959
export function listen_to_event_and_reset_event(element, event, handler, on_reset = handler) {
6060
element.addEventListener(event, () => without_reactive_context(handler));
@@ -65,11 +65,11 @@ export function listen_to_event_and_reset_event(element, event, handler, on_rese
6565
// @ts-expect-error
6666
element.__on_r = () => {
6767
prev();
68-
on_reset();
68+
on_reset(true);
6969
};
7070
} else {
7171
// @ts-expect-error
72-
element.__on_r = on_reset;
72+
element.__on_r = () => on_reset(true);
7373
}
7474

7575
add_form_reset_listener();

0 commit comments

Comments
 (0)