react-number-format
Advanced tools
Comparing version 5.2.2 to 5.3.0
/** | ||
* react-number-format - 5.2.2 | ||
* react-number-format - 5.3.0 | ||
* Author : Sudhanshu Yadav | ||
@@ -52,2 +52,19 @@ * Copyright (c) 2016, 2023 to Sudhanshu Yadav, released under the MIT license. | ||
function noop() { } | ||
function memoizeOnce(cb) { | ||
var lastArgs; | ||
var lastValue = undefined; | ||
return function () { | ||
var args = [], len = arguments.length; | ||
while ( len-- ) args[ len ] = arguments[ len ]; | ||
if (lastArgs && | ||
args.length === lastArgs.length && | ||
args.every(function (value, index) { return value === lastArgs[index]; })) { | ||
return lastValue; | ||
} | ||
lastArgs = args; | ||
lastValue = cb.apply(void 0, args); | ||
return lastValue; | ||
}; | ||
} | ||
function charIsNumber(char) { | ||
@@ -240,3 +257,3 @@ return !!(char || '').match(/\d/); | ||
} | ||
function findChangeRange(prevValue, newValue) { | ||
var findChangeRange = memoizeOnce(function (prevValue, newValue) { | ||
var i = 0, j = 0; | ||
@@ -257,3 +274,3 @@ var prevLength = prevValue.length; | ||
}; | ||
} | ||
}); | ||
/* | ||
@@ -294,11 +311,20 @@ Returns a number whose value is limited to the given range | ||
} | ||
function getCaretPosition(newFormattedValue, lastFormattedValue, curValue, curCaretPos, boundary, isValidInputCharacter) { | ||
var changeRange = findChangeRange(curValue, newFormattedValue); | ||
var from = changeRange.from; | ||
var to = changeRange.to; | ||
// if only last typed character is changed in the | ||
if (from.end - from.start === 1 && from.end === to.end && to.end === curCaretPos) { | ||
// don't do anything | ||
return curCaretPos; | ||
} | ||
function defaultIsCharacterSame(ref) { | ||
var currentValue = ref.currentValue; | ||
var formattedValue = ref.formattedValue; | ||
var currentValueIndex = ref.currentValueIndex; | ||
var formattedValueIndex = ref.formattedValueIndex; | ||
return currentValue[currentValueIndex] === formattedValue[formattedValueIndex]; | ||
} | ||
function getCaretPosition(newFormattedValue, lastFormattedValue, curValue, curCaretPos, boundary, isValidInputCharacter, | ||
/** | ||
* format function can change the character, the caret engine relies on mapping old value and new value | ||
* In such case if character is changed, parent can tell which chars are equivalent | ||
* Some example, all allowedDecimalCharacters are updated to decimalCharacters, 2nd case if user is coverting | ||
* number to different numeric system. | ||
*/ | ||
isCharacterSame) { | ||
if ( isCharacterSame === void 0 ) isCharacterSame = defaultIsCharacterSame; | ||
/** | ||
@@ -311,2 +337,3 @@ * if something got inserted on empty value, add the formatted character before the current value, | ||
if (!lastFormattedValue && !curValue.startsWith(prefixFormat)) { | ||
lastFormattedValue = prefixFormat; | ||
curValue = prefixFormat + curValue; | ||
@@ -323,3 +350,10 @@ curCaretPos = curCaretPos + prefixFormat.length; | ||
for (var j = 0, jLn = formattedValueLn; j < jLn; j++) { | ||
if (curValue[i] === newFormattedValue[j] && addedIndexMap[j] !== true) { | ||
var isCharSame = isCharacterSame({ | ||
currentValue: curValue, | ||
lastValue: lastFormattedValue, | ||
formattedValue: newFormattedValue, | ||
currentValueIndex: i, | ||
formattedValueIndex: j, | ||
}); | ||
if (isCharSame && addedIndexMap[j] !== true) { | ||
indexMap[i] = j; | ||
@@ -465,3 +499,4 @@ addedIndexMap[j] = true; | ||
var isValidInputCharacter = props.isValidInputCharacter; if ( isValidInputCharacter === void 0 ) isValidInputCharacter = charIsNumber; | ||
var otherProps = __rest(props, ["type", "displayType", "customInput", "renderText", "getInputRef", "format", "removeFormatting", "defaultValue", "valueIsNumericString", "onValueChange", "isAllowed", "onChange", "onKeyDown", "onMouseUp", "onFocus", "onBlur", "value", "getCaretBoundary", "isValidInputCharacter"]); | ||
var isCharacterSame = props.isCharacterSame; | ||
var otherProps = __rest(props, ["type", "displayType", "customInput", "renderText", "getInputRef", "format", "removeFormatting", "defaultValue", "valueIsNumericString", "onValueChange", "isAllowed", "onChange", "onKeyDown", "onMouseUp", "onFocus", "onBlur", "value", "getCaretBoundary", "isValidInputCharacter", "isCharacterSame"]); | ||
var ref = useInternalValues(propValue, defaultValue, Boolean(valueIsNumericString), format, removeFormatting, onValueChange); | ||
@@ -521,3 +556,3 @@ var ref_0 = ref[0]; | ||
var caretBoundary = getCaretBoundary(newFormattedValue); | ||
var updatedCaretPos = getCaretPosition(newFormattedValue, formattedValue, inputValue, caretPos, caretBoundary, isValidInputCharacter); | ||
var updatedCaretPos = getCaretPosition(newFormattedValue, formattedValue, inputValue, caretPos, caretBoundary, isValidInputCharacter, isCharacterSame); | ||
//correct caret position if its outside of editable area | ||
@@ -1002,4 +1037,4 @@ updatedCaretPos = getCaretPosInBoundary(newFormattedValue, updatedCaretPos, caretBoundary); | ||
props = validateAndUpdateProps(props); | ||
var decimalSeparator = props.decimalSeparator; if ( decimalSeparator === void 0 ) decimalSeparator = '.'; | ||
var allowedDecimalSeparators = props.allowedDecimalSeparators; | ||
var _decimalSeparator = props.decimalSeparator; | ||
var _allowedDecimalSeparators = props.allowedDecimalSeparators; | ||
var thousandsGroupStyle = props.thousandsGroupStyle; | ||
@@ -1020,2 +1055,6 @@ var suffix = props.suffix; | ||
var restProps = __rest(props, ["decimalSeparator", "allowedDecimalSeparators", "thousandsGroupStyle", "suffix", "allowNegative", "allowLeadingZeros", "onKeyDown", "onBlur", "thousandSeparator", "decimalScale", "fixedDecimalScale", "prefix", "defaultValue", "value", "valueIsNumericString", "onValueChange"]); | ||
// get derived decimalSeparator and allowedDecimalSeparators | ||
var ref = getSeparators(props); | ||
var decimalSeparator = ref.decimalSeparator; | ||
var allowedDecimalSeparators = ref.allowedDecimalSeparators; | ||
var _format = function (numStr) { return format(numStr, props); }; | ||
@@ -1047,7 +1086,7 @@ var _removeFormatting = function (inputValue, changeMeta) { return removeFormatting(inputValue, changeMeta, props); }; | ||
}; | ||
var ref = useInternalValues(roundIncomingValueToPrecision(value), roundIncomingValueToPrecision(defaultValue), Boolean(_valueIsNumericString), _format, _removeFormatting, onValueChange); | ||
var ref_0 = ref[0]; | ||
var numAsString = ref_0.numAsString; | ||
var formattedValue = ref_0.formattedValue; | ||
var _onValueChange = ref[1]; | ||
var ref$1 = useInternalValues(roundIncomingValueToPrecision(value), roundIncomingValueToPrecision(defaultValue), Boolean(_valueIsNumericString), _format, _removeFormatting, onValueChange); | ||
var ref$1_0 = ref$1[0]; | ||
var numAsString = ref$1_0.numAsString; | ||
var formattedValue = ref$1_0.formattedValue; | ||
var _onValueChange = ref$1[1]; | ||
var _onKeyDown = function (e) { | ||
@@ -1073,5 +1112,2 @@ var el = e.target; | ||
// don't allow user to delete decimal separator when decimalScale and fixedDecimalScale is set | ||
var ref = getSeparators(props); | ||
var decimalSeparator = ref.decimalSeparator; | ||
var allowedDecimalSeparators = ref.allowedDecimalSeparators; | ||
if (key === 'Backspace' && | ||
@@ -1130,3 +1166,29 @@ value[selectionStart - 1] === decimalSeparator && | ||
}; | ||
return Object.assign(Object.assign({}, restProps), { value: formattedValue, valueIsNumericString: false, isValidInputCharacter: isValidInputCharacter, onValueChange: _onValueChange, format: _format, removeFormatting: _removeFormatting, getCaretBoundary: function (formattedValue) { return getCaretBoundary(formattedValue, props); }, onKeyDown: _onKeyDown, onBlur: _onBlur }); | ||
var isCharacterSame = function (ref) { | ||
var currentValue = ref.currentValue; | ||
var lastValue = ref.lastValue; | ||
var formattedValue = ref.formattedValue; | ||
var currentValueIndex = ref.currentValueIndex; | ||
var formattedValueIndex = ref.formattedValueIndex; | ||
var curChar = currentValue[currentValueIndex]; | ||
var newChar = formattedValue[formattedValueIndex]; | ||
/** | ||
* NOTE: as thousand separator and allowedDecimalSeparators can be same, we need to check on | ||
* typed range if we have typed any character from allowedDecimalSeparators, in that case we | ||
* consider different characters like , and . same within the range of updated value. | ||
*/ | ||
var typedRange = findChangeRange(lastValue, currentValue); | ||
var to = typedRange.to; | ||
if (currentValueIndex >= to.start && | ||
currentValueIndex < to.end && | ||
allowedDecimalSeparators && | ||
allowedDecimalSeparators.includes(curChar) && | ||
newChar === decimalSeparator) { | ||
return true; | ||
} | ||
return curChar === newChar; | ||
}; | ||
return Object.assign(Object.assign({}, restProps), { value: formattedValue, valueIsNumericString: false, isValidInputCharacter: isValidInputCharacter, | ||
isCharacterSame: isCharacterSame, onValueChange: _onValueChange, format: _format, removeFormatting: _removeFormatting, getCaretBoundary: function (formattedValue) { return getCaretBoundary(formattedValue, props); }, onKeyDown: _onKeyDown, onBlur: _onBlur }); | ||
} | ||
@@ -1133,0 +1195,0 @@ function NumericFormat(props) { |
/** | ||
* react-number-format - 5.2.2 | ||
* react-number-format - 5.3.0 | ||
* Author : Sudhanshu Yadav | ||
@@ -45,2 +45,19 @@ * Copyright (c) 2016, 2023 to Sudhanshu Yadav, released under the MIT license. | ||
function noop() { } | ||
function memoizeOnce(cb) { | ||
var lastArgs; | ||
var lastValue = undefined; | ||
return function () { | ||
var args = [], len = arguments.length; | ||
while ( len-- ) args[ len ] = arguments[ len ]; | ||
if (lastArgs && | ||
args.length === lastArgs.length && | ||
args.every(function (value, index) { return value === lastArgs[index]; })) { | ||
return lastValue; | ||
} | ||
lastArgs = args; | ||
lastValue = cb.apply(void 0, args); | ||
return lastValue; | ||
}; | ||
} | ||
function charIsNumber(char) { | ||
@@ -233,3 +250,3 @@ return !!(char || '').match(/\d/); | ||
} | ||
function findChangeRange(prevValue, newValue) { | ||
var findChangeRange = memoizeOnce(function (prevValue, newValue) { | ||
var i = 0, j = 0; | ||
@@ -250,3 +267,3 @@ var prevLength = prevValue.length; | ||
}; | ||
} | ||
}); | ||
/* | ||
@@ -287,11 +304,20 @@ Returns a number whose value is limited to the given range | ||
} | ||
function getCaretPosition(newFormattedValue, lastFormattedValue, curValue, curCaretPos, boundary, isValidInputCharacter) { | ||
var changeRange = findChangeRange(curValue, newFormattedValue); | ||
var from = changeRange.from; | ||
var to = changeRange.to; | ||
// if only last typed character is changed in the | ||
if (from.end - from.start === 1 && from.end === to.end && to.end === curCaretPos) { | ||
// don't do anything | ||
return curCaretPos; | ||
} | ||
function defaultIsCharacterSame(ref) { | ||
var currentValue = ref.currentValue; | ||
var formattedValue = ref.formattedValue; | ||
var currentValueIndex = ref.currentValueIndex; | ||
var formattedValueIndex = ref.formattedValueIndex; | ||
return currentValue[currentValueIndex] === formattedValue[formattedValueIndex]; | ||
} | ||
function getCaretPosition(newFormattedValue, lastFormattedValue, curValue, curCaretPos, boundary, isValidInputCharacter, | ||
/** | ||
* format function can change the character, the caret engine relies on mapping old value and new value | ||
* In such case if character is changed, parent can tell which chars are equivalent | ||
* Some example, all allowedDecimalCharacters are updated to decimalCharacters, 2nd case if user is coverting | ||
* number to different numeric system. | ||
*/ | ||
isCharacterSame) { | ||
if ( isCharacterSame === void 0 ) isCharacterSame = defaultIsCharacterSame; | ||
/** | ||
@@ -304,2 +330,3 @@ * if something got inserted on empty value, add the formatted character before the current value, | ||
if (!lastFormattedValue && !curValue.startsWith(prefixFormat)) { | ||
lastFormattedValue = prefixFormat; | ||
curValue = prefixFormat + curValue; | ||
@@ -316,3 +343,10 @@ curCaretPos = curCaretPos + prefixFormat.length; | ||
for (var j = 0, jLn = formattedValueLn; j < jLn; j++) { | ||
if (curValue[i] === newFormattedValue[j] && addedIndexMap[j] !== true) { | ||
var isCharSame = isCharacterSame({ | ||
currentValue: curValue, | ||
lastValue: lastFormattedValue, | ||
formattedValue: newFormattedValue, | ||
currentValueIndex: i, | ||
formattedValueIndex: j, | ||
}); | ||
if (isCharSame && addedIndexMap[j] !== true) { | ||
indexMap[i] = j; | ||
@@ -458,3 +492,4 @@ addedIndexMap[j] = true; | ||
var isValidInputCharacter = props.isValidInputCharacter; if ( isValidInputCharacter === void 0 ) isValidInputCharacter = charIsNumber; | ||
var otherProps = __rest(props, ["type", "displayType", "customInput", "renderText", "getInputRef", "format", "removeFormatting", "defaultValue", "valueIsNumericString", "onValueChange", "isAllowed", "onChange", "onKeyDown", "onMouseUp", "onFocus", "onBlur", "value", "getCaretBoundary", "isValidInputCharacter"]); | ||
var isCharacterSame = props.isCharacterSame; | ||
var otherProps = __rest(props, ["type", "displayType", "customInput", "renderText", "getInputRef", "format", "removeFormatting", "defaultValue", "valueIsNumericString", "onValueChange", "isAllowed", "onChange", "onKeyDown", "onMouseUp", "onFocus", "onBlur", "value", "getCaretBoundary", "isValidInputCharacter", "isCharacterSame"]); | ||
var ref = useInternalValues(propValue, defaultValue, Boolean(valueIsNumericString), format, removeFormatting, onValueChange); | ||
@@ -514,3 +549,3 @@ var ref_0 = ref[0]; | ||
var caretBoundary = getCaretBoundary(newFormattedValue); | ||
var updatedCaretPos = getCaretPosition(newFormattedValue, formattedValue, inputValue, caretPos, caretBoundary, isValidInputCharacter); | ||
var updatedCaretPos = getCaretPosition(newFormattedValue, formattedValue, inputValue, caretPos, caretBoundary, isValidInputCharacter, isCharacterSame); | ||
//correct caret position if its outside of editable area | ||
@@ -995,4 +1030,4 @@ updatedCaretPos = getCaretPosInBoundary(newFormattedValue, updatedCaretPos, caretBoundary); | ||
props = validateAndUpdateProps(props); | ||
var decimalSeparator = props.decimalSeparator; if ( decimalSeparator === void 0 ) decimalSeparator = '.'; | ||
var allowedDecimalSeparators = props.allowedDecimalSeparators; | ||
var _decimalSeparator = props.decimalSeparator; | ||
var _allowedDecimalSeparators = props.allowedDecimalSeparators; | ||
var thousandsGroupStyle = props.thousandsGroupStyle; | ||
@@ -1013,2 +1048,6 @@ var suffix = props.suffix; | ||
var restProps = __rest(props, ["decimalSeparator", "allowedDecimalSeparators", "thousandsGroupStyle", "suffix", "allowNegative", "allowLeadingZeros", "onKeyDown", "onBlur", "thousandSeparator", "decimalScale", "fixedDecimalScale", "prefix", "defaultValue", "value", "valueIsNumericString", "onValueChange"]); | ||
// get derived decimalSeparator and allowedDecimalSeparators | ||
var ref = getSeparators(props); | ||
var decimalSeparator = ref.decimalSeparator; | ||
var allowedDecimalSeparators = ref.allowedDecimalSeparators; | ||
var _format = function (numStr) { return format(numStr, props); }; | ||
@@ -1040,7 +1079,7 @@ var _removeFormatting = function (inputValue, changeMeta) { return removeFormatting(inputValue, changeMeta, props); }; | ||
}; | ||
var ref = useInternalValues(roundIncomingValueToPrecision(value), roundIncomingValueToPrecision(defaultValue), Boolean(_valueIsNumericString), _format, _removeFormatting, onValueChange); | ||
var ref_0 = ref[0]; | ||
var numAsString = ref_0.numAsString; | ||
var formattedValue = ref_0.formattedValue; | ||
var _onValueChange = ref[1]; | ||
var ref$1 = useInternalValues(roundIncomingValueToPrecision(value), roundIncomingValueToPrecision(defaultValue), Boolean(_valueIsNumericString), _format, _removeFormatting, onValueChange); | ||
var ref$1_0 = ref$1[0]; | ||
var numAsString = ref$1_0.numAsString; | ||
var formattedValue = ref$1_0.formattedValue; | ||
var _onValueChange = ref$1[1]; | ||
var _onKeyDown = function (e) { | ||
@@ -1066,5 +1105,2 @@ var el = e.target; | ||
// don't allow user to delete decimal separator when decimalScale and fixedDecimalScale is set | ||
var ref = getSeparators(props); | ||
var decimalSeparator = ref.decimalSeparator; | ||
var allowedDecimalSeparators = ref.allowedDecimalSeparators; | ||
if (key === 'Backspace' && | ||
@@ -1123,3 +1159,29 @@ value[selectionStart - 1] === decimalSeparator && | ||
}; | ||
return Object.assign(Object.assign({}, restProps), { value: formattedValue, valueIsNumericString: false, isValidInputCharacter: isValidInputCharacter, onValueChange: _onValueChange, format: _format, removeFormatting: _removeFormatting, getCaretBoundary: function (formattedValue) { return getCaretBoundary(formattedValue, props); }, onKeyDown: _onKeyDown, onBlur: _onBlur }); | ||
var isCharacterSame = function (ref) { | ||
var currentValue = ref.currentValue; | ||
var lastValue = ref.lastValue; | ||
var formattedValue = ref.formattedValue; | ||
var currentValueIndex = ref.currentValueIndex; | ||
var formattedValueIndex = ref.formattedValueIndex; | ||
var curChar = currentValue[currentValueIndex]; | ||
var newChar = formattedValue[formattedValueIndex]; | ||
/** | ||
* NOTE: as thousand separator and allowedDecimalSeparators can be same, we need to check on | ||
* typed range if we have typed any character from allowedDecimalSeparators, in that case we | ||
* consider different characters like , and . same within the range of updated value. | ||
*/ | ||
var typedRange = findChangeRange(lastValue, currentValue); | ||
var to = typedRange.to; | ||
if (currentValueIndex >= to.start && | ||
currentValueIndex < to.end && | ||
allowedDecimalSeparators && | ||
allowedDecimalSeparators.includes(curChar) && | ||
newChar === decimalSeparator) { | ||
return true; | ||
} | ||
return curChar === newChar; | ||
}; | ||
return Object.assign(Object.assign({}, restProps), { value: formattedValue, valueIsNumericString: false, isValidInputCharacter: isValidInputCharacter, | ||
isCharacterSame: isCharacterSame, onValueChange: _onValueChange, format: _format, removeFormatting: _removeFormatting, getCaretBoundary: function (formattedValue) { return getCaretBoundary(formattedValue, props); }, onKeyDown: _onKeyDown, onBlur: _onBlur }); | ||
} | ||
@@ -1126,0 +1188,0 @@ function NumericFormat(props) { |
@@ -1,1 +0,1 @@ | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("react")):"function"==typeof define&&define.amd?define(["exports","react"],t):t((e=e||self).NumberFormat={},e.React)}(this,function(e,R){"use strict";var k,t,M="default"in R?R.default:R;function P(e,t){var r={};for(a in e)Object.prototype.hasOwnProperty.call(e,a)&&t.indexOf(a)<0&&(r[a]=e[a]);if(null!=e&&"function"==typeof Object.getOwnPropertySymbols)for(var n=0,a=Object.getOwnPropertySymbols(e);n<a.length;n++)t.indexOf(a[n])<0&&Object.prototype.propertyIsEnumerable.call(e,a[n])&&(r[a[n]]=e[a[n]]);return r}function K(){}function W(e){return!!(e||"").match(/\d/)}function N(e){return null==e}function O(e){return N(e)||"number"==typeof(t=e)&&isNaN(t)||"number"==typeof e&&!isFinite(e);var t}function h(e){return e.replace(/[-[\]/{}()*+?.\\^$|]/g,"\\$&")}function d(e,t,r){var r=function(e){switch(e){case"lakh":return/(\d+?)(?=(\d\d)+(\d)(?!\d))(\.\d+)?/g;case"wan":return/(\d)(?=(\d{4})+(?!\d))/g;default:return/(\d)(?=(\d{3})+(?!\d))/g}}(r),n=-1===(n=e.search(/[1-9]/))?e.length:n;return e.substring(0,n)+e.substring(n,e.length).replace(r,"$1"+t)}function S(e,t){void 0===t&&(t=!0);var r="-"===e[0],t=r&&t,e=(e=e.replace("-","")).split(".");return{beforeDecimal:e[0],afterDecimal:e[1]||"",hasNegation:r,addNegation:t}}function g(e,t,r){for(var n="",a=r?"0":"",o=0;o<=t-1;o++)n+=e[o]||a;return n}function a(e,t){return Array(t+1).join(e)}function D(e){var t,e=e+"",r="-"===e[0]?"-":"",e=(e=r?e.substring(1):e).split(/[eE]/g),n=e[0],e=e[1];return(e=Number(e))&&(e=1+e,t=(n=n.replace(".","")).length,e<0?n="0."+a("0",Math.abs(e))+n:t<=e?n+=a("0",e-t):n=(n.substring(0,e)||"0")+"."+n.substring(e)),r+n}function E(e,t,r){var n,a,o,u;return-1!==["","-"].indexOf(e)?e:(n=(-1!==e.indexOf(".")||r)&&t,a=(e=S(e)).beforeDecimal,u=e.afterDecimal,e=e.hasNegation,o=parseFloat("0."+(u||"0")),u=(u.length<=t?"0."+u:o.toFixed(t)).split("."),(e?"-":"")+a.split("").reverse().reduce(function(e,t,r){return e.length>r?(Number(e[0])+Number(t)).toString()+e.substring(1,e.length):t+e},u[0])+(n?".":"")+g(u[1]||"",t,r))}function L(e,t){var r;e.value=e.value,null!==e&&(e.createTextRange?((r=e.createTextRange()).move("character",t),r.select()):e.selectionStart||0===e.selectionStart?(e.focus(),e.setSelectionRange(t,t)):e.focus())}function U(e,t){for(var r=0,n=0,a=e.length,o=t.length;e[r]===t[r]&&r<a;)r++;for(;e[a-1-n]===t[o-1-n]&&r<o-n&&r<a-n;)n++;return{from:{start:r,end:a-n},to:{start:r,end:o-n}}}function $(e){return Math.max(e.selectionStart,e.selectionEnd)}function y(e){return{from:{start:0,end:0},to:{start:0,end:e.length},lastValue:""}}function m(e,t){return"string"==typeof(e=void 0===e?" ":e)?e:e[t]||" "}function G(e,t,r,n){var a,o,u=e.length;if(e=t,a=0,o=u,t=Math.min(Math.max(e,a),o),"left"===n){for(;0<=t&&!r[t];)t--;-1===t&&(t=r.indexOf(!0))}else{for(;t<=u&&!r[t];)t++;u<t&&(t=r.lastIndexOf(!0))}return t=-1===t?u:t}function Z(e){for(var t=Array.from({length:e.length+1}).map(function(){return!0}),r=0,n=t.length;r<n;r++)t[r]=Boolean(W(e[r])||W(e[r-1]));return t}function _(e,t,r,n,a,o){void 0===o&&(o=K);l=function(e,t){var r,t=O(e)?r="":(r="number"==typeof e||t?"number"==typeof e?D(e):e:a(e,void 0),n(r));return{formattedValue:t,numAsString:r}},(u=R.useRef(l)).current=l;var u,i=R.useRef(function(){for(var e=[],t=arguments.length;t--;)e[t]=arguments[t];return u.current.apply(u,e)}).current,l=R.useState(function(){return i(N(e)?t:e,r)}),s=l[0],c=l[1],l=e,f=r,d=(N(e)&&(l=s.numAsString,f=!0),i(l,f));return R.useMemo(function(){c(d)},[d.formattedValue]),[s,function(e,t){e.formattedValue!==s.formattedValue&&c({formattedValue:e.formattedValue,numAsString:e.value}),o(e,t)}]}function q(e){return e.replace(/[^0-9]/g,"")}function z(e){return e}function r(e){function s(e,t){O.current={formattedValue:e.formattedValue,numAsString:e.value},N(e,t)}function i(e,t,r){return G(e,t,b(e),r)}var t=e.type,r=(void 0===t&&(t="text"),e.displayType),n=(void 0===r&&(r="input"),e.customInput),a=e.renderText,o=e.getInputRef,u=e.format,l=(void 0===u&&(u=z),e.removeFormatting),c=(void 0===l&&(l=q),e.defaultValue),f=e.valueIsNumericString,d=e.onValueChange,g=e.isAllowed,m=e.onChange,v=(void 0===m&&(m=K),e.onKeyDown),p=(void 0===v&&(v=K),e.onMouseUp),h=(void 0===p&&(p=K),e.onFocus),S=(void 0===h&&(h=K),e.onBlur),y=(void 0===S&&(S=K),e.value),b=e.getCaretBoundary,w=(void 0===b&&(b=Z),e.isValidInputCharacter),e=(void 0===w&&(w=W),P(e,["type","displayType","customInput","renderText","getInputRef","format","removeFormatting","defaultValue","valueIsNumericString","onValueChange","isAllowed","onChange","onKeyDown","onMouseUp","onFocus","onBlur","value","getCaretBoundary","isValidInputCharacter"])),y=_(y,c,Boolean(f),u,l,d),c=y[0],x=c.formattedValue,V=c.numAsString,N=y[1],O=R.useRef({formattedValue:x,numAsString:V}),f=R.useState(!1),d=f[0],D=f[1],E=R.useRef(null),C=R.useRef({setCaretTimeout:null,focusTimeout:null}),F=(R.useEffect(function(){return D(!0),function(){clearTimeout(C.current.setCaretTimeout),clearTimeout(C.current.focusTimeout)}},[]),u),j=function(e,t){var r=parseFloat(t);return{formattedValue:e,value:t,floatValue:isNaN(r)?void 0:r}},A=function(e,t,r){0===e.selectionStart&&e.selectionEnd===e.value.length||(L(e,t),C.current.setCaretTimeout=setTimeout(function(){e.value===r&&e.selectionStart!==e.selectionEnd&&L(e,t)},0))},B=function(e,t,r){var n=b(t);return G(t,function(e,t,r,n,a,o){var u=(i=U(r,e)).from,i=i.to;if(u.end-u.start==1&&u.end===i.end&&i.end===n)return n;for(var u=a.findIndex(function(e){return e}),i=e.slice(0,u),l=(t||r.startsWith(i)||(r=i+r,n+=i.length),r.length),s=e.length,c={},f=new Array(l),d=0;d<l;d++){f[d]=-1;for(var g=0,m=s;g<m;g++)if(r[d]===e[g]&&!0!==c[g]){c[f[d]=g]=!0;break}}for(var v=n;v<l&&(-1===f[v]||!o(r[v]));)v++;for(a=v===l||-1===f[v]?s:f[v],v=n-1;0<v&&-1===f[v];)v--;return u=-1===v||-1===f[v]?0:f[v]+1,!(a<u)&&n-u<a-n?u:a}(t,x,e,r,n,w),n)},T=function(e){var t,r=e.formattedValue,n=(void 0===r&&(r=""),e.input),a=e.setCaretPosition,o=(void 0===a&&(a=!0),e.source),u=e.event,i=e.numAsString,l=e.caretPos;n&&(void 0===l&&a&&(e=e.inputValue||n.value,t=$(n),n.value=r,l=B(e,r,t)),n.value=r,a&&void 0!==l&&A(n,l,r)),r!==x&&s(j(r,i),{event:u,source:o})},I=(R.useEffect(function(){var e=O.current,t=e.formattedValue,e=e.numAsString;x===t||x===V&&t===e||s(j(x,V),{event:void 0,source:k.props})},[x,V]),E.current?$(E.current):void 0),c=(("undefined"!=typeof window?R.useLayoutEffect:R.useEffect)(function(){var e,t=E.current;x!==O.current.formattedValue&&t&&(e=B(O.current.formattedValue,x,I),t.value=x,A(t,e,x))},[x]),d&&!("undefined"==typeof navigator||navigator.platform&&/iPhone|iPod/.test(navigator.platform))?"numeric":void 0),y=Object.assign({inputMode:c},e,{type:t,value:x,onChange:function(e){var t,r,n,a,o,u,i=e.target.value;i=i,t=e,r=k.event,o=U(x,i),o=Object.assign(Object.assign({},o),{lastValue:x}),o=l(i,o),u=F(o),o=l(u,void 0),(g&&!g(j(u,o))?(a=$(n=t.target),a=B(i,x,a),n.value=x,A(n,a,x),1):(T({formattedValue:u,numAsString:o,inputValue:i,event:t,source:r,setCaretPosition:!0,input:t.target}),0))||m(e)},onKeyDown:function(e){var t,r=e.target,n=e.key,a=r.selectionStart,o=r.selectionEnd,u=r.value;void 0===u&&(u=""),"ArrowLeft"===n||"Backspace"===n?t=Math.max(a-1,0):"ArrowRight"===n?t=Math.min(a+1,u.length):"Delete"===n&&(t=a),void 0!==t&&a===o&&(a=t,"ArrowLeft"===n||"ArrowRight"===n?(a=i(u,t,"ArrowLeft"===n?"left":"right"))!==t&&e.preventDefault():"Delete"!==n||w(u[t])?"Backspace"!==n||w(u[t])||(a=i(u,t,"left")):a=i(u,t,"right"),a!==t&&A(r,a,u),e.isUnitTestRun&&A(r,a,u)),v(e)},onMouseUp:function(e){var t=e.target,r=t.selectionStart,n=t.selectionEnd,a=t.value;void 0===a&&(a=""),r===n&&(n=i(a,r))!==r&&A(t,n,a),p(e)},onFocus:function(a){a.persist&&a.persist();var o=a.target;E.current=o,C.current.focusTimeout=setTimeout(function(){var e=o.selectionStart,t=o.selectionEnd,r=o.value,n=i(r=void 0===r?"":r,e);n===e||0===e&&t===r.length||A(o,n,r),h(a)},0)},onBlur:function(e){E.current=null,clearTimeout(C.current.focusTimeout),clearTimeout(C.current.setCaretTimeout),S(e)}});return"text"===r?a?M.createElement(M.Fragment,null,a(x,e)||null):M.createElement("span",Object.assign({},e,{ref:o}),x):n?M.createElement(n,Object.assign({},y,{ref:o})):M.createElement("input",Object.assign({},y,{ref:o}))}function C(e,t){var r,n,a,o,u=t.decimalScale,i=t.fixedDecimalScale,l=t.prefix,s=(void 0===l&&(l=""),t.suffix),c=(void 0===s&&(s=""),t.allowNegative),f=t.thousandsGroupStyle;return void 0===f&&(f="thousand"),""!==e&&"-"!==e&&(r=(t=F(t)).thousandSeparator,t=t.decimalSeparator,n=0!==u&&-1!==e.indexOf(".")||u&&i,a=(c=S(e,c)).beforeDecimal,o=c.afterDecimal,c=c.addNegation,void 0!==u&&(o=g(o,u,!!i)),r&&(a=d(a,r,f)),l&&(a=l+a),s&&(o+=s),e=(a=c?"-"+a:a)+(n&&t||"")+o),e}function F(e){var t=e.decimalSeparator,r=e.thousandSeparator,e=e.allowedDecimalSeparators;return{decimalSeparator:t=void 0===t?".":t,thousandSeparator:r=!0===r?",":r,allowedDecimalSeparators:e=e||[t,"."]}}function j(e,t,r){void 0===t&&(t=y(e));var n,a,o,u,i=r.allowNegative,l=r.prefix,s=(void 0===l&&(l=""),r.suffix),c=(void 0===s&&(s=""),r.decimalScale),f=t.from,d=t.to,g=d.start,m=d.end,r=F(r),v=r.allowedDecimalSeparators,r=r.decimalSeparator,p=e[m]===r;return W(e)&&(e===l||e===s)&&""===t.lastValue||(m-g==1&&-1!==v.indexOf(e[g])&&(v=0===c?"":r,e=e.substring(0,g)+v+e.substring(g+1,e.length)),v=(c=function(e,t,r){var n=!1,a=!1,o=(l.startsWith("-")?n=!1:e.startsWith("--")?a=!(n=!1):s.startsWith("-")&&e.length===s.length?n=!1:"-"===e[0]&&(n=!0),n?1:0);return(o=a?2:o)&&(e=e.substring(o),t-=o,r-=o),{value:e,start:t,end:r,hasNegation:n}})(e,g,m),u=v.hasNegation,e=v.value,g=v.start,m=v.end,c=(v=c(t.lastValue,f.start,f.end)).start,t=v.end,v=v.value,n=e.substring(g,m),a=0,(e=!(e.length&&v.length&&(c>v.length-s.length||t<l.length))||n&&s.startsWith(n)?e:v).startsWith(l)?a+=l.length:g<l.length&&(a=g),m-=a,c=(e=e.substring(a)).length,t=e.length-s.length,e.endsWith(s)?c=t:(t<m||m>e.length-s.length)&&(c=m),e=e.substring(0,c),n=i,void 0===(v=u?"-"+e:e)&&(v=""),g=new RegExp("(-)"),a=new RegExp("(-)(.)*(-)"),g=g.test(v),a=a.test(v),v=v.replace(/-/g,""),o=(e=((e=v=g&&!a&&n?"-"+v:v).match((t=!0,new RegExp("(^-)|[0-9]|"+h(r),t?"g":void 0)))||[]).join("")).indexOf(r),c=(m=S(e=e.replace(new RegExp(h(r),"g"),function(e,t){return t===o?".":""}),i)).beforeDecimal,u=m.afterDecimal,g=m.addNegation,d.end-d.start<f.end-f.start&&""===c&&p&&!parseFloat(u)&&(e=g?"-":"")),e}function A(e,t){var r=t.prefix,t=(void 0===r&&(r=""),t.suffix),n=(void 0===t&&(t=""),Array.from({length:e.length+1}).map(function(){return!0})),a="-"===e[0],r=(n.fill(!1,0,r.length+(a?1:0)),e.length);return n.fill(!1,r-t.length+1,r+1),n}function n(i){function e(e){return C(e,i)}function t(e,t){return j(e,t,i)}function r(e){return!O(e)&&("number"==typeof e&&(e=D(e)),b&&"number"==typeof f)?E(e,f,Boolean(d)):e}var n=(i=function(e){var t=(r=F(e)).thousandSeparator,r=r.decimalSeparator,n=e.prefix,a=(void 0===n&&(n=""),e.allowNegative);if(void 0===a&&(a=!0),t===r)throw new Error("\n Decimal separator can't be same as thousand separator.\n thousandSeparator: "+t+' (thousandSeparator = {true} is same as thousandSeparator = ",")\n decimalSeparator: '+r+" (default value for decimalSeparator is .)\n ");return n.startsWith("-")&&a&&(console.error("\n Prefix can't start with '-' when allowNegative is true.\n prefix: "+n+"\n allowNegative: "+a+"\n "),a=!1),Object.assign(Object.assign({},e),{allowNegative:a})}(i)).decimalSeparator,a=(void 0===n&&(n="."),i.allowedDecimalSeparators,i.thousandsGroupStyle,i.suffix),l=i.allowNegative,o=i.allowLeadingZeros,s=i.onKeyDown,u=(void 0===s&&(s=K),i.onBlur),c=(void 0===u&&(u=K),i.thousandSeparator),f=i.decimalScale,d=i.fixedDecimalScale,g=i.prefix,m=(void 0===g&&(g=""),i.defaultValue),v=i.value,p=i.valueIsNumericString,h=i.onValueChange,S=P(i,["decimalSeparator","allowedDecimalSeparators","thousandsGroupStyle","suffix","allowNegative","allowLeadingZeros","onKeyDown","onBlur","thousandSeparator","decimalScale","fixedDecimalScale","prefix","defaultValue","value","valueIsNumericString","onValueChange"]),y=N(v)?m:v,b=null!=p?p:(w=g,a=a,""===(y=y)||!(null!=w&&w.match(/\d/)||null!=a&&a.match(/\d/)||"string"!=typeof y||isNaN(Number(y)))),w=(N(v)?N(m)||(b=p||"number"==typeof m):b=p||"number"==typeof v,_(r(v),r(m),Boolean(b),e,t,h)),a=w[0],x=a.numAsString,y=a.formattedValue,V=w[1];return Object.assign(Object.assign({},S),{value:y,valueIsNumericString:!1,isValidInputCharacter:function(e){return e===n||W(e)},onValueChange:V,format:e,removeFormatting:t,getCaretBoundary:function(e){return A(e,i)},onKeyDown:function(e){var t,r=e.target,n=e.key,a=r.selectionStart,o=r.selectionEnd,u=r.value;void 0===u&&(u=""),a===o&&("Backspace"===n&&"-"===u[0]&&a===g.length+1&&l&&L(r,1),t=(o=F(i)).decimalSeparator,o=o.allowedDecimalSeparators,"Backspace"===n&&u[a-1]===t&&f&&d&&(L(r,a-1),e.preventDefault()),null!=o&&o.includes(n)&&u[a]===t&&L(r,a+1),o=!0===c?",":c,"Backspace"===n&&u[a-1]===o&&L(r,a-1),"Delete"===n&&u[a]===o&&L(r,a+1)),s(e)},onBlur:function(e){var t,r,n=x;n.match(/\d/g)||(n=""),o||(n=(r=n)&&((t="-"===r[0])?"-":"")+((t=(r=t?r.substring(1,r.length):r).split("."))[0].replace(/^0+/,"")||"0")+((t=t[1]||"")?"."+t:"")),(n=d&&f?E(n,f,d):n)!==x&&(r=C(n,i),V({formattedValue:r,value:n,floatValue:parseFloat(n)},{event:e,source:k.event})),u(e)}})}function v(e,t){var r=t.format,n=t.allowEmptyFormatting,a=t.mask,o=t.patternChar;if(void 0===o&&(o="#"),""===e&&!n)return"";for(var u=0,i=r.split(""),l=0,s=r.length;l<s;l++)r[l]===o&&(i[l]=e[u]||m(a,u),u+=1);return i.join("")}function p(e,t,r){void 0===t&&(t=y(e));function a(e){return u[e]===i}function n(e,t){for(var r="",n=0;n<e.length;n++)a(t+n)&&W(e[n])&&(r+=e[n]);return r}function o(e){return e.replace(/[^0-9]/g,"")}var u=r.format,i=r.patternChar,r=(void 0===i&&(i="#"),t.from),l=t.to,t=t.lastValue;void 0===t&&(t="");if(!u.match(/\d/))return o(e);if(""===t&&e.length===u.length){for(var s="",c=0;c<e.length;c++)if(a(c))W(e[c])&&(s+=e[c]);else if(e[c]!==u[c])return o(e);return s}var f=t.substring(0,r.start),l=e.substring(l.start,l.end),t=t.substring(r.end);return""+n(f,0)+o(l)+n(t,r.end)}function b(n,e){for(var t=e.format,a=e.mask,o=e.patternChar,r=(void 0===o&&(o="#"),Array.from({length:n.length+1}).map(function(){return!0})),u=0,i=-1,l={},s=(t.split("").forEach(function(e,t){var r=void 0;e===o&&(r=m(a,++u-1),-1===i&&n[t]===r&&(i=t)),l[t]=r}),function(e){return t[e]===o&&n[e]!==l[e]}),c=0,f=r.length;c<f;c++)r[c]=c===i||s(c)||s(c-1);return r[t.indexOf(o)]=!0,r}function o(t){t.mask,t.allowEmptyFormatting;var l=t.format,e=t.inputMode,s=(void 0===e&&(e="numeric"),t.onKeyDown),c=(void 0===s&&(s=K),t.patternChar),r=(void 0===c&&(c="#"),t.value),n=t.defaultValue,a=t.valueIsNumericString,o=P(t,["mask","allowEmptyFormatting","format","inputMode","onKeyDown","patternChar","value","defaultValue","valueIsNumericString"]),u=t;if((u=u.mask)&&("string"===u?u:u.toString()).match(/\d/g))throw new Error("Mask "+u+" should not contain numeric character;");function f(e){return b(e,t)}var u=N(r)?n:r,a=null!=a?a:(a=l,""===(u=u)||!(null!=a&&a.match(/\d/)||"string"!=typeof u||!u.match(/^\d+$/)&&""!==u)),i=Object.assign(Object.assign({},t),{valueIsNumericString:a});return Object.assign(Object.assign({},o),{value:r,defaultValue:n,valueIsNumericString:a,inputMode:e,format:function(e){return v(e,i)},removeFormatting:function(e,t){return p(e,t,i)},getCaretBoundary:f,onKeyDown:function(e){var t=e.key,r=e.target,n=r.selectionStart,a=r.selectionEnd,o=r.value;if(n===a){var u=n;if("Backspace"===t||"Delete"===t){a="right";if("Backspace"===t){for(;0<u&&l[u-1]!==c;)u--;a="left"}else{for(var i=l.length;u<i&&l[u]!==c;)u++;a="right"}u=G(o,u,f(o),a)}else l[u]!==c&&"ArrowLeft"!==t&&"ArrowRight"!==t&&(u=G(o,u+1,f(o),"right"));u!==n&&L(r,u)}s(e)}})}(t=k=k||{}).event="event",t.props="prop",e.NumberFormatBase=r,e.NumericFormat=function(e){return e=n(e),M.createElement(r,Object.assign({},e))},e.PatternFormat=function(e){return e=o(e),M.createElement(r,Object.assign({},e))},e.getNumericCaretBoundary=A,e.getPatternCaretBoundary=b,e.numericFormatter=C,e.patternFormatter=v,e.removeNumericFormat=j,e.removePatternFormat=p,e.useNumericFormat=n,e.usePatternFormat=o,Object.defineProperty(e,"__esModule",{value:!0})}); | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("react")):"function"==typeof define&&define.amd?define(["exports","react"],t):t((e=e||self).NumberFormat={},e.React)}(this,function(e,k){"use strict";var M,t,P="default"in k?k.default:k;function K(e,t){var r={};for(a in e)Object.prototype.hasOwnProperty.call(e,a)&&t.indexOf(a)<0&&(r[a]=e[a]);if(null!=e&&"function"==typeof Object.getOwnPropertySymbols)for(var n=0,a=Object.getOwnPropertySymbols(e);n<a.length;n++)t.indexOf(a[n])<0&&Object.prototype.propertyIsEnumerable.call(e,a[n])&&(r[a[n]]=e[a[n]]);return r}function W(){}function L(e){return!!(e||"").match(/\d/)}function O(e){return null==e}function D(e){return O(e)||"number"==typeof(t=e)&&isNaN(t)||"number"==typeof e&&!isFinite(e);var t}function h(e){return e.replace(/[-[\]/{}()*+?.\\^$|]/g,"\\$&")}function b(e,t,r){var r=function(e){switch(e){case"lakh":return/(\d+?)(?=(\d\d)+(\d)(?!\d))(\.\d+)?/g;case"wan":return/(\d)(?=(\d{4})+(?!\d))/g;default:return/(\d)(?=(\d{3})+(?!\d))/g}}(r),n=-1===(n=e.search(/[1-9]/))?e.length:n;return e.substring(0,n)+e.substring(n,e.length).replace(r,"$1"+t)}function S(e,t){void 0===t&&(t=!0);var r="-"===e[0],t=r&&t,e=(e=e.replace("-","")).split(".");return{beforeDecimal:e[0],afterDecimal:e[1]||"",hasNegation:r,addNegation:t}}function d(e,t,r){for(var n="",a=r?"0":"",o=0;o<=t-1;o++)n+=e[o]||a;return n}function a(e,t){return Array(t+1).join(e)}function C(e){var t,e=e+"",r="-"===e[0]?"-":"",e=(e=r?e.substring(1):e).split(/[eE]/g),n=e[0],e=e[1];return(e=Number(e))&&(e=1+e,t=(n=n.replace(".","")).length,e<0?n="0."+a("0",Math.abs(e))+n:t<=e?n+=a("0",e-t):n=(n.substring(0,e)||"0")+"."+n.substring(e)),r+n}function E(e,t,r){var n,a,o,u;return-1!==["","-"].indexOf(e)?e:(n=(-1!==e.indexOf(".")||r)&&t,a=(e=S(e)).beforeDecimal,u=e.afterDecimal,e=e.hasNegation,o=parseFloat("0."+(u||"0")),u=(u.length<=t?"0."+u:o.toFixed(t)).split("."),(e?"-":"")+a.split("").reverse().reduce(function(e,t,r){return e.length>r?(Number(e[0])+Number(t)).toString()+e.substring(1,e.length):t+e},u[0])+(n?".":"")+d(u[1]||"",t,r))}function U(e,t){var r;e.value=e.value,null!==e&&(e.createTextRange?((r=e.createTextRange()).move("character",t),r.select()):e.selectionStart||0===e.selectionStart?(e.focus(),e.setSelectionRange(t,t)):e.focus())}(t=M=M||{}).event="event",t.props="prop";r=function(e,t){for(var r=0,n=0,a=e.length,o=t.length;e[r]===t[r]&&r<a;)r++;for(;e[a-1-n]===t[o-1-n]&&r<o-n&&r<a-n;)n++;return{from:{start:r,end:a-n},to:{start:r,end:o-n}}},o=void 0;var r,n,o,$=function(){for(var e=[],t=arguments.length;t--;)e[t]=arguments[t];return n&&e.length===n.length&&e.every(function(e,t){return e===n[t]})||(n=e,o=r.apply(void 0,e)),o};function G(e){return Math.max(e.selectionStart,e.selectionEnd)}function y(e){return{from:{start:0,end:0},to:{start:0,end:e.length},lastValue:""}}function m(e,t){return"string"==typeof(e=void 0===e?" ":e)?e:e[t]||" "}function q(e){var t=e.currentValue,r=e.formattedValue,n=e.currentValueIndex,e=e.formattedValueIndex;return t[n]===r[e]}function Z(e,t,r,n){var a,o,u=e.length;if(e=t,a=0,o=u,t=Math.min(Math.max(e,a),o),"left"===n){for(;0<=t&&!r[t];)t--;-1===t&&(t=r.indexOf(!0))}else{for(;t<=u&&!r[t];)t++;u<t&&(t=r.lastIndexOf(!0))}return t=-1===t?u:t}function z(e){for(var t=Array.from({length:e.length+1}).map(function(){return!0}),r=0,n=t.length;r<n;r++)t[r]=Boolean(L(e[r])||L(e[r-1]));return t}function _(e,t,r,n,a,o){void 0===o&&(o=W);l=function(e,t){var r,t=D(e)?r="":(r="number"==typeof e||t?"number"==typeof e?C(e):e:a(e,void 0),n(r));return{formattedValue:t,numAsString:r}},(u=k.useRef(l)).current=l;var u,i=k.useRef(function(){for(var e=[],t=arguments.length;t--;)e[t]=arguments[t];return u.current.apply(u,e)}).current,l=k.useState(function(){return i(O(e)?t:e,r)}),s=l[0],c=l[1],l=e,f=r,d=(O(e)&&(l=s.numAsString,f=!0),i(l,f));return k.useMemo(function(){c(d)},[d.formattedValue]),[s,function(e,t){e.formattedValue!==s.formattedValue&&c({formattedValue:e.formattedValue,numAsString:e.value}),o(e,t)}]}function H(e){return e.replace(/[^0-9]/g,"")}function J(e){return e}function u(e){function s(e,t){D.current={formattedValue:e.formattedValue,numAsString:e.value},O(e,t)}function i(e,t,r){return Z(e,t,b(e),r)}var t=e.type,r=(void 0===t&&(t="text"),e.displayType),n=(void 0===r&&(r="input"),e.customInput),a=e.renderText,o=e.getInputRef,u=e.format,l=(void 0===u&&(u=J),e.removeFormatting),c=(void 0===l&&(l=H),e.defaultValue),f=e.valueIsNumericString,d=e.onValueChange,m=e.isAllowed,g=e.onChange,v=(void 0===g&&(g=W),e.onKeyDown),p=(void 0===v&&(v=W),e.onMouseUp),h=(void 0===p&&(p=W),e.onFocus),S=(void 0===h&&(h=W),e.onBlur),y=(void 0===S&&(S=W),e.value),b=e.getCaretBoundary,V=(void 0===b&&(b=z),e.isValidInputCharacter),w=(void 0===V&&(V=L),e.isCharacterSame),e=K(e,["type","displayType","customInput","renderText","getInputRef","format","removeFormatting","defaultValue","valueIsNumericString","onValueChange","isAllowed","onChange","onKeyDown","onMouseUp","onFocus","onBlur","value","getCaretBoundary","isValidInputCharacter","isCharacterSame"]),y=_(y,c,Boolean(f),u,l,d),c=y[0],x=c.formattedValue,N=c.numAsString,O=y[1],D=k.useRef({formattedValue:x,numAsString:N}),f=k.useState(!1),d=f[0],C=f[1],E=k.useRef(null),F=k.useRef({setCaretTimeout:null,focusTimeout:null}),j=(k.useEffect(function(){return C(!0),function(){clearTimeout(F.current.setCaretTimeout),clearTimeout(F.current.focusTimeout)}},[]),u),I=function(e,t){var r=parseFloat(t);return{formattedValue:e,value:t,floatValue:isNaN(r)?void 0:r}},A=function(e,t,r){0===e.selectionStart&&e.selectionEnd===e.value.length||(U(e,t),F.current.setCaretTimeout=setTimeout(function(){e.value===r&&e.selectionStart!==e.selectionEnd&&U(e,t)},0))},B=function(e,t,r){var n=b(t);return Z(t,function(e,t,r,n,a,o,u){void 0===u&&(u=q);for(var a=a.findIndex(function(e){return e}),a=e.slice(0,a),i=(t||r.startsWith(a)||(r=(t=a)+r,n+=a.length),r.length),l=e.length,s={},c=new Array(i),f=0;f<i;f++){c[f]=-1;for(var d=0,m=l;d<m;d++)if(u({currentValue:r,lastValue:t,formattedValue:e,currentValueIndex:f,formattedValueIndex:d})&&!0!==s[d]){s[c[f]=d]=!0;break}}for(var g=n;g<i&&(-1===c[g]||!o(r[g]));)g++;for(a=g===i||-1===c[g]?l:c[g],g=n-1;0<g&&-1===c[g];)g--;var v=-1===g||-1===c[g]?0:c[g]+1;return!(a<v)&&n-v<a-n?v:a}(t,x,e,r,n,V,w),n)},T=function(e){var t,r=e.formattedValue,n=(void 0===r&&(r=""),e.input),a=e.setCaretPosition,o=(void 0===a&&(a=!0),e.source),u=e.event,i=e.numAsString,l=e.caretPos;n&&(void 0===l&&a&&(e=e.inputValue||n.value,t=G(n),n.value=r,l=B(e,r,t)),n.value=r,a&&void 0!==l&&A(n,l,r)),r!==x&&s(I(r,i),{event:u,source:o})},R=(k.useEffect(function(){var e=D.current,t=e.formattedValue,e=e.numAsString;x===t||x===N&&t===e||s(I(x,N),{event:void 0,source:M.props})},[x,N]),E.current?G(E.current):void 0),c=(("undefined"!=typeof window?k.useLayoutEffect:k.useEffect)(function(){var e,t=E.current;x!==D.current.formattedValue&&t&&(e=B(D.current.formattedValue,x,R),t.value=x,A(t,e,x))},[x]),d&&!("undefined"==typeof navigator||navigator.platform&&/iPhone|iPod/.test(navigator.platform))?"numeric":void 0),y=Object.assign({inputMode:c},e,{type:t,value:x,onChange:function(e){var t,r,n,a,o,u,i=e.target.value;i=i,t=e,r=M.event,o=$(x,i),o=Object.assign(Object.assign({},o),{lastValue:x}),o=l(i,o),u=j(o),o=l(u,void 0),(m&&!m(I(u,o))?(a=G(n=t.target),a=B(i,x,a),n.value=x,A(n,a,x),1):(T({formattedValue:u,numAsString:o,inputValue:i,event:t,source:r,setCaretPosition:!0,input:t.target}),0))||g(e)},onKeyDown:function(e){var t,r=e.target,n=e.key,a=r.selectionStart,o=r.selectionEnd,u=r.value;void 0===u&&(u=""),"ArrowLeft"===n||"Backspace"===n?t=Math.max(a-1,0):"ArrowRight"===n?t=Math.min(a+1,u.length):"Delete"===n&&(t=a),void 0!==t&&a===o&&(a=t,"ArrowLeft"===n||"ArrowRight"===n?(a=i(u,t,"ArrowLeft"===n?"left":"right"))!==t&&e.preventDefault():"Delete"!==n||V(u[t])?"Backspace"!==n||V(u[t])||(a=i(u,t,"left")):a=i(u,t,"right"),a!==t&&A(r,a,u),e.isUnitTestRun&&A(r,a,u)),v(e)},onMouseUp:function(e){var t=e.target,r=t.selectionStart,n=t.selectionEnd,a=t.value;void 0===a&&(a=""),r===n&&(n=i(a,r))!==r&&A(t,n,a),p(e)},onFocus:function(a){a.persist&&a.persist();var o=a.target;E.current=o,F.current.focusTimeout=setTimeout(function(){var e=o.selectionStart,t=o.selectionEnd,r=o.value,n=i(r=void 0===r?"":r,e);n===e||0===e&&t===r.length||A(o,n,r),h(a)},0)},onBlur:function(e){E.current=null,clearTimeout(F.current.focusTimeout),clearTimeout(F.current.setCaretTimeout),S(e)}});return"text"===r?a?P.createElement(P.Fragment,null,a(x,e)||null):P.createElement("span",Object.assign({},e,{ref:o}),x):n?P.createElement(n,Object.assign({},y,{ref:o})):P.createElement("input",Object.assign({},y,{ref:o}))}function F(e,t){var r,n,a,o,u=t.decimalScale,i=t.fixedDecimalScale,l=t.prefix,s=(void 0===l&&(l=""),t.suffix),c=(void 0===s&&(s=""),t.allowNegative),f=t.thousandsGroupStyle;return void 0===f&&(f="thousand"),""!==e&&"-"!==e&&(r=(t=j(t)).thousandSeparator,t=t.decimalSeparator,n=0!==u&&-1!==e.indexOf(".")||u&&i,a=(c=S(e,c)).beforeDecimal,o=c.afterDecimal,c=c.addNegation,void 0!==u&&(o=d(o,u,!!i)),r&&(a=b(a,r,f)),l&&(a=l+a),s&&(o+=s),e=(a=c?"-"+a:a)+(n&&t||"")+o),e}function j(e){var t=e.decimalSeparator,r=e.thousandSeparator,e=e.allowedDecimalSeparators;return{decimalSeparator:t=void 0===t?".":t,thousandSeparator:r=!0===r?",":r,allowedDecimalSeparators:e=e||[t,"."]}}function I(e,t,r){void 0===t&&(t=y(e));var n,a,o,u,i=r.allowNegative,l=r.prefix,s=(void 0===l&&(l=""),r.suffix),c=(void 0===s&&(s=""),r.decimalScale),f=t.from,d=t.to,m=d.start,g=d.end,r=j(r),v=r.allowedDecimalSeparators,r=r.decimalSeparator,p=e[g]===r;return L(e)&&(e===l||e===s)&&""===t.lastValue||(g-m==1&&-1!==v.indexOf(e[m])&&(v=0===c?"":r,e=e.substring(0,m)+v+e.substring(m+1,e.length)),v=(c=function(e,t,r){var n=!1,a=!1,o=(l.startsWith("-")?n=!1:e.startsWith("--")?a=!(n=!1):s.startsWith("-")&&e.length===s.length?n=!1:"-"===e[0]&&(n=!0),n?1:0);return(o=a?2:o)&&(e=e.substring(o),t-=o,r-=o),{value:e,start:t,end:r,hasNegation:n}})(e,m,g),u=v.hasNegation,e=v.value,m=v.start,g=v.end,c=(v=c(t.lastValue,f.start,f.end)).start,t=v.end,v=v.value,n=e.substring(m,g),a=0,(e=!(e.length&&v.length&&(c>v.length-s.length||t<l.length))||n&&s.startsWith(n)?e:v).startsWith(l)?a+=l.length:m<l.length&&(a=m),g-=a,c=(e=e.substring(a)).length,t=e.length-s.length,e.endsWith(s)?c=t:(t<g||g>e.length-s.length)&&(c=g),e=e.substring(0,c),n=i,void 0===(v=u?"-"+e:e)&&(v=""),m=new RegExp("(-)"),a=new RegExp("(-)(.)*(-)"),m=m.test(v),a=a.test(v),v=v.replace(/-/g,""),o=(e=((e=v=m&&!a&&n?"-"+v:v).match((t=!0,new RegExp("(^-)|[0-9]|"+h(r),t?"g":void 0)))||[]).join("")).indexOf(r),c=(g=S(e=e.replace(new RegExp(h(r),"g"),function(e,t){return t===o?".":""}),i)).beforeDecimal,u=g.afterDecimal,m=g.addNegation,d.end-d.start<f.end-f.start&&""===c&&p&&!parseFloat(u)&&(e=m?"-":"")),e}function A(e,t){var r=t.prefix,t=(void 0===r&&(r=""),t.suffix),n=(void 0===t&&(t=""),Array.from({length:e.length+1}).map(function(){return!0})),a="-"===e[0],r=(n.fill(!1,0,r.length+(a?1:0)),e.length);return n.fill(!1,r-t.length+1,r+1),n}function i(a){(a=function(e){var t=(r=j(e)).thousandSeparator,r=r.decimalSeparator,n=e.prefix,a=(void 0===n&&(n=""),e.allowNegative);if(void 0===a&&(a=!0),t===r)throw new Error("\n Decimal separator can't be same as thousand separator.\n thousandSeparator: "+t+' (thousandSeparator = {true} is same as thousandSeparator = ",")\n decimalSeparator: '+r+" (default value for decimalSeparator is .)\n ");return n.startsWith("-")&&a&&(console.error("\n Prefix can't start with '-' when allowNegative is true.\n prefix: "+n+"\n allowNegative: "+a+"\n "),a=!1),Object.assign(Object.assign({},e),{allowNegative:a})}(a)).decimalSeparator,a.allowedDecimalSeparators,a.thousandsGroupStyle;function e(e){return F(e,a)}function t(e,t){return I(e,t,a)}function r(e){return!D(e)&&("number"==typeof e&&(e=C(e)),V&&"number"==typeof c)?E(e,c,Boolean(f)):e}var n=a.suffix,u=a.allowNegative,o=a.allowLeadingZeros,i=a.onKeyDown,l=(void 0===i&&(i=W),a.onBlur),s=(void 0===l&&(l=W),a.thousandSeparator),c=a.decimalScale,f=a.fixedDecimalScale,d=a.prefix,m=(void 0===d&&(d=""),a.defaultValue),g=a.value,v=a.valueIsNumericString,p=a.onValueChange,h=K(a,["decimalSeparator","allowedDecimalSeparators","thousandsGroupStyle","suffix","allowNegative","allowLeadingZeros","onKeyDown","onBlur","thousandSeparator","decimalScale","fixedDecimalScale","prefix","defaultValue","value","valueIsNumericString","onValueChange"]),S=j(a),y=S.decimalSeparator,b=S.allowedDecimalSeparators,S=O(g)?m:g,V=null!=v?v:(w=d,n=n,""===(S=S)||!(null!=w&&w.match(/\d/)||null!=n&&n.match(/\d/)||"string"!=typeof S||isNaN(Number(S)))),w=(O(g)?O(m)||(V=v||"number"==typeof m):V=v||"number"==typeof g,_(r(g),r(m),Boolean(V),e,t,p)),n=w[0],x=n.numAsString,S=n.formattedValue,N=w[1];return Object.assign(Object.assign({},h),{value:S,valueIsNumericString:!1,isValidInputCharacter:function(e){return e===y||L(e)},isCharacterSame:function(e){var t=e.currentValue,r=e.lastValue,n=e.formattedValue,a=e.currentValueIndex,e=e.formattedValueIndex,o=t[a],n=n[e],e=$(r,t).to;return!!(a>=e.start&&a<e.end&&b&&b.includes(o)&&n===y)||o===n},onValueChange:N,format:e,removeFormatting:t,getCaretBoundary:function(e){return A(e,a)},onKeyDown:function(e){var t=e.target,r=e.key,n=t.selectionStart,a=t.selectionEnd,o=t.value;void 0===o&&(o=""),n===a&&("Backspace"===r&&"-"===o[0]&&n===d.length+1&&u&&U(t,1),"Backspace"===r&&o[n-1]===y&&c&&f&&(U(t,n-1),e.preventDefault()),null!=b&&b.includes(r)&&o[n]===y&&U(t,n+1),a=!0===s?",":s,"Backspace"===r&&o[n-1]===a&&U(t,n-1),"Delete"===r&&o[n]===a&&U(t,n+1)),i(e)},onBlur:function(e){var t,r,n=x;n.match(/\d/g)||(n=""),o||(n=(r=n)&&((t="-"===r[0])?"-":"")+((t=(r=t?r.substring(1,r.length):r).split("."))[0].replace(/^0+/,"")||"0")+((t=t[1]||"")?"."+t:"")),(n=f&&c?E(n,c,f):n)!==x&&(r=F(n,a),N({formattedValue:r,value:n,floatValue:parseFloat(n)},{event:e,source:M.event})),l(e)}})}function g(e,t){var r=t.format,n=t.allowEmptyFormatting,a=t.mask,o=t.patternChar;if(void 0===o&&(o="#"),""===e&&!n)return"";for(var u=0,i=r.split(""),l=0,s=r.length;l<s;l++)r[l]===o&&(i[l]=e[u]||m(a,u),u+=1);return i.join("")}function v(e,t,r){void 0===t&&(t=y(e));function a(e){return u[e]===i}function n(e,t){for(var r="",n=0;n<e.length;n++)a(t+n)&&L(e[n])&&(r+=e[n]);return r}function o(e){return e.replace(/[^0-9]/g,"")}var u=r.format,i=r.patternChar,r=(void 0===i&&(i="#"),t.from),l=t.to,t=t.lastValue;void 0===t&&(t="");if(!u.match(/\d/))return o(e);if(""===t&&e.length===u.length){for(var s="",c=0;c<e.length;c++)if(a(c))L(e[c])&&(s+=e[c]);else if(e[c]!==u[c])return o(e);return s}var f=t.substring(0,r.start),l=e.substring(l.start,l.end),t=t.substring(r.end);return""+n(f,0)+o(l)+n(t,r.end)}function p(n,e){for(var t=e.format,a=e.mask,o=e.patternChar,r=(void 0===o&&(o="#"),Array.from({length:n.length+1}).map(function(){return!0})),u=0,i=-1,l={},s=(t.split("").forEach(function(e,t){var r=void 0;e===o&&(r=m(a,++u-1),-1===i&&n[t]===r&&(i=t)),l[t]=r}),function(e){return t[e]===o&&n[e]!==l[e]}),c=0,f=r.length;c<f;c++)r[c]=c===i||s(c)||s(c-1);return r[t.indexOf(o)]=!0,r}function l(t){t.mask,t.allowEmptyFormatting;var l=t.format,e=t.inputMode,s=(void 0===e&&(e="numeric"),t.onKeyDown),c=(void 0===s&&(s=W),t.patternChar),r=(void 0===c&&(c="#"),t.value),n=t.defaultValue,a=t.valueIsNumericString,o=K(t,["mask","allowEmptyFormatting","format","inputMode","onKeyDown","patternChar","value","defaultValue","valueIsNumericString"]),u=t;if((u=u.mask)&&("string"===u?u:u.toString()).match(/\d/g))throw new Error("Mask "+u+" should not contain numeric character;");function f(e){return p(e,t)}var u=O(r)?n:r,a=null!=a?a:(a=l,""===(u=u)||!(null!=a&&a.match(/\d/)||"string"!=typeof u||!u.match(/^\d+$/)&&""!==u)),i=Object.assign(Object.assign({},t),{valueIsNumericString:a});return Object.assign(Object.assign({},o),{value:r,defaultValue:n,valueIsNumericString:a,inputMode:e,format:function(e){return g(e,i)},removeFormatting:function(e,t){return v(e,t,i)},getCaretBoundary:f,onKeyDown:function(e){var t=e.key,r=e.target,n=r.selectionStart,a=r.selectionEnd,o=r.value;if(n===a){var u=n;if("Backspace"===t||"Delete"===t){a="right";if("Backspace"===t){for(;0<u&&l[u-1]!==c;)u--;a="left"}else{for(var i=l.length;u<i&&l[u]!==c;)u++;a="right"}u=Z(o,u,f(o),a)}else l[u]!==c&&"ArrowLeft"!==t&&"ArrowRight"!==t&&(u=Z(o,u+1,f(o),"right"));u!==n&&U(r,u)}s(e)}})}e.NumberFormatBase=u,e.NumericFormat=function(e){return e=i(e),P.createElement(u,Object.assign({},e))},e.PatternFormat=function(e){return e=l(e),P.createElement(u,Object.assign({},e))},e.getNumericCaretBoundary=A,e.getPatternCaretBoundary=p,e.numericFormatter=F,e.patternFormatter=g,e.removeNumericFormat=I,e.removePatternFormat=v,e.useNumericFormat=i,e.usePatternFormat=l,Object.defineProperty(e,"__esModule",{value:!0})}); |
{ | ||
"name": "react-number-format", | ||
"description": "React component to format number in an input or as a text.", | ||
"version": "5.2.2", | ||
"version": "5.3.0", | ||
"main": "dist/react-number-format.cjs.js", | ||
@@ -6,0 +6,0 @@ "module": "dist/react-number-format.es.js", |
@@ -5,13 +5,17 @@ [![Actions Status](https://github.com/s-yadav/react-number-format/workflows/CI/badge.svg)](https://github.com/s-yadav/react-number-format/actions) | ||
React Number format is a input formatter library with a sophisticated and lightweight caret engine. | ||
React Number Format is an input-formatter library with a sophisticated and light weight caret engine. It ensures that a user can only enter text that meets specific numeric or string patterns, and formats the input value for display. | ||
### Features | ||
1. Prefix, suffix and thousand separator. | ||
2. Custom pattern formatting. | ||
3. Masking. | ||
4. Custom formatting handler. | ||
5. Format number in an input or format as a simple text. | ||
6. Fully customizable | ||
1. Prefix, suffix and thousands separator. | ||
1. Input Masking. | ||
1. Format number in an input or format as a simple text. | ||
1. Custom pattern formatting. | ||
1. Custom formatting handler. | ||
1. Fully customizable | ||
### Demos | ||
See the many DEMO sections in [the documentation](https://s-yadav.github.io/react-number-format/docs/props). | ||
### Install | ||
@@ -33,2 +37,7 @@ | ||
### Documentation | ||
Read the full documentation here | ||
[https://s-yadav.github.io/react-number-format/docs/intro](https://s-yadav.github.io/react-number-format/docs/intro) | ||
#### ES6 | ||
@@ -42,2 +51,4 @@ | ||
NumericFormat Props: [https://s-yadav.github.io/react-number-format/docs/numeric_format](https://s-yadav.github.io/react-number-format/docs/numeric_format) | ||
Pattern Format | ||
@@ -49,6 +60,4 @@ | ||
Read the full documentation here | ||
PatternFormat Props: [https://s-yadav.github.io/react-number-format/docs/pattern_format](https://s-yadav.github.io/react-number-format/docs/pattern_format) | ||
[https://s-yadav.github.io/react-number-format/docs/intro](https://s-yadav.github.io/react-number-format/docs/intro) | ||
### Migrate from v4 to v5 | ||
@@ -55,0 +64,0 @@ |
@@ -43,2 +43,9 @@ import React, { SyntheticEvent } from 'react'; | ||
export declare type OnValueChange = (values: NumberFormatValues, sourceInfo: SourceInfo) => void; | ||
export declare type IsCharacterSame = (compareProps: { | ||
currentValue: string; | ||
lastValue: string; | ||
formattedValue: string; | ||
currentValueIndex: number; | ||
formattedValueIndex: number; | ||
}) => boolean; | ||
declare type NumberFormatBase = { | ||
@@ -64,2 +71,3 @@ type?: 'text' | 'tel' | 'password'; | ||
isValidInputCharacter?: (character: string) => boolean; | ||
isCharacterSame?: IsCharacterSame; | ||
}; | ||
@@ -66,0 +74,0 @@ export declare type NumberFormatBaseProps<BaseType = InputAttributes> = NumberFormatProps<NumberFormatBase, BaseType>; |
@@ -1,4 +0,5 @@ | ||
import { NumberFormatBaseProps, FormatInputValueFunction, OnValueChange } from './types'; | ||
import { NumberFormatBaseProps, FormatInputValueFunction, OnValueChange, IsCharacterSame } from './types'; | ||
export declare function noop(): void; | ||
export declare function returnTrue(): boolean; | ||
export declare function memoizeOnce<T extends unknown[], R extends unknown>(cb: (...args: T) => R): (...args: T) => R; | ||
export declare function charIsNumber(char?: string): boolean; | ||
@@ -42,3 +43,3 @@ export declare function isNil(val: any): val is null | undefined; | ||
}; | ||
export declare function findChangeRange(prevValue: string, newValue: string): { | ||
export declare const findChangeRange: (prevValue: string, newValue: string) => { | ||
from: { | ||
@@ -68,3 +69,10 @@ start: number; | ||
export declare function getMaskAtIndex(mask: string | string[], index: number): string; | ||
export declare function getCaretPosition(newFormattedValue: string, lastFormattedValue: string, curValue: string, curCaretPos: number, boundary: boolean[], isValidInputCharacter: (char: string) => boolean): any; | ||
export declare function getCaretPosition(newFormattedValue: string, lastFormattedValue: string, curValue: string, curCaretPos: number, boundary: boolean[], isValidInputCharacter: (char: string) => boolean, | ||
/** | ||
* format function can change the character, the caret engine relies on mapping old value and new value | ||
* In such case if character is changed, parent can tell which chars are equivalent | ||
* Some example, all allowedDecimalCharacters are updated to decimalCharacters, 2nd case if user is coverting | ||
* number to different numeric system. | ||
*/ | ||
isCharacterSame?: IsCharacterSame): any; | ||
export declare function getCaretPosInBoundary(value: string, caretPos: number, boundary: boolean[], direction?: string): number; | ||
@@ -71,0 +79,0 @@ export declare function caretUnknownFormatBoundary(formattedValue: string): boolean[]; |
Sorry, the diff of this file is too big to display
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
249163
28
4967
83