react-number-format
React component to format number in an input or as a text
Features
- Allow prefix, suffix and thousand separator.
- Allow format pattern.
- Allow masking.
- Allow custom formatting handler.
- Allow formatting a input or a simple text
Install
Through npm
npm install react-number-format --save
Or get compiled development and production version from ./dist
Props
Props | Options | Default | Description |
---|---|---|---|
thousandSeparator | mixed: single character string or true/false (boolean) | false | Add thousand separators on number |
decimalSeparator | mixed: single character string or true/false (boolean) | . | Support decimal point on a number |
decimalPrecision | mixed: number or boolean | false (2 if true) | If false it does not limit decimal place, if true default precision is 2 or else limits to provided decimal place |
prefix | String (ex : $) | none | Add a prefix before the number |
suffix | String (ex : /-) | none | Add a prefix after the number |
value | Number | null | Value to number format |
displayType | String: text / input | input | If input it renders a input element where formatting happens as you input characters. If text it renders it as a normal text in a span formatting the given value |
format | String : Hash based ex (#### #### #### ####) Or Function |
none | If format given as hash string allow number input inplace of hash. If format given as function, component calls the function with unformatted number and expects formatted number. |
mask | String (ex : _) | none | If mask defined, component will show non entered placed with masked value. |
customInput | Component Reference | input | This allow supporting custom inputs with number format. |
onChange | (e, value) => {} | none | onChange handler accepts event object through which you can get formattedValue (e.targe.value # $2,223) and second parameter non formatted value (ie: 2223) |
Other than this it accepts all the props which can be given to a input or span based on displayType you selected.
Examples
Prefix and thousand separator : Format currency as text
var NumberFormat = ; <NumberFormat = = = = />
Output : $2,456,981
Format with pattern : Format credit card as text
<NumberFormat = = ="#### #### #### ####" />
Output : 4111 1111 1111 1111
Prefix and thousand separator : Format currency in input
<NumberFormat = = />
Maintaining change value on state
<NumberFormat = = = =/>
Format with pattern : Format credit card in an input
<NumberFormat ="#### #### #### ####" />
Format with mask : Format credit card in an input
<NumberFormat ="#### #### #### ####" ="_"/>
Custom format method : Format credit card expiry time
{ ifval && > 1 val = '0'+val; ifval && vallength >1 && > 12 val = '12'+val; val = val+ vallength > 2 ? '/'+val : ''; return val; } <NumberFormat =/>
Custom Inputs
You can easily extend your custom input with number format. But custom input should have all input props.
import TextField from 'material-ui/TextField';
<NumberFormat = ="#### #### #### ####"/>
Passing custom input props All custom input props and number input props are passed together.
<NumberFormat ="Some placeholder" = = ="#### #### #### ####"/>
Live Demo
http://codepen.io/s-yadav/pen/bpKNMa
Major Updates
v1.1.0
- Support custom input
- Support custom decimal / thousandSeparator
- Support providing decimal precision
- Bug fixes (See release notes)
v1.0.0
- Support decimals
- Support changing thousandSeparator to ','
- Updated complete code to ES6
- Added propTypes validation
- Fixed #1, #7, #8, #9
Development
- Download the zip
npm install
npm start
to run example servernpm run test
to test changesnpm run bundle
to bundle files
Testing
Test cases are written in jasmine and run by karma
Test file : /test/test_input.js
To run test : npm run test