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

For input numbers we should use null instead undefined in toNumber #1701

Closed
stalkerg opened this issue Aug 28, 2018 · 10 comments · Fixed by #4772
Closed

For input numbers we should use null instead undefined in toNumber #1701

stalkerg opened this issue Aug 28, 2018 · 10 comments · Fixed by #4772
Labels

Comments

@stalkerg
Copy link
Contributor

If you return undefined it became the problem for some situations like this (we can't set undefined back):
https://svelte.technology/repl?version=2.30.1&gist=4728f0fd73979c92e7c07ec0a7eebb94

by the standard, the "number input" can contain numbers or empty and empty here is an empty string or null.
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/number

@Conduitry
Copy link
Member

I don't know what the problem was supposed to have been when this issue was opened (and I also don't know what version it was against. there is no 2.30.1), but in the latest v2 I don't see anything in the console https://v2.svelte.dev/repl?version=2.16.1&gist=4728f0fd73979c92e7c07ec0a7eebb94

@stalkerg
Copy link
Contributor Author

@Conduitry
this problem still exists, I can reproduce it even on v3 https://svelte.dev/repl/922cb0532c56400a94d95e8d4536e322?version=3.8.0

please reopen. The main idea is you can't return undefined from "input number" because you can't set it back, it's just not valid by the standard.

@Conduitry Conduitry added the awaiting submitter needs a reproduction, or clarification label Aug 15, 2019
@Conduitry Conduitry reopened this Aug 15, 2019
@Conduitry
Copy link
Member

Returning undefined for an empty number input field happened in #606 as a solution for #584. I don't know what returning null instead would do.

@stalkerg
Copy link
Contributor Author

I see, anyway return null also should fix #584 and will be consistent with browsers standards.
(and fix browsers warnings)

@stalkerg
Copy link
Contributor Author

Warnings like this:

VM67:304 The specified value "undefined" is not a valid number. The value must match to the following regular expression: -?(\d+|\d+\.\d+|\.\d+)([eE][-+]?\d+)?

@stalkerg
Copy link
Contributor Author

@Conduitry did you see the same warning?

@antony
Copy link
Member

antony commented Apr 9, 2020

I see the warning in 3.20.1 - note that the warning doesn't appear in the Svelte console, but the browser console.

@antony antony added bug and removed awaiting submitter needs a reproduction, or clarification labels Apr 9, 2020
@tanhauhau
Copy link
Member

Researched on how vue is handling this case,

export function toNumber (val: string): number | string {
  const n = parseFloat(val)
  return isNaN(n) ? val : n
}

https://github.com/vuejs/vue/blob/98b4d683f578bb09c4e56f35048e49441c590a41/src/shared/util.js#L97-L100

we can also set '' as input.value if the value is ''

@Conduitry
Copy link
Member

This has been changed to use null instead of undefined in 3.25.0.

@stalkerg
Copy link
Contributor Author

Thanks!

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.

5 participants