Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Number with decimal input in React?

I have a number input within a React component, and it needs to accept numbers with a decimal point. Usually, entries will be in the fractions of a cent, like 0.0073, that kind of thing.

<div className="form-group">
  <label htmlFor="rate" className="col-sm-6 control-label">Rate:</label>
  <div className="col-sm-2"> 
    <input type="number"
      title="Rate"
      id="rate"
      className="form-control"
      value={this.props.rate}
      min="0.00"
      step="0.001"
      max="1.00"
      onChange={()=>{
        console.log('page rate changed');
        this.props.setrate($('#rate').val());
      }} />
  </div>
</div>

The issue is that with every keystroke, it's resetting the rate for the app, and then putting that value into the input. So it goes like this:

  • User types 0, the value is set to 0, and 0 is displayed.
  • User types ., 0. isn't a valid number, so the input is cleared.

Can anyone think of a workaround? I know I could just use a normal input, but type="number" leads to some nice stuff in various browsers.

like image 650
fnsjdnfksjdb Avatar asked Jan 15 '16 19:01

fnsjdnfksjdb


People also ask

How to use React suite inputnumber component in ReactJS?

InputNumber component allows the user to enter a number within a certain range with the help of a keyboard or mouse. We can use the following approach in ReactJS to use the React Suite InputNumber Component. classPrefix: It is used to denote the prefix of the component CSS class. defaultValue: It is used to denote the default value.

How to pass value value as string in React component?

Value can be passed as string or number but if it is passed as string you should maintain the same decimal separator on the string what you provided as decimalSeparator prop. Enforce cursor to be between prefix and suffix in focus, click or arrow navigation. React component to format numbers in an input or as a text.

How to pass a number format as a prop in react?

As ref is a special property in react, its not passed as props. If you add ref property it will give you the reference of NumberFormat instance. In case you need input reference. You can use getInputRef prop instead. In case you have provided custom input you can pass there props to get the input reference (getInputRef will not work in that case).

What is the use of numberformat ref In react?

As ref is a special property in react, its not passed as props. If you add ref property it will give you the reference of NumberFormat instance. In case you need input reference.


3 Answers

    <input
        type="text"
        value={this.props.rate}
        onChange={this.onAmountChange}
      />

type should be text and input value should be defined by regex.

onAmountChange = e => {
    const amount = e.target.value;

    if (!amount || amount.match(/^\d{1,}(\.\d{0,4})?$/)) {
      this.setState(() => ({ amount }));
    }
  };

regex here means: start with a number and add as many as you want. then optionally end with decimal numbers up to 4 decimals.

like image 86
Yilmaz Avatar answered Sep 17 '22 18:09

Yilmaz


You can do something like this

const floatRegExp = new RegExp('^[+-]?([0-9]+([.][0-9]*)?|[.][0-9]+)$')

const handleValidationOnChange = (e, v, onChange) => {
  const { value } = v
  if (value === '' || floatRegExp.test(value)) {
    onChange(e, v)
  }
}

const InputFloat = props => {
  if (typeof props.onChange !== 'function') {
    return <Form.Input { ...props } />
  }

  const { onChange, ...parentProps } = props

  return <Form.Input
    { ...parentProps }
    onChange={(e, v) => handleValidationOnChange(e, v, onChange)}
  />
}

Form.Input can be any Component that has a value.

You will have to later check for '', that is unavoidable.

like image 30
David Polák Avatar answered Sep 20 '22 18:09

David Polák


<input type="number"
      title="Rate"
      id="rate"
      className="form-control"
      value={this.props.rate}
      min="0.00"
      step="0.001"
      max="1.00"
      presicion={2}   //very important
      onChange={()=>{
        console.log('page rate changed');
        this.props.setrate($('#rate').val());
      }} />
like image 44
Gabriel Pérez Carballo Avatar answered Sep 19 '22 18:09

Gabriel Pérez Carballo