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.

Unformatted value (or floatValue) within onChange

See original GitHub issue

Is there a way to get the whole ChangeEvent with the unformatted value (or float value)?

I’m using formik and therefore need to use onChange, because it needs the ChangeEvent data to determine which field has changed.

The problem is, that onChange’s event.target.value contains the formatted value. But I need the unformatted value (or float value).

<NumberFormat
  onChange={(event) => {
    console.log(event); // I need this event
    console.log(event.target.value); // This is the formatted value, I don't want
  }}
  onValueChange={(values) => {
    console.log(values.value); // Here's the unformatted value, I want
    console.log(values.floatValue); // Here's the float value, which is okay, too
  }}
  prefix="$"
/>

In other words: I need onChange with the value provided by onValueChange.

Is there any way to get this working?

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:12
  • Comments:24 (3 by maintainers)

github_iconTop GitHub Comments

9reactions
eMarekcommented, Oct 17, 2019

I think that majority of us have problems when upgrading to v4 when this was changed:

Trigger onValueChange if the value is formatted due to prop change.

While onChange is triggered only at typing, onValueChange is triggered at typing and at prop change event. This is crucial! What most of us need is pure value at onChange listener and this is not possible anymore out of the box. I’ve tested it @rsilvamanayalle solution and it does work, but we are relying on that onChangeValue is called before onChange which is bad.

2reactions
rsilvamanayallecommented, Apr 1, 2019

Is there a way to get the whole ChangeEvent with the unformatted value (or float value)?

I’m using formik and therefore need to use onChange, because it needs the ChangeEvent data to determine which field has changed.

The problem is, that onChange’s event.target.value contains the formatted value. But I need the unformatted value (or float value).

<NumberFormat
  onChange={(event) => {
    console.log(event); // I need this event
    console.log(event.target.value); // This is the formatted value, I don't want
  }}
  onValueChange={(values) => {
    console.log(values.value); // Here's the unformatted value, I want
    console.log(values.floatValue); // Here's the float value, which is okay, too
  }}
  prefix="$"
/>

In other words: I need onChange with the value provided by onValueChange.

Is there any way to get this working?

I don’t know if this a pretty solution, but it work well for me…

const PrettyInput = ({ onChange, ...rest }) => {
  let floatValue = 0;
  return (
    <NumberFormat
      onValueChange={values => (floatValue = values.floatValue)}
      onChange={() => onChange(floatValue || '')}
      {...rest}
    />
  );
};
Read more comments on GitHub >

github_iconTop Results From Across the Web

React-Number-Format pass value as float to validation
What I'm trying to do is to format an input with react-number-format but also to pass float value to validation and to submit....
Read more >
Developers - Unformatted value (or floatValue) within onChange -
I'm using formik and therefore need to use onChange , because it needs the ChangeEvent data to determine which field has changed. The...
Read more >
Formatted and Unformatted Input/Output functions in C with ...
printf() function is used in a C program to display any value like float, integer, character, string, etc on the console screen.
Read more >
react-number-format - npm
Value to the number format. It can be a float number, or formatted string. If value is string representation of number (unformatted), isNumericString...
Read more >
Default currency values in scripts
Methods such as getValue() and getCurrencyValue() return unformatted numbers as strings. To obtain the floating point value, ...
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