|
| 1 | +# react-input-numeric |
| 2 | + |
| 3 | +[](https://github.com/smeuli/react-input-numeric/blob/master/LICENSE) |
| 4 | +[](https://github.com/smeuli/react-input-numeric/pulls) |
| 5 | + |
| 6 | +**Better number input for React** |
| 7 | + |
| 8 | +* Simple to configure |
| 9 | +* Customizable |
| 10 | +* Same look across browsers |
| 11 | +* Touch screen friendly |
| 12 | + |
| 13 | +→ **[Demo](https://samuelmeuli.github.io/react-input-numeric)** |
| 14 | + |
| 15 | +<img src="demo.gif" alt="Demo" width="200"> |
| 16 | + |
| 17 | + |
| 18 | +## Usage |
| 19 | + |
| 20 | +Install the package using NPM: |
| 21 | + |
| 22 | +``` |
| 23 | +npm install react-input-numeric |
| 24 | +``` |
| 25 | + |
| 26 | +Add the component to your React application: |
| 27 | + |
| 28 | +```jsx |
| 29 | +import InputNumeric from 'react-input-numeric'; |
| 30 | + |
| 31 | +// Somewhere in your code: |
| 32 | +<InputNumeric |
| 33 | + value={this.state.value} |
| 34 | + onChange={value => this.setState({ value })} |
| 35 | +/> |
| 36 | +``` |
| 37 | + |
| 38 | + |
| 39 | +## Full example |
| 40 | + |
| 41 | +```jsx |
| 42 | +import React, { Component } from 'react'; |
| 43 | +import InputNumeric from 'react-input-numeric'; |
| 44 | + |
| 45 | +export default class ExampleComponent extends Component { |
| 46 | + constructor() { |
| 47 | + super(); |
| 48 | + this.state = { |
| 49 | + value: 0 |
| 50 | + }; |
| 51 | + } |
| 52 | + |
| 53 | + render() { |
| 54 | + return ( |
| 55 | + <InputNumeric |
| 56 | + value={this.state.value} |
| 57 | + onChange={value => this.setState({ value })} |
| 58 | + /> |
| 59 | + ); |
| 60 | + } |
| 61 | +} |
| 62 | +``` |
| 63 | + |
| 64 | + |
| 65 | +## Props |
| 66 | + |
| 67 | +Prop | Type | Default | Description |
| 68 | +---- | ---- | ------- | ----------- |
| 69 | +`value` (required) | Number | – | Value currently displayed in the input field |
| 70 | +`max` | Number | – | Maximal value |
| 71 | +`min` | Number | – | Minimal value |
| 72 | +`decimals` | Number (integer) | `2` | Number of digits after the decimal point |
| 73 | +`step` | Number | `1` | Difference between the possible values (e.g. `1` only allows integers) |
| 74 | +`name` | String | – | HTML `name` attribute to be assigned to the input field |
| 75 | +`disabled` | Boolean | `false` | Input should not be editable and buttons not clickable |
| 76 | +`showButtons` | Boolean | `true` | Show/hide buttons for incrementing/decrementing |
| 77 | +`showTrailingZeros` | Boolean | `false` | Display e.g. `4.00` instead of `4` (with `decimals={2}`) |
| 78 | +`snapToStep` | Boolean | `false` | Snap manually entered values to the nearest multiple of `step` |
| 79 | +`onBlur` | Function | – | Function to be executed when the component loses focus (current value is passed as parameter) |
| 80 | +`onChange` | Function | – | Function to be executed when the value is changed (new value is passed as parameter) |
| 81 | +`onFocus` | Function | – | Function to be executed when the input field gets focus (current value is passed as parameter) |
| 82 | + |
| 83 | + |
| 84 | +## More information |
| 85 | + |
| 86 | +* The value can also be incremented/decremented using the up/down arrow keys |
| 87 | +* 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. |
| 88 | +* The component uses [decimal.js](https://github.com/MikeMcl/decimal.js/) internally for handling arbitrary-precision decimals |
| 89 | + |
| 90 | + |
| 91 | +## Development |
| 92 | + |
| 93 | +* `git clone` |
| 94 | +* `npm install` |
| 95 | +* `npm start` to generate the library bundle using [Rollup](https://github.com/rollup/rollup) |
| 96 | +* Open `localhost:3000` to see the component in action using [Storybook](https://github.com/storybooks/storybook) |
0 commit comments