Skip to content

Commit 2d17f4a

Browse files
committed
do not remove defaults if they are in spreads
1 parent 44ac3a9 commit 2d17f4a

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

packages/svelte/src/compiler/phases/3-transform/client/visitors/RegularElement.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,16 @@ export function RegularElement(node, context) {
172172
bindings.has('group') ||
173173
(!bindings.has('group') && has_value_attribute))
174174
) {
175-
context.state.init.push(b.stmt(b.call('$.remove_input_defaults', context.state.node)));
175+
const spreads = has_spread
176+
? b.object(
177+
attributes
178+
.filter((attr) => attr.type === 'SpreadAttribute')
179+
.map((attr) => b.spread(attr.expression))
180+
)
181+
: null;
182+
context.state.init.push(
183+
b.stmt(b.call('$.remove_input_defaults', context.state.node, spreads))
184+
);
176185
}
177186
}
178187

packages/svelte/src/internal/client/dom/elements/attributes.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,20 @@ const IS_CUSTOM_ELEMENT = Symbol('is custom element');
3131
const IS_HTML = Symbol('is html');
3232

3333
/**
34-
* The value/checked attribute in the template actually corresponds to the defaultValue property, so we need
35-
* to remove it upon hydration to avoid a bug when someone resets the form value.
34+
* The value/checked attribute in the template actually corresponds to the defaultValue property,
35+
* so we need to remove it upon hydration to avoid a bug when someone resets the form value,
36+
* unless the property is presented in the spreaded objects and is handled by `set_attributes()`
3637
* @param {HTMLInputElement} input
38+
* @param {Record<string, any>} [spread]
3739
* @returns {void}
3840
*/
39-
export function remove_input_defaults(input) {
41+
export function remove_input_defaults(input, spread) {
4042
if (!hydrating) return;
4143

44+
if (spread && (input.type === 'checkbox' ? 'defaultChecked' : 'defaultValue') in spread) {
45+
return;
46+
}
47+
4248
var already_removed = false;
4349

4450
// We try and remove the default attributes later, rather than sync during hydration.

0 commit comments

Comments
 (0)