Skip to content

Commit b8d2c91

Browse files
committed
gah we can't fix the input in an effect, need to do it here, but after a tick so that changes have been flushed through each blocks
1 parent 184b2fc commit b8d2c91

File tree

1 file changed

+20
-11
lines changed
  • packages/svelte/src/internal/client/dom/elements/bindings

1 file changed

+20
-11
lines changed

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

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import * as e from '../../../errors.js';
66
import { is } from '../../../proxy.js';
77
import { queue_micro_task } from '../../task.js';
88
import { hydrating } from '../../hydration.js';
9-
import { untrack } from '../../../runtime.js';
9+
import { tick, untrack } from '../../../runtime.js';
1010
import { is_runes } from '../../../context.js';
1111
import { current_batch, previous_batch } from '../../../reactivity/batch.js';
1212

@@ -21,7 +21,7 @@ export function bind_value(input, get, set = get) {
2121

2222
var batches = new WeakSet();
2323

24-
listen_to_event_and_reset_event(input, 'input', (is_reset) => {
24+
listen_to_event_and_reset_event(input, 'input', async (is_reset) => {
2525
if (DEV && input.type === 'checkbox') {
2626
// TODO should this happen in prod too?
2727
e.bind_invalid_checkbox_value();
@@ -35,6 +35,24 @@ export function bind_value(input, get, set = get) {
3535
if (current_batch !== null) {
3636
batches.add(current_batch);
3737
}
38+
39+
await tick();
40+
41+
// In runes mode, respect any validation in accessors (doesn't apply in legacy mode,
42+
// because we use mutable state which ensures the render effect always runs)
43+
if (runes && value !== (value = get())) {
44+
var start = input.selectionStart;
45+
var end = input.selectionEnd;
46+
47+
// the value is coerced on assignment
48+
input.value = value ?? '';
49+
50+
// Restore selection
51+
if (end !== null) {
52+
input.selectionStart = start;
53+
input.selectionEnd = Math.min(end, input.value.length);
54+
}
55+
}
3856
});
3957

4058
if (
@@ -88,17 +106,8 @@ export function bind_value(input, get, set = get) {
88106
// don't set the value of the input if it's the same to allow
89107
// minlength to work properly
90108
if (value !== input.value) {
91-
var start = input.selectionStart;
92-
var end = input.selectionEnd;
93-
94109
// @ts-expect-error the value is coerced on assignment
95110
input.value = value ?? '';
96-
97-
// Restore selection
98-
if (end !== null) {
99-
input.selectionStart = start;
100-
input.selectionEnd = Math.min(end, input.value.length);
101-
}
102111
}
103112
});
104113
}

0 commit comments

Comments
 (0)