question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[NumberInput] Asymmetric value and onChange parameter

See original GitHub issue

Describe 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:closed
  • Created 4 years ago
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
segunadebayocommented, Dec 4, 2019

I thought about that and tried it. Here are my thoughts:

  1. 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.

  2. If the value is 3.45, pressing Backspace up until 3.. If we disallow emitting onChange when there’s a decimal point appended, the value will always be 3. except if the user deletes all (in annoyance 😅).

  3. If the user passes the precision prop which rounds the value to specific decimal points. So if the value is 3.4 and precision is 2, the expected output is 3.40 which isn’t possible if we return a number.

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.

1reaction
kripodcommented, Jan 20, 2020

I’ve just released react-typed-inputs, which aims to provide a solution by utilizing inner state. As a bonus, clampAfterBlur and roundAfterBlur options are available.

When overriding type="number" with type="text", the following input sequence can be input without a null result, as seen in this live demo:

12 -> 12. -> 12.3 -> 12.

Unfortunately, React has a bug which results in the decimal point being clamped at the end, resulting in 12 instead of 12. inside the field. On the bright side, Preact works fine and React will also be fixed.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found