1.0.1 • Published 6 years ago

react-input-numeric v1.0.1

Weekly downloads
23
License
MIT
Repository
github
Last release
6 years ago

react-input-numeric

license PRs welcome

Better number input for React

  • Simple to configure
  • Customizable
  • Same look across browsers
  • Touch screen friendly

Demo

Usage

Install the package using NPM:

npm install react-input-numeric

Add the component to your React application:

import InputNumeric from 'react-input-numeric';

// Somewhere in your code:
<InputNumeric
  value={this.state.value}
  onChange={value => this.setState({ value })}
/>

Full example

import React, { Component } from 'react';
import InputNumeric from 'react-input-numeric';

export default class ExampleComponent extends Component {
  constructor() {
    super();
    this.state = {
      value: 0
    };
  }

  render() {
    return (
      <InputNumeric
        value={this.state.value}
        onChange={value => this.setState({ value })}
      />
    );
  }
}

Props

PropTypeDefaultDescription
value (required)NumberValue currently displayed in the input field
maxNumberMaximal value
minNumberMinimal value
decimalsNumber (integer)2Number of digits after the decimal point
stepNumber1Difference between the possible values (e.g. 1 only allows integers)
nameStringHTML name attribute to be assigned to the input field
disabledBooleanfalseInput should not be editable and buttons not clickable
showButtonsBooleantrueShow/hide buttons for incrementing/decrementing
showTrailingZerosBooleanfalseDisplay e.g. 4.00 instead of 4 (with decimals={2})
snapToStepBooleanfalseSnap manually entered values to the nearest multiple of step
onBlurFunctionFunction to be executed when the component loses focus (current value is passed as parameter)
onChangeFunctionFunction to be executed when the value is changed (new value is passed as parameter)
onFocusFunctionFunction to be executed when the input field gets focus (current value is passed as parameter)

More information

  • The value can also be incremented/decremented using the up/down arrow keys
  • When setting the decimals prop, the exact value is always stored and passed to the onBlur, onChange, and onFocus functions. Only the value displayed to the user is rounded.
  • The component uses decimal.js internally for handling arbitrary-precision decimals

Development

  • git clone
  • npm install
  • npm start to generate the library bundle using Rollup
  • Open localhost:3000 to see the component in action using Storybook