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(type=number): Don't parseFloat empty value strings, they NaN #1136

Merged
merged 1 commit into from
Jun 10, 2016

Conversation

jayphelps
Copy link
Contributor

@jayphelps jayphelps commented Jun 10, 2016

#1100 added automatic number parsing of <input type="number"> fields but if the field is empty it currently applies parseFloat to an empty string, which produces NaN which could be argued either way as right or wrong, but this has the consequence of making the <input> warn in Google Chrome with:

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

This happens on every re-render, once that field has been touched. Using undefined also invokes the warning, the only thing that doesn't is the default empty string or a string that is a valid number.

Because of #1089 I can't create a JSBin to reproduce, but here's some code:

console.clear();

const { createStore, combineReducers } = Redux;
const { Provider } = ReactRedux;
const { reduxForm, Field, reducer: form } = ReduxForm;

/////////////////////////////////////////////////

const Form = reduxForm({
  form: 'example-form'
})(
  () =>
    <div>
      <Field name="count" type="number" component="input" />
    </div>
);

/////////////////////////////////////////////////


const store = createStore(
  combineReducers({ form })
);


ReactDOM.render(
  <Provider store={store}>
    <Form />
  </Provider>,
  document.getElementById('app')
);

If it's too awkward now the value might be an empty string or a parsed number, I think removing the parsing number behavior altogether is probably the right call. Then the behavior is the same as the browser. Especially because parseFloat has known quirks that may causes issues in browsers that don't actually restrict you from typing non-number characters in the input (like Chrome does). e.g. they type '1-dal@41' and parseFloat('1-dal@41') === 1

@erikras erikras merged commit 15965d9 into redux-form:v6 Jun 10, 2016
@erikras
Copy link
Member

erikras commented Jul 1, 2016

Published in v6.0.0-rc.1.

@antoinerousseau
Copy link

It also happens to me when I use a decimal separator that is accepted by Chrome but is not valid JS, e.g. if I try to type 12.3 with a French locale, since the separator should be ., the fields empties when I type . and I get the same alert than you.

gif

@lock
Copy link

lock bot commented Jun 2, 2018

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@lock lock bot locked as resolved and limited conversation to collaborators Jun 2, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants