[NumberInput] Asymmetric value and onChange parameter
See original GitHub issueDescribe the bug
The value of NumberInput.value
has a type of number | undefined
, while NumberInput.onChange
receives number | null
as a parameter.
Suggested solution(s)
NumberInput.value
should also be intentionally nullable, as described by #243.
Issue Analytics
- State:
- Created 4 years ago
- Comments:7 (7 by maintainers)
Top Results From Across the Web
How to make a dynamic onChange handler to <NumberInput ...
I noticed in Chakra that NumberInput components pass in the value of the input as the first parameter to the onChange callback rather...
Read more >onchange event for input type="number" - jquery
When the mouseup event calls your function, compare that value to the current value of the input. Only do something if the value...
Read more >HTML onchange Attribute - W3Schools
Definition and Usage. The onchange attribute fires the moment when the value of the element is changed. Tip: This event is similar to...
Read more >How to get the changed value in Input Text? | Blazor FAQ
Use input change event to get the changed value in onchange event argument. If you bind using the two-way bind to value property, ......
Read more >HTMLElement: change event - Web APIs | MDN
The change event is fired for , , and elements when the user modifies the element's value. Unlike the input event, the change...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I thought about that and tried it. Here are my thoughts:
We’d be switching between controlled and uncontrolled mode. As far as I know, that’s not good practice. If it’s controlled, I’d like it to be “fully” controlled, and vice-versa.
If the
value
is3.45
, pressingBackspace
up until3.
. If we disallow emitting onChange when there’s a decimal point appended, the value will always be3.
except if the user deletes all (in annoyance 😅).If the user passes the
precision
prop which rounds the value to specific decimal points. So if thevalue
is3.4
and precision is2
, the expected output is3.40
which isn’t possible if we return anumber
.For now, the onChange callback will pass
string | number
, sadly.I hope you understand what I mean. If you can hack it without any issues, I’d be happy to take a look.
I’ve just released react-typed-inputs, which aims to provide a solution by utilizing inner state. As a bonus,
clampAfterBlur
androundAfterBlur
options are available.When overriding
type="number"
withtype="text"
, the following input sequence can be input without anull
result, as seen in this live demo:Unfortunately, React has a bug which results in the decimal point being clamped at the end, resulting in
12
instead of12.
inside the field. On the bright side, Preact works fine and React will also be fixed.