react-currency-input
Advanced tools
Comparing version 1.0.5 to 1.0.6
@@ -35,3 +35,4 @@ 'use strict'; | ||
precision: _react.PropTypes.oneOfType([_react.PropTypes.number, _react.PropTypes.string]), | ||
inputType: _react.PropTypes.string | ||
inputType: _react.PropTypes.string, | ||
allowNegative: _react.PropTypes.bool | ||
}, | ||
@@ -45,3 +46,3 @@ | ||
* | ||
* @returns {{onChange: onChange, value: string, decimalSeparator: string, thousandSeparator: string, precision: number}} | ||
* @returns {{onChange: onChange, value: string, decimalSeparator: string, thousandSeparator: string, precision: number, inputType: string, allowNegative: boolean}} | ||
*/ | ||
@@ -55,3 +56,4 @@ getDefaultProps: function getDefaultProps() { | ||
precision: "2", | ||
inputType: "text" | ||
inputType: "text", | ||
allowNegative: false | ||
}; | ||
@@ -76,4 +78,5 @@ }, | ||
delete customProps.inputType; | ||
delete customProps.allowNegative; | ||
return { | ||
maskedValue: (0, _mask2.default)(this.props.value, this.props.precision, this.props.decimalSeparator, this.props.thousandSeparator), | ||
maskedValue: (0, _mask2.default)(this.props.value, this.props.precision, this.props.decimalSeparator, this.props.thousandSeparator, this.props.allowNegative), | ||
customProps: customProps | ||
@@ -99,4 +102,5 @@ }; | ||
delete customProps.inputType; | ||
delete customProps.allowNegative; | ||
this.setState({ | ||
maskedValue: (0, _mask2.default)(nextProps.value, nextProps.precision, nextProps.decimalSeparator, nextProps.thousandSeparator), | ||
maskedValue: (0, _mask2.default)(nextProps.value, nextProps.precision, nextProps.decimalSeparator, nextProps.thousandSeparator, nextProps.allowNegative), | ||
customProps: customProps | ||
@@ -123,3 +127,3 @@ }); | ||
event.preventDefault(); | ||
var maskedValue = (0, _mask2.default)(event.target.value, this.props.precision, this.props.decimalSeparator, this.props.thousandSeparator); | ||
var maskedValue = (0, _mask2.default)(event.target.value, this.props.precision, this.props.decimalSeparator, this.props.thousandSeparator, this.props.allowNegative); | ||
this.setState({ maskedValue: maskedValue }); | ||
@@ -126,0 +130,0 @@ this.props.onChange(maskedValue); |
@@ -7,3 +7,3 @@ "use strict"; | ||
exports.default = mask; | ||
function mask(value, precision, decimalSeparator, thousandSeparator) { | ||
function mask(value, precision, decimalSeparator, thousandSeparator, allowNegative) { | ||
// provide some default values and arg validation. | ||
@@ -16,2 +16,5 @@ if (decimalSeparator === undefined) { | ||
} // default to ',' as thousand separator | ||
if (allowNegative === undefined) { | ||
allowNegative = false; | ||
} // default to not allowing negative numbers | ||
if (precision === undefined) { | ||
@@ -27,5 +30,29 @@ precision = 2; | ||
var numberIsNegative = false; | ||
if (allowNegative) { | ||
var negativeSignCount = (value.match(/-/g) || []).length; | ||
// number will be negative if we have an odd number of "-" | ||
// ideally, we should only ever have 0, 1 or 2 (positive number, making a number negative | ||
// and making a negative number positive, respectively) | ||
numberIsNegative = negativeSignCount % 2 === 1; | ||
} | ||
// extract digits. if no digits, fill in a zero. | ||
var digits = value.match(/\d/g) || ['0']; | ||
if (allowNegative) { | ||
// if every digit in the array is '0', then the number should | ||
// never be negative | ||
var allDigitsAreZero = true; | ||
for (var idx = 0; idx < digits.length; idx += 1) { | ||
if (digits[idx] !== '0') { | ||
allDigitsAreZero = false; | ||
break; | ||
} | ||
} | ||
if (allDigitsAreZero) { | ||
numberIsNegative = false; | ||
} | ||
} | ||
// zero-pad a input | ||
@@ -58,3 +85,9 @@ while (digits.length <= precision) { | ||
// if the number is negative, insert a "-" to | ||
// the front of the array | ||
if (allowNegative && numberIsNegative) { | ||
digits.unshift('-'); | ||
} | ||
return digits.join(''); | ||
} |
{ | ||
"name": "react-currency-input", | ||
"version": "1.0.5", | ||
"version": "1.0.6", | ||
"description": "React component for inputing currency amounts", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -104,3 +104,4 @@ # react-currency-input | ||
| inputType | "text" | Input field tag type. You may want to use `number` or `tel` | | ||
| allowNegative | false | Allows negative numbers in the input | | ||
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
12865
195
107