Security News
Research
Data Theft Repackaged: A Case Study in Malicious Wrapper Packages on npm
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
react-currency-format
Advanced tools
React component to format currency in an input or as a text.
React component to format number in an input or as a text
Through npm
npm install react-currency-format --save
Or get compiled development and production version from ./dist
ES6
import CurrencyFormat from 'react-currency-format';
ES5
const CurrencyFormat = require('react-currency-format');
Typescript
import * as CurrencyFormat from 'react-currency-format';
Props | Options | Default | Description |
---|---|---|---|
thousandSeparator | mixed: single character string or boolean true (true is default to ,) | none | Add thousand separators on number |
thousandSpacing | String, One of ['2', '2s', '3', '4'] | '3' | Add thousand group spacing on number. Default: '2' will format like 1,23,45,67,89 __ '3' will format like 1,234,567,981 __ '2s' will format like 1,23,45,67,981 __ '4' will format like 1,2345,6789 |
decimalSeparator | single character string | . | Support decimal point on a number |
decimalScale | number | none | If defined it limits to given decimal scale |
fixedDecimalScale | boolean | false | If true it add 0s to match given decimalScale |
allowNegative | boolean | true | allow negative numbers (Only when format option is not provided) |
prefix | String (ex : $) | none | Add a prefix before the number |
suffix | String (ex : /-) | none | Add a prefix after the number |
value | Number or String | null | Value to the number format. It can be a float number, or formatted string. If value is string representation of number (unformatted), isNumericString props should be passed as true. |
isNumericString | boolean | false | If value is passed as string representation of numbers (unformatted) then this should be passed as true |
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 |
type | One of ['text', 'tel'] | text | Input type attribute |
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. |
removeFormatting | (formattedValue) => numericString | none | If you are providing custom format method and it add numbers as format you will need to add custom removeFormatting logic |
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. |
onValueChange | (values) => {} | none | onValueChange handler accepts values object |
isAllowed | (values) => true or false | none | A checker function to check if input value is valid or not |
renderText | (formattedValue) => React Element | null | A renderText method useful if you want to render formattedValue in different element other than span. |
Other than this it accepts all the props which can be given to a input or span based on displayType you selected.
values object is on following format
{
formattedValue: '$23,234,235.56', //value after applying formatting
value: '23234235.56', //non formatted value as numeric string 23234235.56, if your are setting this value to state make sure to pass isNumericString prop to true
floatValue: 23234235.56 //floating point representation. For big numbers it can have exponential syntax
}
Its recommended to use formattedValue / value / floatValue based on the initial state (it should be same as the initial state format) which you are passing as value prop. If you are saving the value
key on state make sure to pass isNumericString prop to true.
Value can be passed as string or number, but if it is passed as string it should be either formatted value or if it is a numeric string, you have to set isNumericString props to true.
Value as prop will be rounded to given decimal scale if format option is not provided.
If you want to block floating number set decimalScale to 0.
Use type as tel when you are providing format prop. This will change the mobile keyboard layout to have only numbers. In other case use type as text, so user can type decimal separator.
onChange no longer gets values object. You need to use onValueChange instead. onChange/onFocus/onBlur and other input events will be directly passed to the input.
Its recommended to use formattedValue / value / floatValue based on the initial state (it should be same as the initial state format) which you are passing as value prop. If you are saving the value
key on state make sure to pass isNumericString prop to true.
var CurrencyFormat = require('react-currency-format');
<CurrencyFormat value={2456981} displayType={'text'} thousandSeparator={true} prefix={'$'} />
Output : $2,456,981
var CurrencyFormat = require('react-currency-format');
<CurrencyFormat value={2456981} displayType={'text'} thousandSeparator={true} prefix={'$'} renderText={value => <div>{value}</div>} />
Output : <div> $2,456,981 </div>
<CurrencyFormat value={4111111111111111} displayType={'text'} format="#### #### #### ####" />
Output : 4111 1111 1111 1111
<CurrencyFormat thousandSeparator={true} prefix={'$'} />
<CurrencyFormat value={this.state.profit} thousandSeparator={true} prefix={'$'} onValueChange={(values) => {
const {formattedValue, value} = values;
// formattedValue = $2,223
// value ie, 2223
this.setState({profit: formattedValue})
}}/>
<CurrencyFormat format="#### #### #### ####" />
<CurrencyFormat format="#### #### #### ####" mask="_"/>
Mask can also be a array of string. Each item corresponds to the same index #.
<CurrencyFormat format="##/##" placeholder="MM/YY" mask={['M', 'M', 'Y', 'Y']}/>
function limit(val, max) {
if (val.length === 1 && val[0] > max[0]) {
val = '0' + val;
}
if (val.length === 2) {
if (Number(val) === 0) {
val = '01';
//this can happen when user paste number
} else if (val > max) {
val = max;
}
}
return val;
}
function cardExpiry(val) {
let month = limit(val.substring(0, 2), '12');
let year = val.substring(2, 4);
return month + (year.length ? '/' + year : '');
}
<CurrencyFormat format={cardExpiry}/>
<CurrencyFormat format="+1 (###) ###-####" mask="_"/>
You can easily extend your custom input with number format. But custom input should have all input props.
import TextField from 'material-ui/TextField';
<CurrencyFormat customInput={TextField} format="#### #### #### ####"/>
Passing custom input props All custom input props and number input props are passed together.
<CurrencyFormat hintText="Some placeholder" value={this.state.card} customInput={TextField} format="#### #### #### ####"/>
http://codepen.io/mohitgupta8888/pen/bpKNMa
For regular updates follow me on _syadav
npm install
npm start
to run example servernpm run test
to test changesnpm run bundle
to bundle filesTest cases are written in jasmine and run by karma
Test file : /test/test_input.js
To run test : npm run test
FAQs
React component to format currency in an input or as a text.
We found that react-currency-format demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Research
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
Research
Security News
Attackers used a malicious npm package typosquatting a popular ESLint plugin to steal sensitive data, execute commands, and exploit developer systems.
Security News
The Ultralytics' PyPI Package was compromised four times in one weekend through GitHub Actions cache poisoning and failure to rotate previously compromised API tokens.