Package Exports
- react-number-format
- react-number-format/dist/react-number-format
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (react-number-format) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
react-number-format
React component to format number in an input or as a text
Features
- Allow prefix, suffix and thousand seperator.
- 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 |
---|---|---|---|
thousandSeperator | Boolean: true/false | false | Add thousand seperators on number |
prefix | String (ex : $) | none | Add a prefix before the number |
suffix | String (ex : /-) | none | Add a prefix after the number |
value | Number | null | Give initial 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. |
Examples
Prefix and thousand seperator : Format currency as text
var NumberFormat = require('react-number-format');
<NumberFormat value={2456981} displayType={'text'} thousandSeperator={true} prefix={'$'} />
Output : $2,456,981
Format with pattern : Format credit card as text
<NumberFormat value={4111111111111111} displayType={'text'} format="#### #### #### ####" />
Output : 4111 1111 1111 1111
Prefix and thousand seperator : Format currency in input
<NumberFormat thousandSeperator={true} prefix={'$'} />
Format with pattern : Format credit card in an input
<NumberFormat format="#### #### #### ####" />
Format with mask : Format credit card in an input
<NumberFormat format="#### #### #### ####" mask="_"/>
Custom format method : Format credit card expiry time
function formatExpiryChange(val){
if(val && Number(val[0]) > 1){
val = '0'+val;
}
if(val && val.length >1 && Number(val[0]+val[1]) > 12){
val = '12'+val.substring(2,val.length);
}
val = val.substring(0,2)+ (val.length > 2 ? '/'+val.substring(2,4) : '');
return val;
}
<NumberFormat format={formatExpiryChange}/>
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