Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: convert input value to number on hydration #14349

Merged
merged 3 commits into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/wicked-grapes-flash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: coerce value to number when hydrating range/number input with changed value
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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)');
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<script>
let value = $state(0);
</script>

<input type="number" bind:value={value}>
{value} ({typeof value})
Loading