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

Renders of a variable bound to an input element of type="number" will not update if the variable is changed within a reactive statement that is triggered by the input #4631

Closed
EmNudge opened this issue Apr 5, 2020 · 3 comments · Fixed by #4689
Labels

Comments

@EmNudge
Copy link
Contributor

EmNudge commented Apr 5, 2020

Describe the bug
The title is a hell of a mouthful.
I'm still not certain I've actually pinpointed the odd behavior, but this is as close as I could get.
To elaborate on the title, if an <input> element with type="number" triggers a reactive statement via a bound variable, changing the bound variable in the reactive statement will not perform any updates to places which rely on that variable.

That means, in the following example the reactive statement will trigger when myNum changes (due to the console.log). However, if it was triggered by typing in the <input> element, renders using myNum will be unaffected. If other variables are changed, those will work fine, but ones with myNum will be ignored.

Triggering the reactive statement some other way (like having a button increment myNum) will result in myNum changing to 500.

<script>
  let myNum = 0;
  $: {
    console.log(myNum);
    myNum = 500;
  }
</script>

<input type="number" bind:value={myNum} />
<button on:click={() => myNum++}>change num</button>

As an aside, @pngwn found a fix (not for the bug, but to get this REPL working). Using tick before myNum = 500 allows an update cycle and it should work as it does with type="text":
https://svelte.dev/repl/dca7070e064f4d9da9f49d4133056a76?version=3.20.1

To Reproduce
The previous example in a REPL:
https://svelte.dev/repl/2fb57570c5dd48a580619d13246a53e1?version=3.20.1
Change the type from "number" to "text" to see how its behavior changes.

For an actual use-case (and for how I ran into this), this REPL on restrictive/controlled inputs may help:
https://svelte.dev/repl/8019ec1168fe4fb99e2a2cd44545f70b?version=3.20.1

Expected behavior
like type="text", type="number" should allow myNum's change in value to affect renders.

Information about your Svelte project:

  • Your browser and the version: (e.x. Chrome 52.1, Firefox 48.0, IE 10)

  • Windows & OSX, REPL, Rollup, Chrome 80, Svelte v3.2

Severity
Medium. Creating restrictive user inputs is useful. With React it's the default way to make inputs. With svelte it's a bit more difficult and may require a bit of twisting.

@Conduitry
Copy link
Member

Conduitry commented Apr 5, 2020

Possibly related to #2816 and #3470.

The difference in behavior between number and text is presumably related to the input_updating guard in the code generated for number but I haven't dug into it any more deeply than that.

@Conduitry Conduitry added the bug label Apr 5, 2020
@pushkine
Copy link
Contributor

pushkine commented Apr 5, 2020

this.needs_lock = name === 'input' && type === 'number'; // TODO others?

line reference
it was introduced as a fix for #3426

@Conduitry
Copy link
Member

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants