Skip to content

Input element doesn't update with 1-way bind to value prop #6134

Open
@jajabarr

Description

@jajabarr

Is this about svelte@next? This project is currently in a pre-release stage and breaking changes may occur at any time. Please do not post any kind of bug reports or questions on GitHub about it.

Describe the bug
When updating a variable via event value from on:input, and then 1-way binding the value of the element to the variable, it would be expected that when the variable updates, so does the input value.

Logs
N/A

To Reproduce
REPL link: https://svelte.dev/repl/6ea9d09354874ab79685d1131f7d7d77?version=3.35.0

Expected behavior
The input updates when the variable bound to value changes

Stacktraces
N/A

Information about your Svelte project:

Google Chrome 89.0.4389.90 (Official Build) (64-bit) (cohort: Stable)
Revision 62eb262cdaae9ef819aadd778193781455ec7a49-refs/branch-heads/4389@{#1534}
OS Windows 10 OS Version 2004 (Build 19041.867)
JavaScript V8 8.9.255.20

Severity:
Severe? I would really expect this to just work. I'd like to have an input with custom validation, and two-way binding causes unnecessary updates, because reacting to the two-way bound variable will always result in two updates (one from raw input update, the next via validation and reassignment to the variable)

Raw code:

<script>
	let inputValue = 0;
	let previousSafeValue = 0;
	
	const onUpdate = (event) => {
		const rawInput = event.target.value;
		if (isNaN(+rawInput)) {
			inputValue = previousSafeValue;
		} else {
		        // >>bug<< This does NOT update the input 
			inputValue = previousSafeValue = Number(rawInput);
		}
	}
</script>

<input value={inputValue} on:input={onUpdate}/>

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions