nkbt / react-debounce-input

React component that renders Input with debounced onChange
MIT License
449 stars 61 forks source link

zero value for type="number" set to empty string on initial render #62

Closed sentilesdal closed 3 years ago

sentilesdal commented 7 years ago
  getInitialState() {
    return {
      value: this.props.value || '';
    };
  },

should be

  getInitialState() {
    let value = this.props.value || '';

    if (this.props.type === 'number') {
      value = value || '0';
    }

    return {value};
  },

or some such equivalent. thanks for the good work though!

nkbt commented 7 years ago

You're right! Thanks. I'll accept the PR if you are keen... On Fri., 16 Dec. 2016 at 10:28, Matthew Brown notifications@github.com wrote:

getInitialState() { return { value: this.props.value || ''; }; },

should be

getInitialState() { let value = this.props.value || '';

if (this.props.type === 'number') {
  value = value || '0';
}

return {value};

},

or some such equivalent. thanks for the good work though!

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/nkbt/react-debounce-input/issues/62, or mute the thread https://github.com/notifications/unsubscribe-auth/AAKsoEBPDl6PkmNPXdvdvnivgpmdGbKQks5rIc0TgaJpZM4LOsER .

burtyish commented 7 years ago

I recently made the change below on our own fork. We don't test for type === 'number' because we need the same functionality for text inputs as well as number. @nkbt If you approve of my version, I don't mind making a PR.

getInitialState() {
    return {
        value: this.props.value || this.props.value === 0 ? this.props.value : ''
    };
  },
nkbt commented 7 years ago

I think it may be better to do the other way around, plus cast to string like:

  value: this.props.value === undefined ? '' : `${this.props.value}`

As far as I remember there is a code that relies on value being a string.

alextrastero commented 4 years ago

ping?

nkbt commented 3 years ago

Closed by #125