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 inputting of decimals like "1.01" in <input type="number"> #31

Merged
merged 1 commit into from
Apr 15, 2020

Conversation

ericyhwang
Copy link
Contributor

This PR fixes a bug in Derby where a user cannot enter decimals like "1.01" into an element like this:

<input type="number" value="{{modelValue}}">

As soon as the user has typed, say, "1.0", the input immediately gets reset to "1". This applies to any decimal that starts with "NN.0".

Explanation of the issue

For bindings in <input type="number" value="{{modelValue}}">, Derby will automatically turn the user-entered value into a numeric modelValue.

That's done in here:
https://github.com/derbyjs/derby/blob/57d28637e8489244cc8438041e6c61d9468cd344/lib/documentListeners.js#L23-L25
https://github.com/derbyjs/derby/blob/57d28637e8489244cc8438041e6c61d9468cd344/lib/documentListeners.js#L99-L102

However, the expression.set(binding.context, value) in the second link eventually results in DynamicAttribute.update getting called.

That function does do a check to skip updating the property if it doesn't need to be changed. However, because the HTML input value property is a string, the code stringifies 1 into '1', then compares it to the user-entered value '1.0'. The two are different, so the code sets the value to '1', resulting in the observed bug.

The fix

Special-case updates to the <input type="number"> value property to do a comparison based on the numeric value.

@ericyhwang
Copy link
Contributor Author

<input type="range"> without multiple is also mapped between and number in the same way:
https://github.com/derbyjs/derby/blob/57d28637e8489244cc8438041e6c61d9468cd344/lib/documentListeners.js#L21

Ideally I'd be able to find a way to share that check between saddle and the main derby repo. A shared function would have to be in saddle, since derby -> saddle is the dependency direction.

@ericyhwang
Copy link
Contributor Author

Never mind - The browser UI control for <input type="range"> is a slider, so it's not affected by the number entry bug.

@ericyhwang ericyhwang merged commit 2c73757 into master Apr 15, 2020
@ericyhwang ericyhwang deleted the fix-input-number-decimals branch April 15, 2020 23:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant