react-number-format
Advanced tools
Comparing version 4.5.5 to 4.6.0
/** | ||
* react-number-format - 4.5.5 | ||
* react-number-format - 4.6.0 | ||
* Author : Sudhanshu Yadav | ||
@@ -189,6 +189,6 @@ * Copyright (c) 2016, 2021 to Sudhanshu Yadav, released under the MIT license. | ||
function repeat(str, count) { | ||
return Array(count + 1).join(str) | ||
return Array(count + 1).join(str); | ||
} | ||
function toNumericString(num) { | ||
function toNumericString(num) { | ||
num += ''; // typecast number to string | ||
@@ -223,3 +223,3 @@ | ||
// if decimal index is less then 0 add preceding 0s | ||
// add 1 as join will have | ||
// add 1 as join will have | ||
coefficient = '0.' + repeat('0', Math.abs(decimalIndex)) + coefficient; | ||
@@ -231,3 +231,4 @@ } else if (decimalIndex >= coffiecientLn) { | ||
// else add decimal point at proper index | ||
coefficient = (coefficient.substring(0, decimalIndex) || '0') + '.' + coefficient.substring(decimalIndex); | ||
coefficient = | ||
(coefficient.substring(0, decimalIndex) || '0') + '.' + coefficient.substring(decimalIndex); | ||
} | ||
@@ -355,3 +356,2 @@ | ||
var propTypes$1 = { | ||
@@ -367,16 +367,7 @@ thousandSeparator: propTypes.oneOfType([propTypes.string, propTypes.oneOf([true])]), | ||
suffix: propTypes.string, | ||
format: propTypes.oneOfType([ | ||
propTypes.string, | ||
propTypes.func | ||
]), | ||
format: propTypes.oneOfType([propTypes.string, propTypes.func]), | ||
removeFormatting: propTypes.func, | ||
mask: propTypes.oneOfType([propTypes.string, propTypes.arrayOf(propTypes.string)]), | ||
value: propTypes.oneOfType([ | ||
propTypes.number, | ||
propTypes.string | ||
]), | ||
defaultValue: propTypes.oneOfType([ | ||
propTypes.number, | ||
propTypes.string | ||
]), | ||
value: propTypes.oneOfType([propTypes.number, propTypes.string]), | ||
defaultValue: propTypes.oneOfType([propTypes.number, propTypes.string]), | ||
isNumericString: propTypes.bool, | ||
@@ -398,4 +389,3 @@ customInput: propTypes.elementType, | ||
propTypes.func, // for legacy refs | ||
propTypes.shape({ current: propTypes.any }) | ||
]) | ||
propTypes.shape({ current: propTypes.any }) ]), | ||
}; | ||
@@ -421,3 +411,3 @@ | ||
onBlur: noop, | ||
isAllowed: returnTrue | ||
isAllowed: returnTrue, | ||
}; | ||
@@ -443,3 +433,3 @@ var NumberFormat = /*@__PURE__*/(function (superclass) { | ||
selectionStart: 0, | ||
selectionEnd: 0 | ||
selectionEnd: 0, | ||
}; | ||
@@ -462,3 +452,3 @@ | ||
this.setState({ | ||
mounted: true | ||
mounted: true, | ||
}); | ||
@@ -484,3 +474,3 @@ }; | ||
// If only state changed no need to do any thing | ||
if(prevProps !== props) { | ||
if (prevProps !== props) { | ||
//validate props | ||
@@ -522,3 +512,3 @@ this.validateProps(); | ||
var hasNegation = num[0] === '-'; | ||
if(hasNegation) { num = num.replace('-', ''); } | ||
if (hasNegation) { num = num.replace('-', ''); } | ||
@@ -530,3 +520,3 @@ //if decimal scale is zero remove decimal and number after decimalSeparator | ||
num = (num.match(numRegex) || []).join('').replace(decimalSeparator, '.'); | ||
num = (num.match(numRegex) || []).join('').replace(decimalSeparator, '.'); | ||
@@ -537,7 +527,9 @@ //remove extra decimals | ||
if (firstDecimalIndex !== -1) { | ||
num = (num.substring(0, firstDecimalIndex)) + "." + (num.substring(firstDecimalIndex + 1, num.length).replace(new RegExp(escapeRegExp(decimalSeparator), 'g'), '')); | ||
num = (num.substring(0, firstDecimalIndex)) + "." + (num | ||
.substring(firstDecimalIndex + 1, num.length) | ||
.replace(new RegExp(escapeRegExp(decimalSeparator), 'g'), '')); | ||
} | ||
//add negation back | ||
if(hasNegation) { num = '-' + num; } | ||
if (hasNegation) { num = '-' + num; } | ||
@@ -554,3 +546,9 @@ return num; | ||
var decimalSeparator = ref$1.decimalSeparator; | ||
return new RegExp('\\d' + (decimalSeparator && decimalScale !== 0 && !ignoreDecimalSeparator && !format ? '|' + escapeRegExp(decimalSeparator) : ''), g ? 'g' : undefined); | ||
return new RegExp( | ||
'\\d' + | ||
(decimalSeparator && decimalScale !== 0 && !ignoreDecimalSeparator && !format | ||
? '|' + escapeRegExp(decimalSeparator) | ||
: ''), | ||
g ? 'g' : undefined | ||
); | ||
}; | ||
@@ -576,3 +574,3 @@ | ||
allowedDecimalSeparators: allowedDecimalSeparators, | ||
} | ||
}; | ||
}; | ||
@@ -596,5 +594,4 @@ | ||
value: numAsString, | ||
floatValue: isNaN(floatValue) ? undefined : floatValue | ||
floatValue: isNaN(floatValue) ? undefined : floatValue, | ||
}; | ||
}; | ||
@@ -619,6 +616,5 @@ | ||
if (maskAsStr.match(/\d/g)) { | ||
throw new Error(("\n Mask " + mask + " should not contain numeric character;\n ")) | ||
throw new Error(("\n Mask " + mask + " should not contain numeric character;\n ")); | ||
} | ||
} | ||
}; | ||
@@ -634,3 +630,3 @@ /** Misc methods end **/ | ||
setTimeout(function () { | ||
if(el.value === currentValue) { setCaretPosition(el, caretPos); } | ||
if (el.value === currentValue) { setCaretPosition(el, caretPos); } | ||
}, 0); | ||
@@ -664,6 +660,10 @@ }; | ||
//in case the caretPos have input value on it don't do anything | ||
if (format[caretPos] === '#' && charIsNumber(value[caretPos])) { return caretPos; } | ||
if (format[caretPos] === '#' && charIsNumber(value[caretPos])) { | ||
return caretPos; | ||
} | ||
//if caretPos is just after input value don't do anything | ||
if (format[caretPos - 1] === '#' && charIsNumber(value[caretPos - 1])) { return caretPos; } | ||
if (format[caretPos - 1] === '#' && charIsNumber(value[caretPos - 1])) { | ||
return caretPos; | ||
} | ||
@@ -682,9 +682,13 @@ //find the nearest caret position | ||
//get the position where the last number is present | ||
while (caretLeftBound > firstHashPosition && (format[caretLeftBound] !== '#' || !charIsNumber(value[caretLeftBound]))) { | ||
while ( | ||
caretLeftBound > firstHashPosition && | ||
(format[caretLeftBound] !== '#' || !charIsNumber(value[caretLeftBound])) | ||
) { | ||
caretLeftBound -= 1; | ||
} | ||
var goToLeft = !charIsNumber(value[caretRightBound]) | ||
|| (direction === 'left' && caretPos !== firstHashPosition) | ||
|| (caretPos - caretLeftBound < caretRightBound - caretPos); | ||
var goToLeft = | ||
!charIsNumber(value[caretRightBound]) || | ||
(direction === 'left' && caretPos !== firstHashPosition) || | ||
caretPos - caretLeftBound < caretRightBound - caretPos; | ||
@@ -711,3 +715,3 @@ if (goToLeft) { | ||
for(i=0; i<caretPos; i++){ | ||
for (i = 0; i < caretPos; i++) { | ||
var currentInputChar = inputValue[i] || ''; | ||
@@ -717,14 +721,25 @@ var currentFormatChar = formattedValue[j] || ''; | ||
//case inputValue = 1a23 and formattedValue = 123 | ||
if(!currentInputChar.match(numRegex) && currentInputChar !== currentFormatChar) { continue; } | ||
if (!currentInputChar.match(numRegex) && currentInputChar !== currentFormatChar) { | ||
continue; | ||
} | ||
//When we are striping out leading zeros maintain the new cursor position | ||
//Case inputValue = 00023 and formattedValue = 23; | ||
if (currentInputChar === '0' && currentFormatChar.match(numRegex) && currentFormatChar !== '0' && inputNumber.length !== formattedNumber.length) { continue; } | ||
if ( | ||
currentInputChar === '0' && | ||
currentFormatChar.match(numRegex) && | ||
currentFormatChar !== '0' && | ||
inputNumber.length !== formattedNumber.length | ||
) { | ||
continue; | ||
} | ||
//we are not using currentFormatChar because j can change here | ||
while(currentInputChar !== formattedValue[j] && j < formattedValue.length) { j++; } | ||
while (currentInputChar !== formattedValue[j] && j < formattedValue.length) { | ||
j++; | ||
} | ||
j++; | ||
} | ||
if ((typeof format === 'string' && !stateValue)) { | ||
if (typeof format === 'string' && !stateValue) { | ||
//set it to the maximum value so it goes after the last number | ||
@@ -741,3 +756,2 @@ j = formattedValue.length; | ||
/** methods to remove formattting **/ | ||
@@ -762,3 +776,6 @@ NumberFormat.prototype.removePrefixAndSuffix = function removePrefixAndSuffix (val ) { | ||
var suffixLastIndex = val.lastIndexOf(suffix); | ||
val = suffix && suffixLastIndex !== -1 && suffixLastIndex === val.length - suffix.length ? val.substring(0, suffixLastIndex) : val; | ||
val = | ||
suffix && suffixLastIndex !== -1 && suffixLastIndex === val.length - suffix.length | ||
? val.substring(0, suffixLastIndex) | ||
: val; | ||
@@ -779,3 +796,3 @@ //add negation sign back | ||
for (var i=0, ln=formatArray.length; i <= ln; i++) { | ||
for (var i = 0, ln = formatArray.length; i <= ln; i++) { | ||
var part = formatArray[i] || ''; | ||
@@ -813,3 +830,4 @@ | ||
val = this.removePatternFormatting(val); | ||
} else if (typeof removeFormatting === 'function') { //condition need to be handled if format method is provide, | ||
} else if (typeof removeFormatting === 'function') { | ||
//condition need to be handled if format method is provide, | ||
val = removeFormatting(val); | ||
@@ -823,3 +841,2 @@ } else { | ||
/*** format specific methods start ***/ | ||
@@ -867,5 +884,7 @@ /** | ||
//apply decimal precision if its defined | ||
if (decimalScale !== undefined) { afterDecimal = limitToScale(afterDecimal, decimalScale, fixedDecimalScale); } | ||
if (decimalScale !== undefined) { | ||
afterDecimal = limitToScale(afterDecimal, decimalScale, fixedDecimalScale); | ||
} | ||
if(thousandSeparator) { | ||
if (thousandSeparator) { | ||
beforeDecimal = applyThousandSeparator(beforeDecimal, thousandSeparator, thousandsGroupStyle); | ||
@@ -875,4 +894,4 @@ } | ||
//add prefix and suffix | ||
if(prefix) { beforeDecimal = prefix + beforeDecimal; } | ||
if(suffix) { afterDecimal = afterDecimal + suffix; } | ||
if (prefix) { beforeDecimal = prefix + beforeDecimal; } | ||
if (suffix) { afterDecimal = afterDecimal + suffix; } | ||
@@ -882,3 +901,3 @@ //restore negation sign | ||
numStr = beforeDecimal + (hasDecimalSeparator && decimalSeparator || '') + afterDecimal; | ||
numStr = beforeDecimal + ((hasDecimalSeparator && decimalSeparator) || '') + afterDecimal; | ||
@@ -911,3 +930,3 @@ return numStr; | ||
NumberFormat.prototype.formatValueProp = function formatValueProp (defaultValue ) { | ||
NumberFormat.prototype.formatValueProp = function formatValueProp (defaultValue ) { | ||
var ref = this.props; | ||
@@ -1012,5 +1031,7 @@ var format = ref.format; | ||
//check in number format | ||
if (!format && (caretPos < prefix.length | ||
|| caretPos >= value.length - suffix.length | ||
|| (decimalScale && fixedDecimalScale && value[caretPos] === decimalSeparator)) | ||
if ( | ||
!format && | ||
(caretPos < prefix.length || | ||
caretPos >= value.length - suffix.length || | ||
(decimalScale && fixedDecimalScale && value[caretPos] === decimalSeparator)) | ||
) { | ||
@@ -1053,8 +1074,13 @@ return true; | ||
/** Check for any allowed decimal separator is added in the numeric format and replace it with decimal separator */ | ||
if (!format && start === end && allowedDecimalSeparators.indexOf(value[selectionStart]) !== -1 ) { | ||
if ( | ||
!format && | ||
start === end && | ||
allowedDecimalSeparators.indexOf(value[selectionStart]) !== -1 | ||
) { | ||
var separator = decimalScale === 0 ? '' : decimalSeparator; | ||
return value.substr(0, selectionStart) + separator + value.substr(selectionStart + 1, value.length); | ||
return ( | ||
value.substr(0, selectionStart) + separator + value.substr(selectionStart + 1, value.length) | ||
); | ||
} | ||
var leftBound = !!format ? 0 : prefix.length; | ||
@@ -1066,3 +1092,3 @@ var rightBound = lastValue.length - (!!format ? 0 : suffix.length); | ||
value.length > lastValue.length || | ||
// or if the new value is an empty string | ||
// or if the new value is an empty string | ||
!value.length || | ||
@@ -1075,3 +1101,3 @@ // or if nothing has changed, in which case start will be same as end | ||
(start === 0 && end === lastValue.length) || | ||
// or if charcters between prefix and suffix is selected. | ||
// or if charcters between prefix and suffix is selected. | ||
// For numeric inputs we apply the format so, prefix and suffix can be ignored | ||
@@ -1092,3 +1118,6 @@ (selectionStart === leftBound && selectionEnd === rightBound) | ||
var numericString = this.removeFormatting(value); | ||
var ref$4 = splitDecimal(numericString, allowNegative); | ||
var ref$4 = splitDecimal( | ||
numericString, | ||
allowNegative | ||
); | ||
var beforeDecimal = ref$4.beforeDecimal; | ||
@@ -1100,3 +1129,8 @@ var afterDecimal = ref$4.afterDecimal; | ||
var isBeforeDecimalPoint = caretPos < value.indexOf(decimalSeparator) + 1; | ||
if (numericString.length < lastNumStr.length && isBeforeDecimalPoint && beforeDecimal === '' && !parseFloat(afterDecimal)) { | ||
if ( | ||
numericString.length < lastNumStr.length && | ||
isBeforeDecimalPoint && | ||
beforeDecimal === '' && | ||
!parseFloat(afterDecimal) | ||
) { | ||
return addNegation ? '-' : ''; | ||
@@ -1111,10 +1145,9 @@ } | ||
NumberFormat.prototype.updateValue = function updateValue (params | ||
) { | ||
) { | ||
var formattedValue = params.formattedValue; | ||
@@ -1133,3 +1166,2 @@ var input = params.input; | ||
if (setCaretPosition) { | ||
//calculate caret position if not defined | ||
@@ -1163,3 +1195,2 @@ if (!caretPos) { | ||
//calculate numeric string if not passed | ||
@@ -1172,3 +1203,3 @@ if (numAsString === undefined) { | ||
if (formattedValue !== lastValue) { | ||
this.setState({ value : formattedValue, numAsString: numAsString }); | ||
this.setState({ value: formattedValue, numAsString: numAsString }); | ||
@@ -1191,3 +1222,3 @@ // trigger onValueChange synchronously, so parent is updated along with the number format. Fix for #277, #287 | ||
inputValue = this.correctInputValue(currentCaretPosition, lastValue, inputValue); | ||
inputValue = this.correctInputValue(currentCaretPosition, lastValue, inputValue); | ||
@@ -1206,3 +1237,3 @@ var formattedValue = this.formatInput(inputValue) || ''; | ||
if(isChangeAllowed) { | ||
if (isChangeAllowed) { | ||
props.onChange(e); | ||
@@ -1225,3 +1256,2 @@ } | ||
if (!format) { | ||
@@ -1242,3 +1272,8 @@ // if the numAsString is not a valid number reset it to empty | ||
// the event needs to be persisted because its properties can be accessed in an asynchronous way | ||
this.updateValue({ formattedValue: formattedValue, numAsString: numAsString, input: e.target, setCaretPosition: false }); | ||
this.updateValue({ | ||
formattedValue: formattedValue, | ||
numAsString: numAsString, | ||
input: e.target, | ||
setCaretPosition: false, | ||
}); | ||
onBlur(e); | ||
@@ -1272,3 +1307,3 @@ return; | ||
selectionStart: selectionStart, | ||
selectionEnd: selectionEnd | ||
selectionEnd: selectionEnd, | ||
}; | ||
@@ -1299,4 +1334,10 @@ | ||
newCaretPosition = this.correctCaretPosition(value, expectedCaretPosition, direction); | ||
} else if (key === 'Delete' && !numRegex.test(value[expectedCaretPosition]) && !negativeRegex.test(value[expectedCaretPosition])) { | ||
while (!numRegex.test(value[newCaretPosition]) && newCaretPosition < rightBound) { newCaretPosition++; } | ||
} else if ( | ||
key === 'Delete' && | ||
!numRegex.test(value[expectedCaretPosition]) && | ||
!negativeRegex.test(value[expectedCaretPosition]) | ||
) { | ||
while (!numRegex.test(value[newCaretPosition]) && newCaretPosition < rightBound) { | ||
newCaretPosition++; | ||
} | ||
} else if (key === 'Backspace' && !numRegex.test(value[expectedCaretPosition])) { | ||
@@ -1309,5 +1350,11 @@ /* NOTE: This is special case when backspace is pressed on a | ||
var newValue = value.substring(1); | ||
this.updateValue({formattedValue: newValue, caretPos: newCaretPosition, input: el}); | ||
this.updateValue({ | ||
formattedValue: newValue, | ||
caretPos: newCaretPosition, | ||
input: el, | ||
}); | ||
} else if (!negativeRegex.test(value[expectedCaretPosition])) { | ||
while (!numRegex.test(value[newCaretPosition - 1]) && newCaretPosition > leftBound){ newCaretPosition--; } | ||
while (!numRegex.test(value[newCaretPosition - 1]) && newCaretPosition > leftBound) { | ||
newCaretPosition--; | ||
} | ||
newCaretPosition = this.correctCaretPosition(value, newCaretPosition, 'left'); | ||
@@ -1317,4 +1364,7 @@ } | ||
if (newCaretPosition !== expectedCaretPosition || expectedCaretPosition < leftBound || expectedCaretPosition > rightBound) { | ||
if ( | ||
newCaretPosition !== expectedCaretPosition || | ||
expectedCaretPosition < leftBound || | ||
expectedCaretPosition > rightBound | ||
) { | ||
e.preventDefault(); | ||
@@ -1330,5 +1380,3 @@ this.setPatchedCaretPosition(el, newCaretPosition, value); | ||
onKeyDown(e); | ||
}; | ||
@@ -1343,3 +1391,3 @@ | ||
* value can come as undefined when nothing is provided on value prop. | ||
*/ | ||
*/ | ||
var selectionStart = el.selectionStart; | ||
@@ -1376,3 +1424,6 @@ var selectionEnd = el.selectionEnd; | ||
//setPatchedCaretPosition only when everything is not selected on focus (while tabbing into the field) | ||
if (caretPosition !== selectionStart && !(selectionStart === 0 && selectionEnd === value.length)) { | ||
if ( | ||
caretPosition !== selectionStart && | ||
!(selectionStart === 0 && selectionEnd === value.length) | ||
) { | ||
this$1.setPatchedCaretPosition(el, caretPosition, value); | ||
@@ -1399,3 +1450,3 @@ } | ||
// add input mode on element based on format prop and device once the component is mounted | ||
// add input mode on element based on format prop and device once the component is mounted | ||
var inputMode = mounted && addInputMode(format) ? 'numeric' : undefined; | ||
@@ -1410,21 +1461,19 @@ | ||
onFocus: this.onFocus, | ||
onBlur: this.onBlur | ||
onBlur: this.onBlur, | ||
}); | ||
if( displayType === 'text'){ | ||
return renderText ? (renderText(value, otherProps) || null) : React.createElement( 'span', Object.assign({}, otherProps, { ref: getInputRef }), value); | ||
} | ||
else if (customInput) { | ||
if (displayType === 'text') { | ||
return renderText ? ( | ||
renderText(value, otherProps) || null | ||
) : ( | ||
React.createElement( 'span', Object.assign({}, otherProps, { ref: getInputRef }), | ||
value | ||
) | ||
); | ||
} else if (customInput) { | ||
var CustomInput = customInput; | ||
return ( | ||
React.createElement( CustomInput, Object.assign({}, | ||
inputProps, { ref: getInputRef })) | ||
) | ||
return React.createElement( CustomInput, Object.assign({}, inputProps, { ref: getInputRef })); | ||
} | ||
return ( | ||
React.createElement( 'input', Object.assign({}, | ||
inputProps, { ref: getInputRef })) | ||
) | ||
return React.createElement( 'input', Object.assign({}, inputProps, { ref: getInputRef })); | ||
}; | ||
@@ -1431,0 +1480,0 @@ |
/** | ||
* react-number-format - 4.5.5 | ||
* react-number-format - 4.6.0 | ||
* Author : Sudhanshu Yadav | ||
@@ -185,6 +185,6 @@ * Copyright (c) 2016, 2021 to Sudhanshu Yadav, released under the MIT license. | ||
function repeat(str, count) { | ||
return Array(count + 1).join(str) | ||
return Array(count + 1).join(str); | ||
} | ||
function toNumericString(num) { | ||
function toNumericString(num) { | ||
num += ''; // typecast number to string | ||
@@ -219,3 +219,3 @@ | ||
// if decimal index is less then 0 add preceding 0s | ||
// add 1 as join will have | ||
// add 1 as join will have | ||
coefficient = '0.' + repeat('0', Math.abs(decimalIndex)) + coefficient; | ||
@@ -227,3 +227,4 @@ } else if (decimalIndex >= coffiecientLn) { | ||
// else add decimal point at proper index | ||
coefficient = (coefficient.substring(0, decimalIndex) || '0') + '.' + coefficient.substring(decimalIndex); | ||
coefficient = | ||
(coefficient.substring(0, decimalIndex) || '0') + '.' + coefficient.substring(decimalIndex); | ||
} | ||
@@ -351,3 +352,2 @@ | ||
var propTypes$1 = { | ||
@@ -363,16 +363,7 @@ thousandSeparator: propTypes.oneOfType([propTypes.string, propTypes.oneOf([true])]), | ||
suffix: propTypes.string, | ||
format: propTypes.oneOfType([ | ||
propTypes.string, | ||
propTypes.func | ||
]), | ||
format: propTypes.oneOfType([propTypes.string, propTypes.func]), | ||
removeFormatting: propTypes.func, | ||
mask: propTypes.oneOfType([propTypes.string, propTypes.arrayOf(propTypes.string)]), | ||
value: propTypes.oneOfType([ | ||
propTypes.number, | ||
propTypes.string | ||
]), | ||
defaultValue: propTypes.oneOfType([ | ||
propTypes.number, | ||
propTypes.string | ||
]), | ||
value: propTypes.oneOfType([propTypes.number, propTypes.string]), | ||
defaultValue: propTypes.oneOfType([propTypes.number, propTypes.string]), | ||
isNumericString: propTypes.bool, | ||
@@ -394,4 +385,3 @@ customInput: propTypes.elementType, | ||
propTypes.func, // for legacy refs | ||
propTypes.shape({ current: propTypes.any }) | ||
]) | ||
propTypes.shape({ current: propTypes.any }) ]), | ||
}; | ||
@@ -417,3 +407,3 @@ | ||
onBlur: noop, | ||
isAllowed: returnTrue | ||
isAllowed: returnTrue, | ||
}; | ||
@@ -439,3 +429,3 @@ var NumberFormat = /*@__PURE__*/(function (superclass) { | ||
selectionStart: 0, | ||
selectionEnd: 0 | ||
selectionEnd: 0, | ||
}; | ||
@@ -458,3 +448,3 @@ | ||
this.setState({ | ||
mounted: true | ||
mounted: true, | ||
}); | ||
@@ -480,3 +470,3 @@ }; | ||
// If only state changed no need to do any thing | ||
if(prevProps !== props) { | ||
if (prevProps !== props) { | ||
//validate props | ||
@@ -518,3 +508,3 @@ this.validateProps(); | ||
var hasNegation = num[0] === '-'; | ||
if(hasNegation) { num = num.replace('-', ''); } | ||
if (hasNegation) { num = num.replace('-', ''); } | ||
@@ -526,3 +516,3 @@ //if decimal scale is zero remove decimal and number after decimalSeparator | ||
num = (num.match(numRegex) || []).join('').replace(decimalSeparator, '.'); | ||
num = (num.match(numRegex) || []).join('').replace(decimalSeparator, '.'); | ||
@@ -533,7 +523,9 @@ //remove extra decimals | ||
if (firstDecimalIndex !== -1) { | ||
num = (num.substring(0, firstDecimalIndex)) + "." + (num.substring(firstDecimalIndex + 1, num.length).replace(new RegExp(escapeRegExp(decimalSeparator), 'g'), '')); | ||
num = (num.substring(0, firstDecimalIndex)) + "." + (num | ||
.substring(firstDecimalIndex + 1, num.length) | ||
.replace(new RegExp(escapeRegExp(decimalSeparator), 'g'), '')); | ||
} | ||
//add negation back | ||
if(hasNegation) { num = '-' + num; } | ||
if (hasNegation) { num = '-' + num; } | ||
@@ -550,3 +542,9 @@ return num; | ||
var decimalSeparator = ref$1.decimalSeparator; | ||
return new RegExp('\\d' + (decimalSeparator && decimalScale !== 0 && !ignoreDecimalSeparator && !format ? '|' + escapeRegExp(decimalSeparator) : ''), g ? 'g' : undefined); | ||
return new RegExp( | ||
'\\d' + | ||
(decimalSeparator && decimalScale !== 0 && !ignoreDecimalSeparator && !format | ||
? '|' + escapeRegExp(decimalSeparator) | ||
: ''), | ||
g ? 'g' : undefined | ||
); | ||
}; | ||
@@ -572,3 +570,3 @@ | ||
allowedDecimalSeparators: allowedDecimalSeparators, | ||
} | ||
}; | ||
}; | ||
@@ -592,5 +590,4 @@ | ||
value: numAsString, | ||
floatValue: isNaN(floatValue) ? undefined : floatValue | ||
floatValue: isNaN(floatValue) ? undefined : floatValue, | ||
}; | ||
}; | ||
@@ -615,6 +612,5 @@ | ||
if (maskAsStr.match(/\d/g)) { | ||
throw new Error(("\n Mask " + mask + " should not contain numeric character;\n ")) | ||
throw new Error(("\n Mask " + mask + " should not contain numeric character;\n ")); | ||
} | ||
} | ||
}; | ||
@@ -630,3 +626,3 @@ /** Misc methods end **/ | ||
setTimeout(function () { | ||
if(el.value === currentValue) { setCaretPosition(el, caretPos); } | ||
if (el.value === currentValue) { setCaretPosition(el, caretPos); } | ||
}, 0); | ||
@@ -660,6 +656,10 @@ }; | ||
//in case the caretPos have input value on it don't do anything | ||
if (format[caretPos] === '#' && charIsNumber(value[caretPos])) { return caretPos; } | ||
if (format[caretPos] === '#' && charIsNumber(value[caretPos])) { | ||
return caretPos; | ||
} | ||
//if caretPos is just after input value don't do anything | ||
if (format[caretPos - 1] === '#' && charIsNumber(value[caretPos - 1])) { return caretPos; } | ||
if (format[caretPos - 1] === '#' && charIsNumber(value[caretPos - 1])) { | ||
return caretPos; | ||
} | ||
@@ -678,9 +678,13 @@ //find the nearest caret position | ||
//get the position where the last number is present | ||
while (caretLeftBound > firstHashPosition && (format[caretLeftBound] !== '#' || !charIsNumber(value[caretLeftBound]))) { | ||
while ( | ||
caretLeftBound > firstHashPosition && | ||
(format[caretLeftBound] !== '#' || !charIsNumber(value[caretLeftBound])) | ||
) { | ||
caretLeftBound -= 1; | ||
} | ||
var goToLeft = !charIsNumber(value[caretRightBound]) | ||
|| (direction === 'left' && caretPos !== firstHashPosition) | ||
|| (caretPos - caretLeftBound < caretRightBound - caretPos); | ||
var goToLeft = | ||
!charIsNumber(value[caretRightBound]) || | ||
(direction === 'left' && caretPos !== firstHashPosition) || | ||
caretPos - caretLeftBound < caretRightBound - caretPos; | ||
@@ -707,3 +711,3 @@ if (goToLeft) { | ||
for(i=0; i<caretPos; i++){ | ||
for (i = 0; i < caretPos; i++) { | ||
var currentInputChar = inputValue[i] || ''; | ||
@@ -713,14 +717,25 @@ var currentFormatChar = formattedValue[j] || ''; | ||
//case inputValue = 1a23 and formattedValue = 123 | ||
if(!currentInputChar.match(numRegex) && currentInputChar !== currentFormatChar) { continue; } | ||
if (!currentInputChar.match(numRegex) && currentInputChar !== currentFormatChar) { | ||
continue; | ||
} | ||
//When we are striping out leading zeros maintain the new cursor position | ||
//Case inputValue = 00023 and formattedValue = 23; | ||
if (currentInputChar === '0' && currentFormatChar.match(numRegex) && currentFormatChar !== '0' && inputNumber.length !== formattedNumber.length) { continue; } | ||
if ( | ||
currentInputChar === '0' && | ||
currentFormatChar.match(numRegex) && | ||
currentFormatChar !== '0' && | ||
inputNumber.length !== formattedNumber.length | ||
) { | ||
continue; | ||
} | ||
//we are not using currentFormatChar because j can change here | ||
while(currentInputChar !== formattedValue[j] && j < formattedValue.length) { j++; } | ||
while (currentInputChar !== formattedValue[j] && j < formattedValue.length) { | ||
j++; | ||
} | ||
j++; | ||
} | ||
if ((typeof format === 'string' && !stateValue)) { | ||
if (typeof format === 'string' && !stateValue) { | ||
//set it to the maximum value so it goes after the last number | ||
@@ -737,3 +752,2 @@ j = formattedValue.length; | ||
/** methods to remove formattting **/ | ||
@@ -758,3 +772,6 @@ NumberFormat.prototype.removePrefixAndSuffix = function removePrefixAndSuffix (val ) { | ||
var suffixLastIndex = val.lastIndexOf(suffix); | ||
val = suffix && suffixLastIndex !== -1 && suffixLastIndex === val.length - suffix.length ? val.substring(0, suffixLastIndex) : val; | ||
val = | ||
suffix && suffixLastIndex !== -1 && suffixLastIndex === val.length - suffix.length | ||
? val.substring(0, suffixLastIndex) | ||
: val; | ||
@@ -775,3 +792,3 @@ //add negation sign back | ||
for (var i=0, ln=formatArray.length; i <= ln; i++) { | ||
for (var i = 0, ln = formatArray.length; i <= ln; i++) { | ||
var part = formatArray[i] || ''; | ||
@@ -809,3 +826,4 @@ | ||
val = this.removePatternFormatting(val); | ||
} else if (typeof removeFormatting === 'function') { //condition need to be handled if format method is provide, | ||
} else if (typeof removeFormatting === 'function') { | ||
//condition need to be handled if format method is provide, | ||
val = removeFormatting(val); | ||
@@ -819,3 +837,2 @@ } else { | ||
/*** format specific methods start ***/ | ||
@@ -863,5 +880,7 @@ /** | ||
//apply decimal precision if its defined | ||
if (decimalScale !== undefined) { afterDecimal = limitToScale(afterDecimal, decimalScale, fixedDecimalScale); } | ||
if (decimalScale !== undefined) { | ||
afterDecimal = limitToScale(afterDecimal, decimalScale, fixedDecimalScale); | ||
} | ||
if(thousandSeparator) { | ||
if (thousandSeparator) { | ||
beforeDecimal = applyThousandSeparator(beforeDecimal, thousandSeparator, thousandsGroupStyle); | ||
@@ -871,4 +890,4 @@ } | ||
//add prefix and suffix | ||
if(prefix) { beforeDecimal = prefix + beforeDecimal; } | ||
if(suffix) { afterDecimal = afterDecimal + suffix; } | ||
if (prefix) { beforeDecimal = prefix + beforeDecimal; } | ||
if (suffix) { afterDecimal = afterDecimal + suffix; } | ||
@@ -878,3 +897,3 @@ //restore negation sign | ||
numStr = beforeDecimal + (hasDecimalSeparator && decimalSeparator || '') + afterDecimal; | ||
numStr = beforeDecimal + ((hasDecimalSeparator && decimalSeparator) || '') + afterDecimal; | ||
@@ -907,3 +926,3 @@ return numStr; | ||
NumberFormat.prototype.formatValueProp = function formatValueProp (defaultValue ) { | ||
NumberFormat.prototype.formatValueProp = function formatValueProp (defaultValue ) { | ||
var ref = this.props; | ||
@@ -1008,5 +1027,7 @@ var format = ref.format; | ||
//check in number format | ||
if (!format && (caretPos < prefix.length | ||
|| caretPos >= value.length - suffix.length | ||
|| (decimalScale && fixedDecimalScale && value[caretPos] === decimalSeparator)) | ||
if ( | ||
!format && | ||
(caretPos < prefix.length || | ||
caretPos >= value.length - suffix.length || | ||
(decimalScale && fixedDecimalScale && value[caretPos] === decimalSeparator)) | ||
) { | ||
@@ -1049,8 +1070,13 @@ return true; | ||
/** Check for any allowed decimal separator is added in the numeric format and replace it with decimal separator */ | ||
if (!format && start === end && allowedDecimalSeparators.indexOf(value[selectionStart]) !== -1 ) { | ||
if ( | ||
!format && | ||
start === end && | ||
allowedDecimalSeparators.indexOf(value[selectionStart]) !== -1 | ||
) { | ||
var separator = decimalScale === 0 ? '' : decimalSeparator; | ||
return value.substr(0, selectionStart) + separator + value.substr(selectionStart + 1, value.length); | ||
return ( | ||
value.substr(0, selectionStart) + separator + value.substr(selectionStart + 1, value.length) | ||
); | ||
} | ||
var leftBound = !!format ? 0 : prefix.length; | ||
@@ -1062,3 +1088,3 @@ var rightBound = lastValue.length - (!!format ? 0 : suffix.length); | ||
value.length > lastValue.length || | ||
// or if the new value is an empty string | ||
// or if the new value is an empty string | ||
!value.length || | ||
@@ -1071,3 +1097,3 @@ // or if nothing has changed, in which case start will be same as end | ||
(start === 0 && end === lastValue.length) || | ||
// or if charcters between prefix and suffix is selected. | ||
// or if charcters between prefix and suffix is selected. | ||
// For numeric inputs we apply the format so, prefix and suffix can be ignored | ||
@@ -1088,3 +1114,6 @@ (selectionStart === leftBound && selectionEnd === rightBound) | ||
var numericString = this.removeFormatting(value); | ||
var ref$4 = splitDecimal(numericString, allowNegative); | ||
var ref$4 = splitDecimal( | ||
numericString, | ||
allowNegative | ||
); | ||
var beforeDecimal = ref$4.beforeDecimal; | ||
@@ -1096,3 +1125,8 @@ var afterDecimal = ref$4.afterDecimal; | ||
var isBeforeDecimalPoint = caretPos < value.indexOf(decimalSeparator) + 1; | ||
if (numericString.length < lastNumStr.length && isBeforeDecimalPoint && beforeDecimal === '' && !parseFloat(afterDecimal)) { | ||
if ( | ||
numericString.length < lastNumStr.length && | ||
isBeforeDecimalPoint && | ||
beforeDecimal === '' && | ||
!parseFloat(afterDecimal) | ||
) { | ||
return addNegation ? '-' : ''; | ||
@@ -1107,10 +1141,9 @@ } | ||
NumberFormat.prototype.updateValue = function updateValue (params | ||
) { | ||
) { | ||
var formattedValue = params.formattedValue; | ||
@@ -1129,3 +1162,2 @@ var input = params.input; | ||
if (setCaretPosition) { | ||
//calculate caret position if not defined | ||
@@ -1159,3 +1191,2 @@ if (!caretPos) { | ||
//calculate numeric string if not passed | ||
@@ -1168,3 +1199,3 @@ if (numAsString === undefined) { | ||
if (formattedValue !== lastValue) { | ||
this.setState({ value : formattedValue, numAsString: numAsString }); | ||
this.setState({ value: formattedValue, numAsString: numAsString }); | ||
@@ -1187,3 +1218,3 @@ // trigger onValueChange synchronously, so parent is updated along with the number format. Fix for #277, #287 | ||
inputValue = this.correctInputValue(currentCaretPosition, lastValue, inputValue); | ||
inputValue = this.correctInputValue(currentCaretPosition, lastValue, inputValue); | ||
@@ -1202,3 +1233,3 @@ var formattedValue = this.formatInput(inputValue) || ''; | ||
if(isChangeAllowed) { | ||
if (isChangeAllowed) { | ||
props.onChange(e); | ||
@@ -1221,3 +1252,2 @@ } | ||
if (!format) { | ||
@@ -1238,3 +1268,8 @@ // if the numAsString is not a valid number reset it to empty | ||
// the event needs to be persisted because its properties can be accessed in an asynchronous way | ||
this.updateValue({ formattedValue: formattedValue, numAsString: numAsString, input: e.target, setCaretPosition: false }); | ||
this.updateValue({ | ||
formattedValue: formattedValue, | ||
numAsString: numAsString, | ||
input: e.target, | ||
setCaretPosition: false, | ||
}); | ||
onBlur(e); | ||
@@ -1268,3 +1303,3 @@ return; | ||
selectionStart: selectionStart, | ||
selectionEnd: selectionEnd | ||
selectionEnd: selectionEnd, | ||
}; | ||
@@ -1295,4 +1330,10 @@ | ||
newCaretPosition = this.correctCaretPosition(value, expectedCaretPosition, direction); | ||
} else if (key === 'Delete' && !numRegex.test(value[expectedCaretPosition]) && !negativeRegex.test(value[expectedCaretPosition])) { | ||
while (!numRegex.test(value[newCaretPosition]) && newCaretPosition < rightBound) { newCaretPosition++; } | ||
} else if ( | ||
key === 'Delete' && | ||
!numRegex.test(value[expectedCaretPosition]) && | ||
!negativeRegex.test(value[expectedCaretPosition]) | ||
) { | ||
while (!numRegex.test(value[newCaretPosition]) && newCaretPosition < rightBound) { | ||
newCaretPosition++; | ||
} | ||
} else if (key === 'Backspace' && !numRegex.test(value[expectedCaretPosition])) { | ||
@@ -1305,5 +1346,11 @@ /* NOTE: This is special case when backspace is pressed on a | ||
var newValue = value.substring(1); | ||
this.updateValue({formattedValue: newValue, caretPos: newCaretPosition, input: el}); | ||
this.updateValue({ | ||
formattedValue: newValue, | ||
caretPos: newCaretPosition, | ||
input: el, | ||
}); | ||
} else if (!negativeRegex.test(value[expectedCaretPosition])) { | ||
while (!numRegex.test(value[newCaretPosition - 1]) && newCaretPosition > leftBound){ newCaretPosition--; } | ||
while (!numRegex.test(value[newCaretPosition - 1]) && newCaretPosition > leftBound) { | ||
newCaretPosition--; | ||
} | ||
newCaretPosition = this.correctCaretPosition(value, newCaretPosition, 'left'); | ||
@@ -1313,4 +1360,7 @@ } | ||
if (newCaretPosition !== expectedCaretPosition || expectedCaretPosition < leftBound || expectedCaretPosition > rightBound) { | ||
if ( | ||
newCaretPosition !== expectedCaretPosition || | ||
expectedCaretPosition < leftBound || | ||
expectedCaretPosition > rightBound | ||
) { | ||
e.preventDefault(); | ||
@@ -1326,5 +1376,3 @@ this.setPatchedCaretPosition(el, newCaretPosition, value); | ||
onKeyDown(e); | ||
}; | ||
@@ -1339,3 +1387,3 @@ | ||
* value can come as undefined when nothing is provided on value prop. | ||
*/ | ||
*/ | ||
var selectionStart = el.selectionStart; | ||
@@ -1372,3 +1420,6 @@ var selectionEnd = el.selectionEnd; | ||
//setPatchedCaretPosition only when everything is not selected on focus (while tabbing into the field) | ||
if (caretPosition !== selectionStart && !(selectionStart === 0 && selectionEnd === value.length)) { | ||
if ( | ||
caretPosition !== selectionStart && | ||
!(selectionStart === 0 && selectionEnd === value.length) | ||
) { | ||
this$1.setPatchedCaretPosition(el, caretPosition, value); | ||
@@ -1395,3 +1446,3 @@ } | ||
// add input mode on element based on format prop and device once the component is mounted | ||
// add input mode on element based on format prop and device once the component is mounted | ||
var inputMode = mounted && addInputMode(format) ? 'numeric' : undefined; | ||
@@ -1406,21 +1457,19 @@ | ||
onFocus: this.onFocus, | ||
onBlur: this.onBlur | ||
onBlur: this.onBlur, | ||
}); | ||
if( displayType === 'text'){ | ||
return renderText ? (renderText(value, otherProps) || null) : React.createElement( 'span', Object.assign({}, otherProps, { ref: getInputRef }), value); | ||
} | ||
else if (customInput) { | ||
if (displayType === 'text') { | ||
return renderText ? ( | ||
renderText(value, otherProps) || null | ||
) : ( | ||
React.createElement( 'span', Object.assign({}, otherProps, { ref: getInputRef }), | ||
value | ||
) | ||
); | ||
} else if (customInput) { | ||
var CustomInput = customInput; | ||
return ( | ||
React.createElement( CustomInput, Object.assign({}, | ||
inputProps, { ref: getInputRef })) | ||
) | ||
return React.createElement( CustomInput, Object.assign({}, inputProps, { ref: getInputRef })); | ||
} | ||
return ( | ||
React.createElement( 'input', Object.assign({}, | ||
inputProps, { ref: getInputRef })) | ||
) | ||
return React.createElement( 'input', Object.assign({}, inputProps, { ref: getInputRef })); | ||
}; | ||
@@ -1427,0 +1476,0 @@ |
/** | ||
* react-number-format - 4.5.5 | ||
* react-number-format - 4.6.0 | ||
* Author : Sudhanshu Yadav | ||
@@ -191,6 +191,6 @@ * Copyright (c) 2016, 2021 to Sudhanshu Yadav, released under the MIT license. | ||
function repeat(str, count) { | ||
return Array(count + 1).join(str) | ||
return Array(count + 1).join(str); | ||
} | ||
function toNumericString(num) { | ||
function toNumericString(num) { | ||
num += ''; // typecast number to string | ||
@@ -225,3 +225,3 @@ | ||
// if decimal index is less then 0 add preceding 0s | ||
// add 1 as join will have | ||
// add 1 as join will have | ||
coefficient = '0.' + repeat('0', Math.abs(decimalIndex)) + coefficient; | ||
@@ -233,3 +233,4 @@ } else if (decimalIndex >= coffiecientLn) { | ||
// else add decimal point at proper index | ||
coefficient = (coefficient.substring(0, decimalIndex) || '0') + '.' + coefficient.substring(decimalIndex); | ||
coefficient = | ||
(coefficient.substring(0, decimalIndex) || '0') + '.' + coefficient.substring(decimalIndex); | ||
} | ||
@@ -357,3 +358,2 @@ | ||
var propTypes$1 = { | ||
@@ -369,16 +369,7 @@ thousandSeparator: propTypes.oneOfType([propTypes.string, propTypes.oneOf([true])]), | ||
suffix: propTypes.string, | ||
format: propTypes.oneOfType([ | ||
propTypes.string, | ||
propTypes.func | ||
]), | ||
format: propTypes.oneOfType([propTypes.string, propTypes.func]), | ||
removeFormatting: propTypes.func, | ||
mask: propTypes.oneOfType([propTypes.string, propTypes.arrayOf(propTypes.string)]), | ||
value: propTypes.oneOfType([ | ||
propTypes.number, | ||
propTypes.string | ||
]), | ||
defaultValue: propTypes.oneOfType([ | ||
propTypes.number, | ||
propTypes.string | ||
]), | ||
value: propTypes.oneOfType([propTypes.number, propTypes.string]), | ||
defaultValue: propTypes.oneOfType([propTypes.number, propTypes.string]), | ||
isNumericString: propTypes.bool, | ||
@@ -400,4 +391,3 @@ customInput: propTypes.elementType, | ||
propTypes.func, // for legacy refs | ||
propTypes.shape({ current: propTypes.any }) | ||
]) | ||
propTypes.shape({ current: propTypes.any }) ]), | ||
}; | ||
@@ -423,3 +413,3 @@ | ||
onBlur: noop, | ||
isAllowed: returnTrue | ||
isAllowed: returnTrue, | ||
}; | ||
@@ -445,3 +435,3 @@ var NumberFormat = /*@__PURE__*/(function (superclass) { | ||
selectionStart: 0, | ||
selectionEnd: 0 | ||
selectionEnd: 0, | ||
}; | ||
@@ -464,3 +454,3 @@ | ||
this.setState({ | ||
mounted: true | ||
mounted: true, | ||
}); | ||
@@ -486,3 +476,3 @@ }; | ||
// If only state changed no need to do any thing | ||
if(prevProps !== props) { | ||
if (prevProps !== props) { | ||
//validate props | ||
@@ -524,3 +514,3 @@ this.validateProps(); | ||
var hasNegation = num[0] === '-'; | ||
if(hasNegation) { num = num.replace('-', ''); } | ||
if (hasNegation) { num = num.replace('-', ''); } | ||
@@ -532,3 +522,3 @@ //if decimal scale is zero remove decimal and number after decimalSeparator | ||
num = (num.match(numRegex) || []).join('').replace(decimalSeparator, '.'); | ||
num = (num.match(numRegex) || []).join('').replace(decimalSeparator, '.'); | ||
@@ -539,7 +529,9 @@ //remove extra decimals | ||
if (firstDecimalIndex !== -1) { | ||
num = (num.substring(0, firstDecimalIndex)) + "." + (num.substring(firstDecimalIndex + 1, num.length).replace(new RegExp(escapeRegExp(decimalSeparator), 'g'), '')); | ||
num = (num.substring(0, firstDecimalIndex)) + "." + (num | ||
.substring(firstDecimalIndex + 1, num.length) | ||
.replace(new RegExp(escapeRegExp(decimalSeparator), 'g'), '')); | ||
} | ||
//add negation back | ||
if(hasNegation) { num = '-' + num; } | ||
if (hasNegation) { num = '-' + num; } | ||
@@ -556,3 +548,9 @@ return num; | ||
var decimalSeparator = ref$1.decimalSeparator; | ||
return new RegExp('\\d' + (decimalSeparator && decimalScale !== 0 && !ignoreDecimalSeparator && !format ? '|' + escapeRegExp(decimalSeparator) : ''), g ? 'g' : undefined); | ||
return new RegExp( | ||
'\\d' + | ||
(decimalSeparator && decimalScale !== 0 && !ignoreDecimalSeparator && !format | ||
? '|' + escapeRegExp(decimalSeparator) | ||
: ''), | ||
g ? 'g' : undefined | ||
); | ||
}; | ||
@@ -578,3 +576,3 @@ | ||
allowedDecimalSeparators: allowedDecimalSeparators, | ||
} | ||
}; | ||
}; | ||
@@ -598,5 +596,4 @@ | ||
value: numAsString, | ||
floatValue: isNaN(floatValue) ? undefined : floatValue | ||
floatValue: isNaN(floatValue) ? undefined : floatValue, | ||
}; | ||
}; | ||
@@ -621,6 +618,5 @@ | ||
if (maskAsStr.match(/\d/g)) { | ||
throw new Error(("\n Mask " + mask + " should not contain numeric character;\n ")) | ||
throw new Error(("\n Mask " + mask + " should not contain numeric character;\n ")); | ||
} | ||
} | ||
}; | ||
@@ -636,3 +632,3 @@ /** Misc methods end **/ | ||
setTimeout(function () { | ||
if(el.value === currentValue) { setCaretPosition(el, caretPos); } | ||
if (el.value === currentValue) { setCaretPosition(el, caretPos); } | ||
}, 0); | ||
@@ -666,6 +662,10 @@ }; | ||
//in case the caretPos have input value on it don't do anything | ||
if (format[caretPos] === '#' && charIsNumber(value[caretPos])) { return caretPos; } | ||
if (format[caretPos] === '#' && charIsNumber(value[caretPos])) { | ||
return caretPos; | ||
} | ||
//if caretPos is just after input value don't do anything | ||
if (format[caretPos - 1] === '#' && charIsNumber(value[caretPos - 1])) { return caretPos; } | ||
if (format[caretPos - 1] === '#' && charIsNumber(value[caretPos - 1])) { | ||
return caretPos; | ||
} | ||
@@ -684,9 +684,13 @@ //find the nearest caret position | ||
//get the position where the last number is present | ||
while (caretLeftBound > firstHashPosition && (format[caretLeftBound] !== '#' || !charIsNumber(value[caretLeftBound]))) { | ||
while ( | ||
caretLeftBound > firstHashPosition && | ||
(format[caretLeftBound] !== '#' || !charIsNumber(value[caretLeftBound])) | ||
) { | ||
caretLeftBound -= 1; | ||
} | ||
var goToLeft = !charIsNumber(value[caretRightBound]) | ||
|| (direction === 'left' && caretPos !== firstHashPosition) | ||
|| (caretPos - caretLeftBound < caretRightBound - caretPos); | ||
var goToLeft = | ||
!charIsNumber(value[caretRightBound]) || | ||
(direction === 'left' && caretPos !== firstHashPosition) || | ||
caretPos - caretLeftBound < caretRightBound - caretPos; | ||
@@ -713,3 +717,3 @@ if (goToLeft) { | ||
for(i=0; i<caretPos; i++){ | ||
for (i = 0; i < caretPos; i++) { | ||
var currentInputChar = inputValue[i] || ''; | ||
@@ -719,14 +723,25 @@ var currentFormatChar = formattedValue[j] || ''; | ||
//case inputValue = 1a23 and formattedValue = 123 | ||
if(!currentInputChar.match(numRegex) && currentInputChar !== currentFormatChar) { continue; } | ||
if (!currentInputChar.match(numRegex) && currentInputChar !== currentFormatChar) { | ||
continue; | ||
} | ||
//When we are striping out leading zeros maintain the new cursor position | ||
//Case inputValue = 00023 and formattedValue = 23; | ||
if (currentInputChar === '0' && currentFormatChar.match(numRegex) && currentFormatChar !== '0' && inputNumber.length !== formattedNumber.length) { continue; } | ||
if ( | ||
currentInputChar === '0' && | ||
currentFormatChar.match(numRegex) && | ||
currentFormatChar !== '0' && | ||
inputNumber.length !== formattedNumber.length | ||
) { | ||
continue; | ||
} | ||
//we are not using currentFormatChar because j can change here | ||
while(currentInputChar !== formattedValue[j] && j < formattedValue.length) { j++; } | ||
while (currentInputChar !== formattedValue[j] && j < formattedValue.length) { | ||
j++; | ||
} | ||
j++; | ||
} | ||
if ((typeof format === 'string' && !stateValue)) { | ||
if (typeof format === 'string' && !stateValue) { | ||
//set it to the maximum value so it goes after the last number | ||
@@ -743,3 +758,2 @@ j = formattedValue.length; | ||
/** methods to remove formattting **/ | ||
@@ -764,3 +778,6 @@ NumberFormat.prototype.removePrefixAndSuffix = function removePrefixAndSuffix (val ) { | ||
var suffixLastIndex = val.lastIndexOf(suffix); | ||
val = suffix && suffixLastIndex !== -1 && suffixLastIndex === val.length - suffix.length ? val.substring(0, suffixLastIndex) : val; | ||
val = | ||
suffix && suffixLastIndex !== -1 && suffixLastIndex === val.length - suffix.length | ||
? val.substring(0, suffixLastIndex) | ||
: val; | ||
@@ -781,3 +798,3 @@ //add negation sign back | ||
for (var i=0, ln=formatArray.length; i <= ln; i++) { | ||
for (var i = 0, ln = formatArray.length; i <= ln; i++) { | ||
var part = formatArray[i] || ''; | ||
@@ -815,3 +832,4 @@ | ||
val = this.removePatternFormatting(val); | ||
} else if (typeof removeFormatting === 'function') { //condition need to be handled if format method is provide, | ||
} else if (typeof removeFormatting === 'function') { | ||
//condition need to be handled if format method is provide, | ||
val = removeFormatting(val); | ||
@@ -825,3 +843,2 @@ } else { | ||
/*** format specific methods start ***/ | ||
@@ -869,5 +886,7 @@ /** | ||
//apply decimal precision if its defined | ||
if (decimalScale !== undefined) { afterDecimal = limitToScale(afterDecimal, decimalScale, fixedDecimalScale); } | ||
if (decimalScale !== undefined) { | ||
afterDecimal = limitToScale(afterDecimal, decimalScale, fixedDecimalScale); | ||
} | ||
if(thousandSeparator) { | ||
if (thousandSeparator) { | ||
beforeDecimal = applyThousandSeparator(beforeDecimal, thousandSeparator, thousandsGroupStyle); | ||
@@ -877,4 +896,4 @@ } | ||
//add prefix and suffix | ||
if(prefix) { beforeDecimal = prefix + beforeDecimal; } | ||
if(suffix) { afterDecimal = afterDecimal + suffix; } | ||
if (prefix) { beforeDecimal = prefix + beforeDecimal; } | ||
if (suffix) { afterDecimal = afterDecimal + suffix; } | ||
@@ -884,3 +903,3 @@ //restore negation sign | ||
numStr = beforeDecimal + (hasDecimalSeparator && decimalSeparator || '') + afterDecimal; | ||
numStr = beforeDecimal + ((hasDecimalSeparator && decimalSeparator) || '') + afterDecimal; | ||
@@ -913,3 +932,3 @@ return numStr; | ||
NumberFormat.prototype.formatValueProp = function formatValueProp (defaultValue ) { | ||
NumberFormat.prototype.formatValueProp = function formatValueProp (defaultValue ) { | ||
var ref = this.props; | ||
@@ -1014,5 +1033,7 @@ var format = ref.format; | ||
//check in number format | ||
if (!format && (caretPos < prefix.length | ||
|| caretPos >= value.length - suffix.length | ||
|| (decimalScale && fixedDecimalScale && value[caretPos] === decimalSeparator)) | ||
if ( | ||
!format && | ||
(caretPos < prefix.length || | ||
caretPos >= value.length - suffix.length || | ||
(decimalScale && fixedDecimalScale && value[caretPos] === decimalSeparator)) | ||
) { | ||
@@ -1055,8 +1076,13 @@ return true; | ||
/** Check for any allowed decimal separator is added in the numeric format and replace it with decimal separator */ | ||
if (!format && start === end && allowedDecimalSeparators.indexOf(value[selectionStart]) !== -1 ) { | ||
if ( | ||
!format && | ||
start === end && | ||
allowedDecimalSeparators.indexOf(value[selectionStart]) !== -1 | ||
) { | ||
var separator = decimalScale === 0 ? '' : decimalSeparator; | ||
return value.substr(0, selectionStart) + separator + value.substr(selectionStart + 1, value.length); | ||
return ( | ||
value.substr(0, selectionStart) + separator + value.substr(selectionStart + 1, value.length) | ||
); | ||
} | ||
var leftBound = !!format ? 0 : prefix.length; | ||
@@ -1068,3 +1094,3 @@ var rightBound = lastValue.length - (!!format ? 0 : suffix.length); | ||
value.length > lastValue.length || | ||
// or if the new value is an empty string | ||
// or if the new value is an empty string | ||
!value.length || | ||
@@ -1077,3 +1103,3 @@ // or if nothing has changed, in which case start will be same as end | ||
(start === 0 && end === lastValue.length) || | ||
// or if charcters between prefix and suffix is selected. | ||
// or if charcters between prefix and suffix is selected. | ||
// For numeric inputs we apply the format so, prefix and suffix can be ignored | ||
@@ -1094,3 +1120,6 @@ (selectionStart === leftBound && selectionEnd === rightBound) | ||
var numericString = this.removeFormatting(value); | ||
var ref$4 = splitDecimal(numericString, allowNegative); | ||
var ref$4 = splitDecimal( | ||
numericString, | ||
allowNegative | ||
); | ||
var beforeDecimal = ref$4.beforeDecimal; | ||
@@ -1102,3 +1131,8 @@ var afterDecimal = ref$4.afterDecimal; | ||
var isBeforeDecimalPoint = caretPos < value.indexOf(decimalSeparator) + 1; | ||
if (numericString.length < lastNumStr.length && isBeforeDecimalPoint && beforeDecimal === '' && !parseFloat(afterDecimal)) { | ||
if ( | ||
numericString.length < lastNumStr.length && | ||
isBeforeDecimalPoint && | ||
beforeDecimal === '' && | ||
!parseFloat(afterDecimal) | ||
) { | ||
return addNegation ? '-' : ''; | ||
@@ -1113,10 +1147,9 @@ } | ||
NumberFormat.prototype.updateValue = function updateValue (params | ||
) { | ||
) { | ||
var formattedValue = params.formattedValue; | ||
@@ -1135,3 +1168,2 @@ var input = params.input; | ||
if (setCaretPosition) { | ||
//calculate caret position if not defined | ||
@@ -1165,3 +1197,2 @@ if (!caretPos) { | ||
//calculate numeric string if not passed | ||
@@ -1174,3 +1205,3 @@ if (numAsString === undefined) { | ||
if (formattedValue !== lastValue) { | ||
this.setState({ value : formattedValue, numAsString: numAsString }); | ||
this.setState({ value: formattedValue, numAsString: numAsString }); | ||
@@ -1193,3 +1224,3 @@ // trigger onValueChange synchronously, so parent is updated along with the number format. Fix for #277, #287 | ||
inputValue = this.correctInputValue(currentCaretPosition, lastValue, inputValue); | ||
inputValue = this.correctInputValue(currentCaretPosition, lastValue, inputValue); | ||
@@ -1208,3 +1239,3 @@ var formattedValue = this.formatInput(inputValue) || ''; | ||
if(isChangeAllowed) { | ||
if (isChangeAllowed) { | ||
props.onChange(e); | ||
@@ -1227,3 +1258,2 @@ } | ||
if (!format) { | ||
@@ -1244,3 +1274,8 @@ // if the numAsString is not a valid number reset it to empty | ||
// the event needs to be persisted because its properties can be accessed in an asynchronous way | ||
this.updateValue({ formattedValue: formattedValue, numAsString: numAsString, input: e.target, setCaretPosition: false }); | ||
this.updateValue({ | ||
formattedValue: formattedValue, | ||
numAsString: numAsString, | ||
input: e.target, | ||
setCaretPosition: false, | ||
}); | ||
onBlur(e); | ||
@@ -1274,3 +1309,3 @@ return; | ||
selectionStart: selectionStart, | ||
selectionEnd: selectionEnd | ||
selectionEnd: selectionEnd, | ||
}; | ||
@@ -1301,4 +1336,10 @@ | ||
newCaretPosition = this.correctCaretPosition(value, expectedCaretPosition, direction); | ||
} else if (key === 'Delete' && !numRegex.test(value[expectedCaretPosition]) && !negativeRegex.test(value[expectedCaretPosition])) { | ||
while (!numRegex.test(value[newCaretPosition]) && newCaretPosition < rightBound) { newCaretPosition++; } | ||
} else if ( | ||
key === 'Delete' && | ||
!numRegex.test(value[expectedCaretPosition]) && | ||
!negativeRegex.test(value[expectedCaretPosition]) | ||
) { | ||
while (!numRegex.test(value[newCaretPosition]) && newCaretPosition < rightBound) { | ||
newCaretPosition++; | ||
} | ||
} else if (key === 'Backspace' && !numRegex.test(value[expectedCaretPosition])) { | ||
@@ -1311,5 +1352,11 @@ /* NOTE: This is special case when backspace is pressed on a | ||
var newValue = value.substring(1); | ||
this.updateValue({formattedValue: newValue, caretPos: newCaretPosition, input: el}); | ||
this.updateValue({ | ||
formattedValue: newValue, | ||
caretPos: newCaretPosition, | ||
input: el, | ||
}); | ||
} else if (!negativeRegex.test(value[expectedCaretPosition])) { | ||
while (!numRegex.test(value[newCaretPosition - 1]) && newCaretPosition > leftBound){ newCaretPosition--; } | ||
while (!numRegex.test(value[newCaretPosition - 1]) && newCaretPosition > leftBound) { | ||
newCaretPosition--; | ||
} | ||
newCaretPosition = this.correctCaretPosition(value, newCaretPosition, 'left'); | ||
@@ -1319,4 +1366,7 @@ } | ||
if (newCaretPosition !== expectedCaretPosition || expectedCaretPosition < leftBound || expectedCaretPosition > rightBound) { | ||
if ( | ||
newCaretPosition !== expectedCaretPosition || | ||
expectedCaretPosition < leftBound || | ||
expectedCaretPosition > rightBound | ||
) { | ||
e.preventDefault(); | ||
@@ -1332,5 +1382,3 @@ this.setPatchedCaretPosition(el, newCaretPosition, value); | ||
onKeyDown(e); | ||
}; | ||
@@ -1345,3 +1393,3 @@ | ||
* value can come as undefined when nothing is provided on value prop. | ||
*/ | ||
*/ | ||
var selectionStart = el.selectionStart; | ||
@@ -1378,3 +1426,6 @@ var selectionEnd = el.selectionEnd; | ||
//setPatchedCaretPosition only when everything is not selected on focus (while tabbing into the field) | ||
if (caretPosition !== selectionStart && !(selectionStart === 0 && selectionEnd === value.length)) { | ||
if ( | ||
caretPosition !== selectionStart && | ||
!(selectionStart === 0 && selectionEnd === value.length) | ||
) { | ||
this$1.setPatchedCaretPosition(el, caretPosition, value); | ||
@@ -1401,3 +1452,3 @@ } | ||
// add input mode on element based on format prop and device once the component is mounted | ||
// add input mode on element based on format prop and device once the component is mounted | ||
var inputMode = mounted && addInputMode(format) ? 'numeric' : undefined; | ||
@@ -1412,21 +1463,19 @@ | ||
onFocus: this.onFocus, | ||
onBlur: this.onBlur | ||
onBlur: this.onBlur, | ||
}); | ||
if( displayType === 'text'){ | ||
return renderText ? (renderText(value, otherProps) || null) : React.createElement( 'span', Object.assign({}, otherProps, { ref: getInputRef }), value); | ||
} | ||
else if (customInput) { | ||
if (displayType === 'text') { | ||
return renderText ? ( | ||
renderText(value, otherProps) || null | ||
) : ( | ||
React.createElement( 'span', Object.assign({}, otherProps, { ref: getInputRef }), | ||
value | ||
) | ||
); | ||
} else if (customInput) { | ||
var CustomInput = customInput; | ||
return ( | ||
React.createElement( CustomInput, Object.assign({}, | ||
inputProps, { ref: getInputRef })) | ||
) | ||
return React.createElement( CustomInput, Object.assign({}, inputProps, { ref: getInputRef })); | ||
} | ||
return ( | ||
React.createElement( 'input', Object.assign({}, | ||
inputProps, { ref: getInputRef })) | ||
) | ||
return React.createElement( 'input', Object.assign({}, inputProps, { ref: getInputRef })); | ||
}; | ||
@@ -1433,0 +1482,0 @@ |
@@ -325,6 +325,12 @@ "use strict"; | ||
if (format[caretPos] === '#' && (0, _utils.charIsNumber)(value[caretPos])) return caretPos; //if caretPos is just after input value don't do anything | ||
if (format[caretPos] === '#' && (0, _utils.charIsNumber)(value[caretPos])) { | ||
return caretPos; | ||
} //if caretPos is just after input value don't do anything | ||
if (format[caretPos - 1] === '#' && (0, _utils.charIsNumber)(value[caretPos - 1])) return caretPos; //find the nearest caret position | ||
if (format[caretPos - 1] === '#' && (0, _utils.charIsNumber)(value[caretPos - 1])) { | ||
return caretPos; | ||
} //find the nearest caret position | ||
var firstHashPosition = format.indexOf('#'); | ||
@@ -368,7 +374,13 @@ var lastHashPosition = format.lastIndexOf('#'); //limit the cursor between the first # position and the last # position | ||
if (!currentInputChar.match(numRegex) && currentInputChar !== currentFormatChar) continue; //When we are striping out leading zeros maintain the new cursor position | ||
if (!currentInputChar.match(numRegex) && currentInputChar !== currentFormatChar) { | ||
continue; | ||
} //When we are striping out leading zeros maintain the new cursor position | ||
//Case inputValue = 00023 and formattedValue = 23; | ||
if (currentInputChar === '0' && currentFormatChar.match(numRegex) && currentFormatChar !== '0' && inputNumber.length !== formattedNumber.length) continue; //we are not using currentFormatChar because j can change here | ||
if (currentInputChar === '0' && currentFormatChar.match(numRegex) && currentFormatChar !== '0' && inputNumber.length !== formattedNumber.length) { | ||
continue; | ||
} //we are not using currentFormatChar because j can change here | ||
while (currentInputChar !== formattedValue[j] && j < formattedValue.length) { | ||
@@ -524,3 +536,5 @@ j++; | ||
if (decimalScale !== undefined) afterDecimal = (0, _utils.limitToScale)(afterDecimal, decimalScale, fixedDecimalScale); | ||
if (decimalScale !== undefined) { | ||
afterDecimal = (0, _utils.limitToScale)(afterDecimal, decimalScale, fixedDecimalScale); | ||
} | ||
@@ -710,7 +724,7 @@ if (thousandSeparator) { | ||
if ( // don't do anything if something got added | ||
value.length > lastValue.length || // or if the new value is an empty string | ||
value.length > lastValue.length || // or if the new value is an empty string | ||
!value.length || // or if nothing has changed, in which case start will be same as end | ||
start === end || // or in case if whole input is selected and new value is typed | ||
selectionStart === 0 && selectionEnd === lastValue.length || // or in case if the whole content is replaced by browser, example (autocomplete) | ||
start === 0 && end === lastValue.length || // or if charcters between prefix and suffix is selected. | ||
start === 0 && end === lastValue.length || // or if charcters between prefix and suffix is selected. | ||
// For numeric inputs we apply the format so, prefix and suffix can be ignored | ||
@@ -972,3 +986,3 @@ selectionStart === leftBound && selectionEnd === rightBound) { | ||
* value can come as undefined when nothing is provided on value prop. | ||
*/ | ||
*/ | ||
@@ -1029,3 +1043,3 @@ var selectionStart = el.selectionStart, | ||
mounted = _this$state.mounted; | ||
var otherProps = (0, _utils.omit)(this.props, propTypes); // add input mode on element based on format prop and device once the component is mounted | ||
var otherProps = (0, _utils.omit)(this.props, propTypes); // add input mode on element based on format prop and device once the component is mounted | ||
@@ -1032,0 +1046,0 @@ var inputMode = mounted && (0, _utils.addInputMode)(format) ? 'numeric' : undefined; |
@@ -147,3 +147,3 @@ "use strict"; | ||
// if decimal index is less then 0 add preceding 0s | ||
// add 1 as join will have | ||
// add 1 as join will have | ||
coefficient = '0.' + repeat('0', Math.abs(decimalIndex)) + coefficient; | ||
@@ -150,0 +150,0 @@ } else if (decimalIndex >= coffiecientLn) { |
{ | ||
"name": "react-number-format", | ||
"description": "React component to format number in an input or as a text.", | ||
"version": "4.5.5", | ||
"version": "4.6.0", | ||
"main": "dist/react-number-format.cjs.js", | ||
@@ -29,3 +29,4 @@ "module": "dist/react-number-format.es.js", | ||
"start": "webpack-dev-server --hot", | ||
"bundle": "cross-env yarn compile && yarn bundle-dist && yarn test-build && yarn test-ts", | ||
"format": "prettier --write ./src ./typings ./test ./example", | ||
"bundle": "cross-env yarn compile && yarn bundle-dist && yarn test-build && yarn test-ts && yarn format", | ||
"bundle-dist": "cross-env NODE_ENV=production rollup -c rollup.config.js", | ||
@@ -58,3 +59,3 @@ "compile": "cross-env NODE_ENV=production babel src --out-dir lib", | ||
"eslint": "^6.5.1", | ||
"eslint-config-prettier": "^8.1.0", | ||
"eslint-config-prettier": "^8.3.0", | ||
"eslint-loader": "^3.0.2", | ||
@@ -61,0 +62,0 @@ "eslint-plugin-import": "^2.18.2", |
/// <reference types="react" /> | ||
declare module "react-number-format" { | ||
declare module 'react-number-format' { | ||
//exclude types from the InputHTMLAttributes | ||
const {defaultValue, value, ...inputAttributes}: React.InputHTMLAttributes<HTMLInputElement>; | ||
const { defaultValue, value, ...inputAttributes }: React.InputHTMLAttributes<HTMLInputElement>; | ||
type InputAttributes = typeof inputAttributes; | ||
@@ -22,4 +21,3 @@ | ||
export interface SyntheticInputEvent | ||
extends React.SyntheticEvent<HTMLInputElement> { | ||
export interface SyntheticInputEvent extends React.SyntheticEvent<HTMLInputElement> { | ||
readonly target: HTMLInputElement; | ||
@@ -29,4 +27,3 @@ data: any; | ||
export interface NumberFormatProps | ||
extends InputAttributes { | ||
export interface NumberFormatPropsBase extends InputAttributes { | ||
thousandSeparator?: boolean | string; | ||
@@ -59,3 +56,3 @@ decimalSeparator?: string; | ||
*/ | ||
type?: 'text' | 'tel' | 'password'; | ||
type?: 'text' | 'tel' | 'password'; | ||
isAllowed?: (values: NumberFormatValues) => boolean; | ||
@@ -65,2 +62,8 @@ renderText?: (formattedValue: string) => React.ReactNode; | ||
allowedDecimalSeparators?: Array<string>; | ||
} | ||
// The index signature allows any prop to be passed in, such as if wanting to send props | ||
// to a customInput. We export this as a separate interface to allow client authors to | ||
// choose the stricter typing of NumberFormatPropsBase if desired. | ||
export interface NumberFormatProps extends NumberFormatPropsBase { | ||
[key: string]: any; | ||
@@ -67,0 +70,0 @@ } |
@@ -0,0 +0,0 @@ { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
248139
24
5224