Skip to content

Commit

Permalink
Release for #345 - Add allowedDecimalSeparators prop
Browse files Browse the repository at this point in the history
  • Loading branch information
s-yadav committed Oct 1, 2019
1 parent 3fe3806 commit 1f2df39
Show file tree
Hide file tree
Showing 6 changed files with 211 additions and 182 deletions.
127 changes: 68 additions & 59 deletions dist/react-number-format.es.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* react-number-format - 4.2.0
* react-number-format - 4.3.0
* Author : Sudhanshu Yadav
* Copyright (c) 2016, 2019 to Sudhanshu Yadav, released under the MIT license.
* https://github.com/s-yadav/react-number-format
Expand Down Expand Up @@ -350,6 +350,7 @@ function getCurrentCaretPosition(el) {
var propTypes$1 = {
thousandSeparator: propTypes.oneOfType([propTypes.string, propTypes.oneOf([true])]),
decimalSeparator: propTypes.string,
allowedDecimalSeparators: propTypes.arrayOf(propTypes.string),
thousandsGroupStyle: propTypes.oneOf(['thousand', 'lakh', 'wan']),
decimalScale: propTypes.number,
fixedDecimalScale: propTypes.bool,
Expand Down Expand Up @@ -517,15 +518,22 @@ function (_React$Component) {
key: "getSeparators",
value: function getSeparators() {
var decimalSeparator = this.props.decimalSeparator;
var thousandSeparator = this.props.thousandSeparator;
var _this$props2 = this.props,
thousandSeparator = _this$props2.thousandSeparator,
allowedDecimalSeparators = _this$props2.allowedDecimalSeparators;

if (thousandSeparator === true) {
thousandSeparator = ',';
}

if (!allowedDecimalSeparators) {
allowedDecimalSeparators = [decimalSeparator, '.'];
}

return {
decimalSeparator: decimalSeparator,
thousandSeparator: thousandSeparator
thousandSeparator: thousandSeparator,
allowedDecimalSeparators: allowedDecimalSeparators
};
}
}, {
Expand Down Expand Up @@ -592,10 +600,10 @@ function (_React$Component) {
}, {
key: "correctCaretPosition",
value: function correctCaretPosition(value, caretPos, direction) {
var _this$props2 = this.props,
prefix = _this$props2.prefix,
suffix = _this$props2.suffix,
format = _this$props2.format; //if value is empty return 0
var _this$props3 = this.props,
prefix = _this$props3.prefix,
suffix = _this$props3.suffix,
format = _this$props3.format; //if value is empty return 0

if (value === '') return 0; //caret position should be between 0 and value length

Expand Down Expand Up @@ -681,10 +689,10 @@ function (_React$Component) {
}, {
key: "removePrefixAndSuffix",
value: function removePrefixAndSuffix(val) {
var _this$props3 = this.props,
format = _this$props3.format,
prefix = _this$props3.prefix,
suffix = _this$props3.suffix; //remove prefix and suffix
var _this$props4 = this.props,
format = _this$props4.format,
prefix = _this$props4.prefix,
suffix = _this$props4.suffix; //remove prefix and suffix

if (!format && val) {
var isNegative = val[0] === '-'; //remove negation sign
Expand Down Expand Up @@ -734,9 +742,9 @@ function (_React$Component) {
}, {
key: "removeFormatting",
value: function removeFormatting(val) {
var _this$props4 = this.props,
format = _this$props4.format,
removeFormatting = _this$props4.removeFormatting;
var _this$props5 = this.props,
format = _this$props5.format,
removeFormatting = _this$props5.removeFormatting;
if (!val) return val;

if (!format) {
Expand Down Expand Up @@ -787,13 +795,13 @@ function (_React$Component) {
}, {
key: "formatAsNumber",
value: function formatAsNumber(numStr) {
var _this$props5 = this.props,
decimalScale = _this$props5.decimalScale,
fixedDecimalScale = _this$props5.fixedDecimalScale,
prefix = _this$props5.prefix,
suffix = _this$props5.suffix,
allowNegative = _this$props5.allowNegative,
thousandsGroupStyle = _this$props5.thousandsGroupStyle;
var _this$props6 = this.props,
decimalScale = _this$props6.decimalScale,
fixedDecimalScale = _this$props6.fixedDecimalScale,
prefix = _this$props6.prefix,
suffix = _this$props6.suffix,
allowNegative = _this$props6.allowNegative,
thousandsGroupStyle = _this$props6.thousandsGroupStyle;

var _this$getSeparators4 = this.getSeparators(),
thousandSeparator = _this$getSeparators4.thousandSeparator,
Expand Down Expand Up @@ -826,9 +834,9 @@ function (_React$Component) {
key: "formatNumString",
value: function formatNumString() {
var numStr = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
var _this$props6 = this.props,
format = _this$props6.format,
allowEmptyFormatting = _this$props6.allowEmptyFormatting;
var _this$props7 = this.props,
format = _this$props7.format,
allowEmptyFormatting = _this$props7.allowEmptyFormatting;
var formattedValue = numStr;

if (numStr === '' && !allowEmptyFormatting) {
Expand All @@ -848,15 +856,15 @@ function (_React$Component) {
}, {
key: "formatValueProp",
value: function formatValueProp(defaultValue) {
var _this$props7 = this.props,
format = _this$props7.format,
decimalScale = _this$props7.decimalScale,
fixedDecimalScale = _this$props7.fixedDecimalScale,
allowEmptyFormatting = _this$props7.allowEmptyFormatting;
var _this$props8 = this.props,
_this$props8$value = _this$props8.value,
value = _this$props8$value === void 0 ? defaultValue : _this$props8$value,
isNumericString = _this$props8.isNumericString;
format = _this$props8.format,
decimalScale = _this$props8.decimalScale,
fixedDecimalScale = _this$props8.fixedDecimalScale,
allowEmptyFormatting = _this$props8.allowEmptyFormatting;
var _this$props9 = this.props,
_this$props9$value = _this$props9.value,
value = _this$props9$value === void 0 ? defaultValue : _this$props9$value,
isNumericString = _this$props9.isNumericString;
var isNonNumericFalsy = !value && value !== 0;

if (isNonNumericFalsy && allowEmptyFormatting) {
Expand Down Expand Up @@ -925,12 +933,12 @@ function (_React$Component) {
}, {
key: "isCharacterAFormat",
value: function isCharacterAFormat(caretPos, value) {
var _this$props9 = this.props,
format = _this$props9.format,
prefix = _this$props9.prefix,
suffix = _this$props9.suffix,
decimalScale = _this$props9.decimalScale,
fixedDecimalScale = _this$props9.fixedDecimalScale;
var _this$props10 = this.props,
format = _this$props10.format,
prefix = _this$props10.prefix,
suffix = _this$props10.suffix,
decimalScale = _this$props10.decimalScale,
fixedDecimalScale = _this$props10.fixedDecimalScale;

var _this$getSeparators5 = this.getSeparators(),
decimalSeparator = _this$getSeparators5.decimalSeparator; //check within format pattern
Expand Down Expand Up @@ -961,13 +969,14 @@ function (_React$Component) {
}, {
key: "correctInputValue",
value: function correctInputValue(caretPos, lastValue, value) {
var _this$props10 = this.props,
format = _this$props10.format,
allowNegative = _this$props10.allowNegative,
prefix = _this$props10.prefix,
suffix = _this$props10.suffix;
var _this$props11 = this.props,
format = _this$props11.format,
allowNegative = _this$props11.allowNegative,
prefix = _this$props11.prefix,
suffix = _this$props11.suffix;

var _this$getSeparators6 = this.getSeparators(),
allowedDecimalSeparators = _this$getSeparators6.allowedDecimalSeparators,
decimalSeparator = _this$getSeparators6.decimalSeparator;

var lastNumStr = this.state.numAsString || '';
Expand All @@ -978,10 +987,10 @@ function (_React$Component) {
var _findChangedIndex = findChangedIndex(lastValue, value),
start = _findChangedIndex.start,
end = _findChangedIndex.end;
/** Check if only . is added in the numeric format and replace it with decimal separator */
/** Check for any allowed decimal separator is added in the numeric format and replace it with decimal separator */


if (!format && decimalSeparator !== '.' && start === end && value[selectionStart] === '.') {
if (!format && start === end && allowedDecimalSeparators.indexOf(value[selectionStart]) !== -1) {
return value.substr(0, selectionStart) + decimalSeparator + value.substr(selectionStart + 1, value.length);
}
/* don't do anyhting if something got added,
Expand Down Expand Up @@ -1144,14 +1153,14 @@ function (_React$Component) {
_el$value = el.value,
value = _el$value === void 0 ? '' : _el$value;
var expectedCaretPosition;
var _this$props11 = this.props,
decimalScale = _this$props11.decimalScale,
fixedDecimalScale = _this$props11.fixedDecimalScale,
prefix = _this$props11.prefix,
suffix = _this$props11.suffix,
format = _this$props11.format,
onKeyDown = _this$props11.onKeyDown,
onValueChange = _this$props11.onValueChange;
var _this$props12 = this.props,
decimalScale = _this$props12.decimalScale,
fixedDecimalScale = _this$props12.fixedDecimalScale,
prefix = _this$props12.prefix,
suffix = _this$props12.suffix,
format = _this$props12.format,
onKeyDown = _this$props12.onKeyDown,
onValueChange = _this$props12.onValueChange;
var ignoreDecimalSeparator = decimalScale !== undefined && fixedDecimalScale;
var numRegex = this.getNumberRegex(false, ignoreDecimalSeparator);
var negativeRegex = new RegExp('-');
Expand Down Expand Up @@ -1280,12 +1289,12 @@ function (_React$Component) {
}, {
key: "render",
value: function render() {
var _this$props12 = this.props,
type = _this$props12.type,
displayType = _this$props12.displayType,
customInput = _this$props12.customInput,
renderText = _this$props12.renderText,
getInputRef = _this$props12.getInputRef;
var _this$props13 = this.props,
type = _this$props13.type,
displayType = _this$props13.displayType,
customInput = _this$props13.customInput,
renderText = _this$props13.renderText,
getInputRef = _this$props13.getInputRef;
var value = this.state.value;
var otherProps = omit(this.props, propTypes$1);

Expand Down
Loading

0 comments on commit 1f2df39

Please sign in to comment.