fix: convert input value to number on hydration (#14349)

* convert input value to number on hydration

* add test

* changeset

---------

Co-authored-by: Rich Harris <rich.harris@vercel.com>
pull/14363/head
Santiago Cézar 1 month ago committed by GitHub
parent b145035a00
commit 012166ec3c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,5 @@
---
'svelte': patch
---
fix: coerce value to number when hydrating range/number input with changed value

@ -45,7 +45,7 @@ export function bind_value(input, get, set = get) {
// If we are hydrating and the value has since changed, then use the update value
// from the input instead.
if (hydrating && input.defaultValue !== input.value) {
set(input.value);
set(is_numberlike_input(input) ? to_number(input.value) : input.value);
return;
}

@ -0,0 +1,17 @@
import { flushSync } from 'svelte';
import { test } from '../../test';
export default test({
skip_mode: ['client'],
test({ assert, target, hydrate }) {
const input = /** @type {HTMLInputElement} */ (target.querySelector('input'));
input.value = '1';
input.dispatchEvent(new window.Event('input'));
// Hydration shouldn't reset the value to empty
hydrate();
flushSync();
assert.htmlEqual(target.innerHTML, '<input type="number">\n1 (number)');
}
});

@ -0,0 +1,6 @@
<script>
let value = $state(0);
</script>
<input type="number" bind:value={value}>
{value} ({typeof value})
Loading…
Cancel
Save