react-number-format
Advanced tools
Comparing version 4.6.2 to 4.6.3
/** | ||
* react-number-format - 4.6.2 | ||
* react-number-format - 4.6.3 | ||
* Author : Sudhanshu Yadav | ||
@@ -454,2 +454,3 @@ * Copyright (c) 2016, 2021 to Sudhanshu Yadav, released under the MIT license. | ||
clearTimeout(this.focusTimeout); | ||
clearTimeout(this.caretPositionTimeout); | ||
}; | ||
@@ -613,3 +614,3 @@ | ||
setCaretPosition(el, caretPos); | ||
setTimeout(function () { | ||
this.caretPositionTimeout = setTimeout(function () { | ||
if (el.value === currentValue) { setCaretPosition(el, caretPos); } | ||
@@ -1235,2 +1236,3 @@ }, 0); | ||
clearTimeout(this.focusTimeout); | ||
clearTimeout(this.caretPositionTimeout); | ||
@@ -1237,0 +1239,0 @@ if (!format) { |
/** | ||
* react-number-format - 4.6.2 | ||
* react-number-format - 4.6.3 | ||
* Author : Sudhanshu Yadav | ||
@@ -450,2 +450,3 @@ * Copyright (c) 2016, 2021 to Sudhanshu Yadav, released under the MIT license. | ||
clearTimeout(this.focusTimeout); | ||
clearTimeout(this.caretPositionTimeout); | ||
}; | ||
@@ -609,3 +610,3 @@ | ||
setCaretPosition(el, caretPos); | ||
setTimeout(function () { | ||
this.caretPositionTimeout = setTimeout(function () { | ||
if (el.value === currentValue) { setCaretPosition(el, caretPos); } | ||
@@ -1231,2 +1232,3 @@ }, 0); | ||
clearTimeout(this.focusTimeout); | ||
clearTimeout(this.caretPositionTimeout); | ||
@@ -1233,0 +1235,0 @@ if (!format) { |
/** | ||
* react-number-format - 4.6.2 | ||
* react-number-format - 4.6.3 | ||
* Author : Sudhanshu Yadav | ||
@@ -456,2 +456,3 @@ * Copyright (c) 2016, 2021 to Sudhanshu Yadav, released under the MIT license. | ||
clearTimeout(this.focusTimeout); | ||
clearTimeout(this.caretPositionTimeout); | ||
}; | ||
@@ -615,3 +616,3 @@ | ||
setCaretPosition(el, caretPos); | ||
setTimeout(function () { | ||
this.caretPositionTimeout = setTimeout(function () { | ||
if (el.value === currentValue) { setCaretPosition(el, caretPos); } | ||
@@ -1237,2 +1238,3 @@ }, 0); | ||
clearTimeout(this.focusTimeout); | ||
clearTimeout(this.caretPositionTimeout); | ||
@@ -1239,0 +1241,0 @@ if (!format) { |
@@ -1,1 +0,1 @@ | ||
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e(require("react")):"function"==typeof define&&define.amd?define(["react"],e):(t=t||self).NumberFormat=e(t.React)}(this,function(g){"use strict";g=g&&g.hasOwnProperty("default")?g.default:g;function n(){}function o(){}o.resetWarningCache=n;var t,e=(function(t){t.exports=function(){function t(t,e,r,n,o,a){if("SECRET_DO_NOT_PASS_THIS_OR_YOU_WILL_BE_FIRED"!==a){var i=new Error("Calling PropTypes validators directly is not supported by the `prop-types` package. Use PropTypes.checkPropTypes() to call them. Read more at http://fb.me/use-check-prop-types");throw i.name="Invariant Violation",i}}function e(){return t}var r={array:t.isRequired=t,bool:t,func:t,number:t,object:t,string:t,symbol:t,any:t,arrayOf:e,element:t,elementType:t,instanceOf:e,node:t,objectOf:e,oneOf:e,oneOfType:e,shape:e,exact:e,checkPropTypes:o,resetWarningCache:n};return r.PropTypes=r}()}(t={exports:{}},t.exports),t.exports);function r(){}function c(t){return!!(t||"").match(/\d/)}function f(t){return null==t}function i(t){return t.replace(/[-[\]/{}()*+?.\\^$|]/g,"\\$&")}function R(t,e){void 0===e&&(e=!0);var r="-"===t[0],n=r&&e,o=(t=t.replace("-","")).split(".");return{beforeDecimal:o[0],afterDecimal:o[1]||"",hasNagation:r,addNegation:n}}function d(t,e,r){for(var n="",o=r?"0":"",a=0;a<=e-1;a++)n+=t[a]||o;return n}function p(t,e){return Array(e+1).join(t)}function a(t,e){if(t.value=t.value,null!==t){if(t.createTextRange){var r=t.createTextRange();return r.move("character",e),r.select(),!0}return t.selectionStart||0===t.selectionStart?(t.focus(),t.setSelectionRange(e,e),!0):(t.focus(),!1)}}function h(t,e,r){return Math.min(Math.max(t,e),r)}function m(t){return Math.max(t.selectionStart,t.selectionEnd)}var v={thousandSeparator:e.oneOfType([e.string,e.oneOf([!0])]),decimalSeparator:e.string,allowedDecimalSeparators:e.arrayOf(e.string),thousandsGroupStyle:e.oneOf(["thousand","lakh","wan"]),decimalScale:e.number,fixedDecimalScale:e.bool,displayType:e.oneOf(["input","text"]),prefix:e.string,suffix:e.string,format:e.oneOfType([e.string,e.func]),removeFormatting:e.func,mask:e.oneOfType([e.string,e.arrayOf(e.string)]),value:e.oneOfType([e.number,e.string]),defaultValue:e.oneOfType([e.number,e.string]),isNumericString:e.bool,customInput:e.elementType,allowNegative:e.bool,allowEmptyFormatting:e.bool,allowLeadingZeros:e.bool,onValueChange:e.func,onKeyDown:e.func,onMouseUp:e.func,onChange:e.func,onFocus:e.func,onBlur:e.func,type:e.oneOf(["text","tel","password"]),isAllowed:e.func,renderText:e.func,getInputRef:e.oneOfType([e.func,e.shape({current:e.any})])},s={displayType:"input",decimalSeparator:".",thousandsGroupStyle:"thousand",fixedDecimalScale:!1,prefix:"",suffix:"",allowNegative:!0,allowEmptyFormatting:!1,allowLeadingZeros:!1,isNumericString:!1,type:"text",onValueChange:r,onChange:r,onKeyDown:r,onMouseUp:r,onFocus:r,onBlur:r,isAllowed:function(){return!0}},u=function(n){function t(t){n.call(this,t);var e=t.defaultValue;this.validateProps();var r=this.formatValueProp(e);this.state={value:r,numAsString:this.removeFormatting(r),mounted:!1},this.selectionBeforeInput={selectionStart:0,selectionEnd:0},this.onChange=this.onChange.bind(this),this.onKeyDown=this.onKeyDown.bind(this),this.onMouseUp=this.onMouseUp.bind(this),this.onFocus=this.onFocus.bind(this),this.onBlur=this.onBlur.bind(this)}return n&&(t.__proto__=n),((t.prototype=Object.create(n&&n.prototype)).constructor=t).prototype.componentDidMount=function(){this.setState({mounted:!0})},t.prototype.componentDidUpdate=function(t){this.updateValueIfRequired(t)},t.prototype.componentWillUnmount=function(){clearTimeout(this.focusTimeout)},t.prototype.updateValueIfRequired=function(t){var e=this.props,r=this.state,n=this.focusedElm,o=r.value,a=r.numAsString;if(void 0===a&&(a=""),t!==e){this.validateProps();var i=this.formatNumString(a),s=f(e.value)?i:this.formatValueProp(),u=this.removeFormatting(s),l=parseFloat(u),p=parseFloat(a);(isNaN(l)&&isNaN(p)||l===p)&&i===o&&(null!==n||s===o)||this.updateValue({formattedValue:s,numAsString:u,input:n})}},t.prototype.getFloatString=function(t){void 0===t&&(t="");var e=this.props.decimalScale,r=this.getSeparators().decimalSeparator,n=this.getNumberRegex(!0),o="-"===t[0];o&&(t=t.replace("-","")),r&&0===e&&(t=t.split(r)[0]);var a=(t=(t.match(n)||[]).join("").replace(r,".")).indexOf(".");return-1!==a&&(t=t.substring(0,a)+"."+t.substring(a+1,t.length).replace(new RegExp(i(r),"g"),"")),o&&(t="-"+t),t},t.prototype.getNumberRegex=function(t,e){var r=this.props,n=r.format,o=r.decimalScale,a=this.getSeparators().decimalSeparator;return new RegExp("\\d"+(!a||0===o||e||n?"":"|"+i(a)),t?"g":void 0)},t.prototype.getSeparators=function(){var t=this.props.decimalSeparator,e=this.props,r=e.thousandSeparator,n=e.allowedDecimalSeparators;return!0===r&&(r=","),{decimalSeparator:t,thousandSeparator:r,allowedDecimalSeparators:n=n||[t,"."]}},t.prototype.getMaskAtIndex=function(t){var e=this.props.mask;return void 0===e&&(e=" "),"string"==typeof e?e:e[t]||" "},t.prototype.getValueObject=function(t,e){var r=parseFloat(e);return{formattedValue:t,value:e,floatValue:isNaN(r)?void 0:r}},t.prototype.validateProps=function(){var t=this.props.mask,e=this.getSeparators(),r=e.decimalSeparator,n=e.thousandSeparator;if(r===n)throw new Error("\n Decimal separator can't be same as thousand separator.\n thousandSeparator: "+n+' (thousandSeparator = {true} is same as thousandSeparator = ",")\n decimalSeparator: '+r+" (default value for decimalSeparator is .)\n ");if(t&&("string"===t?t:t.toString()).match(/\d/g))throw new Error("\n Mask "+t+" should not contain numeric character;\n ")},t.prototype.setPatchedCaretPosition=function(t,e,r){a(t,e),setTimeout(function(){t.value===r&&a(t,e)},0)},t.prototype.correctCaretPosition=function(t,e,r){var n=this.props,o=n.prefix,a=n.suffix,i=n.format;if(""===t)return 0;if(e=h(e,0,t.length),!i){var s="-"===t[0];return h(e,o.length+(s?1:0),t.length-a.length)}if("function"==typeof i)return e;if("#"===i[e]&&c(t[e]))return e;if("#"===i[e-1]&&c(t[e-1]))return e;var u=i.indexOf("#");e=h(e,u,i.lastIndexOf("#")+1);for(var l=i.substring(e,i.length).indexOf("#"),p=e,f=e+(-1===l?0:l);u<p&&("#"!==i[p]||!c(t[p]));)p-=1;return!c(t[f])||"left"===r&&e!==u||e-p<f-e?c(t[p])?p+1:p:f},t.prototype.getCaretPosition=function(t,e,r){var n,o,a=this.props.format,i=this.state.value,s=this.getNumberRegex(!0),u=(t.match(s)||[]).join(""),l=(e.match(s)||[]).join("");for(o=n=0;o<r;o++){var p=t[o]||"",f=e[n]||"";if((p.match(s)||p===f)&&("0"!==p||!f.match(s)||"0"===f||u.length===l.length)){for(;p!==e[n]&&n<e.length;)n++;n++}}return"string"!=typeof a||i||(n=e.length),n=this.correctCaretPosition(e,n)},t.prototype.removePrefixAndSuffix=function(t){var e=this.props,r=e.format,n=e.prefix,o=e.suffix;if(!r&&t){var a="-"===t[0];a&&(t=t.substring(1,t.length));var i=(t=n&&0===t.indexOf(n)?t.substring(n.length,t.length):t).lastIndexOf(o);t=o&&-1!==i&&i===t.length-o.length?t.substring(0,i):t,a&&(t="-"+t)}return t},t.prototype.removePatternFormatting=function(t){for(var e=this.props.format.split("#").filter(function(t){return""!==t}),r=0,n="",o=0,a=e.length;o<=a;o++){var i=e[o]||"",s=o===a?t.length:t.indexOf(i,r);if(-1===s){n=t;break}n+=t.substring(r,s),r=s+i.length}return(n.match(/\d/g)||[]).join("")},t.prototype.removeFormatting=function(t){var e=this.props,r=e.format,n=e.removeFormatting;return t?t=r?"string"==typeof r?this.removePatternFormatting(t):"function"==typeof n?n(t):(t.match(/\d/g)||[]).join(""):(t=this.removePrefixAndSuffix(t),this.getFloatString(t)):t},t.prototype.formatWithPattern=function(t){for(var e=this.props.format,r=0,n=e.split(""),o=0,a=e.length;o<a;o++)"#"===e[o]&&(n[o]=t[r]||this.getMaskAtIndex(r),r+=1);return n.join("")},t.prototype.formatAsNumber=function(t){var e=this.props,r=e.decimalScale,n=e.fixedDecimalScale,o=e.prefix,a=e.suffix,i=e.allowNegative,s=e.thousandsGroupStyle,u=this.getSeparators(),l=u.thousandSeparator,p=u.decimalSeparator,f=-1!==t.indexOf(".")||r&&n,c=R(t,i),h=c.beforeDecimal,g=c.afterDecimal,m=c.addNegation;return void 0!==r&&(g=d(g,r,n)),l&&(h=function(t,e,r){var n=function(t){switch(t){case"lakh":return/(\d+?)(?=(\d\d)+(\d)(?!\d))(\.\d+)?/g;case"wan":return/(\d)(?=(\d{4})+(?!\d))/g;case"thousand":default:return/(\d)(?=(\d{3})+(?!\d))/g}}(r),o=t.search(/[1-9]/);return o=-1===o?t.length:o,t.substring(0,o)+t.substring(o,t.length).replace(n,"$1"+e)}(h,l,s)),o&&(h=o+h),a&&(g+=a),m&&(h="-"+h),t=h+(f&&p||"")+g},t.prototype.formatNumString=function(t){void 0===t&&(t="");var e=this.props,r=e.format,n=e.allowEmptyFormatting,o=t;return o=""!==t||n?"-"!==t||r?"string"==typeof r?this.formatWithPattern(o):"function"==typeof r?r(o):this.formatAsNumber(o):"-":""},t.prototype.formatValueProp=function(t){var e=this.props,r=e.format,n=e.decimalScale,o=e.fixedDecimalScale,a=e.allowEmptyFormatting,i=this.props,s=i.value,u=i.isNumericString,l=!(s=f(s)?t:s)&&0!==s;return l&&a&&(s=""),l&&!a?"":("number"==typeof s&&(s=function(t){var e="-"===(t+="")[0]?"-":"";e&&(t=t.substring(1));var r=t.split(/[eE]/g),n=r[0],o=r[1];if(!(o=Number(o)))return e+n;var a=1+o,i=(n=n.replace(".","")).length;return a<0?n="0."+p("0",Math.abs(a))+n:i<=a?n+=p("0",a-i):n=(n.substring(0,a)||"0")+"."+n.substring(a),e+n}(s),u=!0),"Infinity"===s&&u&&(s=""),u&&!r&&"number"==typeof n&&(s=function(t,e,r){if(-1!==["","-"].indexOf(t))return t;var n=-1!==t.indexOf(".")&&e,o=R(t),a=o.beforeDecimal,i=o.afterDecimal,s=o.hasNagation,u=parseFloat("0."+(i||"0")),l=(i.length<=e?"0."+i:u.toFixed(e)).split(".");return(s?"-":"")+a.split("").reverse().reduce(function(t,e,r){return t.length>r?(Number(t[0])+Number(e)).toString()+t.substring(1,t.length):e+t},l[0])+(n?".":"")+d(l[1]||"",Math.min(e,i.length),r)}(s,n,o)),u?this.formatNumString(s):this.formatInput(s))},t.prototype.formatNegation=function(t){void 0===t&&(t="");var e=this.props.allowNegative,r=new RegExp("(-)"),n=new RegExp("(-)(.)*(-)"),o=r.test(t),a=n.test(t);return t=t.replace(/-/g,""),o&&!a&&e&&(t="-"+t),t},t.prototype.formatInput=function(t){return void 0===t&&(t=""),this.props.format||(t=this.removePrefixAndSuffix(t),t=this.formatNegation(t)),t=this.removeFormatting(t),this.formatNumString(t)},t.prototype.isCharacterAFormat=function(t,e){var r=this.props,n=r.format,o=r.prefix,a=r.suffix,i=r.decimalScale,s=r.fixedDecimalScale,u=this.getSeparators().decimalSeparator;return"string"==typeof n&&"#"!==n[t]||!(n||!(t<o.length||t>=e.length-a.length||i&&s&&e[t]===u))},t.prototype.correctInputValue=function(t,r,e){var n=this,o=this.props,a=o.format,i=o.allowNegative,s=o.prefix,u=o.suffix,l=o.decimalScale,p=this.getSeparators(),f=p.allowedDecimalSeparators,c=p.decimalSeparator,h=this.state.numAsString||"",g=this.selectionBeforeInput,m=g.selectionStart,d=g.selectionEnd,v=function(t,e){for(var r=0,n=0,o=t.length,a=e.length;t[r]===e[r]&&r<o;)r++;for(;t[o-1-n]===e[a-1-n]&&r<a-n&&r<o-n;)n++;return{start:r,end:o-n}}(r,e),y=v.start,S=v.end;if(!a&&y===S&&-1!==f.indexOf(e[m])){var x=0===l?"":c;return e.substr(0,m)+x+e.substr(m+1,e.length)}var b=a?0:s.length,w=r.length-(a?0:u.length);if(e.length>r.length||!e.length||y===S||0===m&&d===r.length||0===y&&S===r.length||m===b&&d===w)return e;var P=r.substr(y,S-y);if(!![].concat(P).find(function(t,e){return n.isCharacterAFormat(e+y,r)})){var O=r.substr(y),N={},F=[];[].concat(O).forEach(function(t,e){n.isCharacterAFormat(e+y,r)?N[e]=t:e>P.length-1&&F.push(t)}),Object.keys(N).forEach(function(t){F.length>t?F.splice(t,0,N[t]):F.push(N[t])}),e=r.substr(0,y)+F.join("")}if(!a){var C=this.removeFormatting(e),D=R(C,i),E=D.beforeDecimal,T=D.afterDecimal,V=D.addNegation,A=t<e.indexOf(c)+1;if(C.length<h.length&&A&&""===E&&!parseFloat(T))return V?"-":""}return e},t.prototype.updateValue=function(t){var e=t.formattedValue,r=t.input,n=t.setCaretPosition;void 0===n&&(n=!0);var o=t.numAsString,a=t.caretPos,i=this.props.onValueChange,s=this.state.value;if(r)if(n){if(!a){var u=t.inputValue||r.value,l=m(r);r.value=e,a=this.getCaretPosition(u,e,l)}this.setPatchedCaretPosition(r,a,e)}else r.value=e;void 0===o&&(o=this.removeFormatting(e)),e!==s&&(this.setState({value:e,numAsString:o}),i(this.getValueObject(e,o)))},t.prototype.onChange=function(t){var e=t.target,r=e.value,n=this.state,o=this.props,a=o.isAllowed,i=n.value||"",s=m(e);r=this.correctInputValue(s,i,r);var u=this.formatInput(r)||"",l=this.removeFormatting(u),p=a(this.getValueObject(u,l));p||(u=i),this.updateValue({formattedValue:u,numAsString:l,inputValue:r,input:e}),p&&o.onChange(t)},t.prototype.onBlur=function(t){var e=this.props,r=this.state,n=e.format,o=e.onBlur,a=e.allowLeadingZeros,i=r.numAsString,s=r.value;if(this.focusedElm=null,clearTimeout(this.focusTimeout),!n){isNaN(parseFloat(i))&&(i=""),a||(i=function(t){if(!t)return t;var e="-"===t[0];e&&(t=t.substring(1,t.length));var r=t.split("."),n=r[0].replace(/^0+/,"")||"0",o=r[1]||"";return(e?"-":"")+n+(o?"."+o:"")}(i));var u=this.formatNumString(i);if(u!==s)return this.updateValue({formattedValue:u,numAsString:i,input:t.target,setCaretPosition:!1}),void o(t)}o(t)},t.prototype.onKeyDown=function(t){var e,r=t.target,n=t.key,o=r.selectionStart,a=r.selectionEnd,i=r.value;void 0===i&&(i="");var s=this.props,u=s.decimalScale,l=s.fixedDecimalScale,p=s.prefix,f=s.suffix,c=s.format,h=s.onKeyDown,g=void 0!==u&&l,m=this.getNumberRegex(!1,g),d=new RegExp("-"),v="string"==typeof c;if(this.selectionBeforeInput={selectionStart:o,selectionEnd:a},"ArrowLeft"===n||"Backspace"===n?e=o-1:"ArrowRight"===n?e=o+1:"Delete"===n&&(e=o),void 0!==e&&o===a){var y=e,S=v?c.indexOf("#"):p.length,x=v?c.lastIndexOf("#")+1:i.length-f.length;if("ArrowLeft"===n||"ArrowRight"===n){var b="ArrowLeft"===n?"left":"right";y=this.correctCaretPosition(i,e,b)}else if("Delete"!==n||m.test(i[e])||d.test(i[e])){if("Backspace"===n&&!m.test(i[e]))if(o<=S+1&&"-"===i[0]&&void 0===c){var w=i.substring(1);this.updateValue({formattedValue:w,caretPos:y,input:r})}else if(!d.test(i[e])){for(;!m.test(i[y-1])&&S<y;)y--;y=this.correctCaretPosition(i,y,"left")}}else for(;!m.test(i[y])&&y<x;)y++;(y!==e||e<S||x<e)&&(t.preventDefault(),this.setPatchedCaretPosition(r,y,i)),t.isUnitTestRun&&this.setPatchedCaretPosition(r,y,i),h(t)}else h(t)},t.prototype.onMouseUp=function(t){var e=t.target,r=e.selectionStart,n=e.selectionEnd,o=e.value;if(void 0===o&&(o=""),r===n){var a=this.correctCaretPosition(o,r);a!==r&&this.setPatchedCaretPosition(e,a,o)}this.props.onMouseUp(t)},t.prototype.onFocus=function(a){var i=this;a.persist(),this.focusedElm=a.target,this.focusTimeout=setTimeout(function(){var t=a.target,e=t.selectionStart,r=t.selectionEnd,n=t.value;void 0===n&&(n="");var o=i.correctCaretPosition(n,e);o===e||0===e&&r===n.length||i.setPatchedCaretPosition(t,o,n),i.props.onFocus(a)},0)},t.prototype.render=function(){var t=this.props,e=t.type,r=t.displayType,n=t.customInput,o=t.renderText,a=t.getInputRef,i=t.format,s=this.state,u=s.value,l=s.mounted,p=function(e,r){var n={};return Object.keys(e).forEach(function(t){r[t]||(n[t]=e[t])}),n}(this.props,v),f=l&&function(t){return t||!(navigator.platform&&/iPhone|iPod/.test(navigator.platform))}(i)?"numeric":void 0,c=Object.assign({inputMode:f},p,{type:e,value:u,onChange:this.onChange,onKeyDown:this.onKeyDown,onMouseUp:this.onMouseUp,onFocus:this.onFocus,onBlur:this.onBlur});if("text"===r)return o?o(u,p)||null:g.createElement("span",Object.assign({},p,{ref:a}),u);if(n){var h=n;return g.createElement(h,Object.assign({},c,{ref:a}))}return g.createElement("input",Object.assign({},c,{ref:a}))},t}(g.Component);return u.propTypes=v,u.defaultProps=s,u}); | ||
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e(require("react")):"function"==typeof define&&define.amd?define(["react"],e):(t=t||self).NumberFormat=e(t.React)}(this,function(m){"use strict";m=m&&m.hasOwnProperty("default")?m.default:m;function o(){}function n(){}n.resetWarningCache=o;var t,e=(function(t){t.exports=function(){function t(t,e,r,o,n,a){if("SECRET_DO_NOT_PASS_THIS_OR_YOU_WILL_BE_FIRED"!==a){var i=new Error("Calling PropTypes validators directly is not supported by the `prop-types` package. Use PropTypes.checkPropTypes() to call them. Read more at http://fb.me/use-check-prop-types");throw i.name="Invariant Violation",i}}function e(){return t}var r={array:t.isRequired=t,bool:t,func:t,number:t,object:t,string:t,symbol:t,any:t,arrayOf:e,element:t,elementType:t,instanceOf:e,node:t,objectOf:e,oneOf:e,oneOfType:e,shape:e,exact:e,checkPropTypes:n,resetWarningCache:o};return r.PropTypes=r}()}(t={exports:{}},t.exports),t.exports);function r(){}function c(t){return!!(t||"").match(/\d/)}function f(t){return null==t}function i(t){return t.replace(/[-[\]/{}()*+?.\\^$|]/g,"\\$&")}function R(t,e){void 0===e&&(e=!0);var r="-"===t[0],o=r&&e,n=(t=t.replace("-","")).split(".");return{beforeDecimal:n[0],afterDecimal:n[1]||"",hasNagation:r,addNegation:o}}function d(t,e,r){for(var o="",n=r?"0":"",a=0;a<=e-1;a++)o+=t[a]||n;return o}function p(t,e){return Array(e+1).join(t)}function a(t,e){if(t.value=t.value,null!==t){if(t.createTextRange){var r=t.createTextRange();return r.move("character",e),r.select(),!0}return t.selectionStart||0===t.selectionStart?(t.focus(),t.setSelectionRange(e,e),!0):(t.focus(),!1)}}function h(t,e,r){return Math.min(Math.max(t,e),r)}function g(t){return Math.max(t.selectionStart,t.selectionEnd)}var v={thousandSeparator:e.oneOfType([e.string,e.oneOf([!0])]),decimalSeparator:e.string,allowedDecimalSeparators:e.arrayOf(e.string),thousandsGroupStyle:e.oneOf(["thousand","lakh","wan"]),decimalScale:e.number,fixedDecimalScale:e.bool,displayType:e.oneOf(["input","text"]),prefix:e.string,suffix:e.string,format:e.oneOfType([e.string,e.func]),removeFormatting:e.func,mask:e.oneOfType([e.string,e.arrayOf(e.string)]),value:e.oneOfType([e.number,e.string]),defaultValue:e.oneOfType([e.number,e.string]),isNumericString:e.bool,customInput:e.elementType,allowNegative:e.bool,allowEmptyFormatting:e.bool,allowLeadingZeros:e.bool,onValueChange:e.func,onKeyDown:e.func,onMouseUp:e.func,onChange:e.func,onFocus:e.func,onBlur:e.func,type:e.oneOf(["text","tel","password"]),isAllowed:e.func,renderText:e.func,getInputRef:e.oneOfType([e.func,e.shape({current:e.any})])},s={displayType:"input",decimalSeparator:".",thousandsGroupStyle:"thousand",fixedDecimalScale:!1,prefix:"",suffix:"",allowNegative:!0,allowEmptyFormatting:!1,allowLeadingZeros:!1,isNumericString:!1,type:"text",onValueChange:r,onChange:r,onKeyDown:r,onMouseUp:r,onFocus:r,onBlur:r,isAllowed:function(){return!0}},u=function(o){function t(t){o.call(this,t);var e=t.defaultValue;this.validateProps();var r=this.formatValueProp(e);this.state={value:r,numAsString:this.removeFormatting(r),mounted:!1},this.selectionBeforeInput={selectionStart:0,selectionEnd:0},this.onChange=this.onChange.bind(this),this.onKeyDown=this.onKeyDown.bind(this),this.onMouseUp=this.onMouseUp.bind(this),this.onFocus=this.onFocus.bind(this),this.onBlur=this.onBlur.bind(this)}return o&&(t.__proto__=o),((t.prototype=Object.create(o&&o.prototype)).constructor=t).prototype.componentDidMount=function(){this.setState({mounted:!0})},t.prototype.componentDidUpdate=function(t){this.updateValueIfRequired(t)},t.prototype.componentWillUnmount=function(){clearTimeout(this.focusTimeout),clearTimeout(this.caretPositionTimeout)},t.prototype.updateValueIfRequired=function(t){var e=this.props,r=this.state,o=this.focusedElm,n=r.value,a=r.numAsString;if(void 0===a&&(a=""),t!==e){this.validateProps();var i=this.formatNumString(a),s=f(e.value)?i:this.formatValueProp(),u=this.removeFormatting(s),l=parseFloat(u),p=parseFloat(a);(isNaN(l)&&isNaN(p)||l===p)&&i===n&&(null!==o||s===n)||this.updateValue({formattedValue:s,numAsString:u,input:o})}},t.prototype.getFloatString=function(t){void 0===t&&(t="");var e=this.props.decimalScale,r=this.getSeparators().decimalSeparator,o=this.getNumberRegex(!0),n="-"===t[0];n&&(t=t.replace("-","")),r&&0===e&&(t=t.split(r)[0]);var a=(t=(t.match(o)||[]).join("").replace(r,".")).indexOf(".");return-1!==a&&(t=t.substring(0,a)+"."+t.substring(a+1,t.length).replace(new RegExp(i(r),"g"),"")),n&&(t="-"+t),t},t.prototype.getNumberRegex=function(t,e){var r=this.props,o=r.format,n=r.decimalScale,a=this.getSeparators().decimalSeparator;return new RegExp("\\d"+(!a||0===n||e||o?"":"|"+i(a)),t?"g":void 0)},t.prototype.getSeparators=function(){var t=this.props.decimalSeparator,e=this.props,r=e.thousandSeparator,o=e.allowedDecimalSeparators;return!0===r&&(r=","),{decimalSeparator:t,thousandSeparator:r,allowedDecimalSeparators:o=o||[t,"."]}},t.prototype.getMaskAtIndex=function(t){var e=this.props.mask;return void 0===e&&(e=" "),"string"==typeof e?e:e[t]||" "},t.prototype.getValueObject=function(t,e){var r=parseFloat(e);return{formattedValue:t,value:e,floatValue:isNaN(r)?void 0:r}},t.prototype.validateProps=function(){var t=this.props.mask,e=this.getSeparators(),r=e.decimalSeparator,o=e.thousandSeparator;if(r===o)throw new Error("\n Decimal separator can't be same as thousand separator.\n thousandSeparator: "+o+' (thousandSeparator = {true} is same as thousandSeparator = ",")\n decimalSeparator: '+r+" (default value for decimalSeparator is .)\n ");if(t&&("string"===t?t:t.toString()).match(/\d/g))throw new Error("\n Mask "+t+" should not contain numeric character;\n ")},t.prototype.setPatchedCaretPosition=function(t,e,r){a(t,e),this.caretPositionTimeout=setTimeout(function(){t.value===r&&a(t,e)},0)},t.prototype.correctCaretPosition=function(t,e,r){var o=this.props,n=o.prefix,a=o.suffix,i=o.format;if(""===t)return 0;if(e=h(e,0,t.length),!i){var s="-"===t[0];return h(e,n.length+(s?1:0),t.length-a.length)}if("function"==typeof i)return e;if("#"===i[e]&&c(t[e]))return e;if("#"===i[e-1]&&c(t[e-1]))return e;var u=i.indexOf("#");e=h(e,u,i.lastIndexOf("#")+1);for(var l=i.substring(e,i.length).indexOf("#"),p=e,f=e+(-1===l?0:l);u<p&&("#"!==i[p]||!c(t[p]));)p-=1;return!c(t[f])||"left"===r&&e!==u||e-p<f-e?c(t[p])?p+1:p:f},t.prototype.getCaretPosition=function(t,e,r){var o,n,a=this.props.format,i=this.state.value,s=this.getNumberRegex(!0),u=(t.match(s)||[]).join(""),l=(e.match(s)||[]).join("");for(n=o=0;n<r;n++){var p=t[n]||"",f=e[o]||"";if((p.match(s)||p===f)&&("0"!==p||!f.match(s)||"0"===f||u.length===l.length)){for(;p!==e[o]&&o<e.length;)o++;o++}}return"string"!=typeof a||i||(o=e.length),o=this.correctCaretPosition(e,o)},t.prototype.removePrefixAndSuffix=function(t){var e=this.props,r=e.format,o=e.prefix,n=e.suffix;if(!r&&t){var a="-"===t[0];a&&(t=t.substring(1,t.length));var i=(t=o&&0===t.indexOf(o)?t.substring(o.length,t.length):t).lastIndexOf(n);t=n&&-1!==i&&i===t.length-n.length?t.substring(0,i):t,a&&(t="-"+t)}return t},t.prototype.removePatternFormatting=function(t){for(var e=this.props.format.split("#").filter(function(t){return""!==t}),r=0,o="",n=0,a=e.length;n<=a;n++){var i=e[n]||"",s=n===a?t.length:t.indexOf(i,r);if(-1===s){o=t;break}o+=t.substring(r,s),r=s+i.length}return(o.match(/\d/g)||[]).join("")},t.prototype.removeFormatting=function(t){var e=this.props,r=e.format,o=e.removeFormatting;return t?t=r?"string"==typeof r?this.removePatternFormatting(t):"function"==typeof o?o(t):(t.match(/\d/g)||[]).join(""):(t=this.removePrefixAndSuffix(t),this.getFloatString(t)):t},t.prototype.formatWithPattern=function(t){for(var e=this.props.format,r=0,o=e.split(""),n=0,a=e.length;n<a;n++)"#"===e[n]&&(o[n]=t[r]||this.getMaskAtIndex(r),r+=1);return o.join("")},t.prototype.formatAsNumber=function(t){var e=this.props,r=e.decimalScale,o=e.fixedDecimalScale,n=e.prefix,a=e.suffix,i=e.allowNegative,s=e.thousandsGroupStyle,u=this.getSeparators(),l=u.thousandSeparator,p=u.decimalSeparator,f=-1!==t.indexOf(".")||r&&o,c=R(t,i),h=c.beforeDecimal,m=c.afterDecimal,g=c.addNegation;return void 0!==r&&(m=d(m,r,o)),l&&(h=function(t,e,r){var o=function(t){switch(t){case"lakh":return/(\d+?)(?=(\d\d)+(\d)(?!\d))(\.\d+)?/g;case"wan":return/(\d)(?=(\d{4})+(?!\d))/g;case"thousand":default:return/(\d)(?=(\d{3})+(?!\d))/g}}(r),n=t.search(/[1-9]/);return n=-1===n?t.length:n,t.substring(0,n)+t.substring(n,t.length).replace(o,"$1"+e)}(h,l,s)),n&&(h=n+h),a&&(m+=a),g&&(h="-"+h),t=h+(f&&p||"")+m},t.prototype.formatNumString=function(t){void 0===t&&(t="");var e=this.props,r=e.format,o=e.allowEmptyFormatting,n=t;return n=""!==t||o?"-"!==t||r?"string"==typeof r?this.formatWithPattern(n):"function"==typeof r?r(n):this.formatAsNumber(n):"-":""},t.prototype.formatValueProp=function(t){var e=this.props,r=e.format,o=e.decimalScale,n=e.fixedDecimalScale,a=e.allowEmptyFormatting,i=this.props,s=i.value,u=i.isNumericString,l=!(s=f(s)?t:s)&&0!==s;return l&&a&&(s=""),l&&!a?"":("number"==typeof s&&(s=function(t){var e="-"===(t+="")[0]?"-":"";e&&(t=t.substring(1));var r=t.split(/[eE]/g),o=r[0],n=r[1];if(!(n=Number(n)))return e+o;var a=1+n,i=(o=o.replace(".","")).length;return a<0?o="0."+p("0",Math.abs(a))+o:i<=a?o+=p("0",a-i):o=(o.substring(0,a)||"0")+"."+o.substring(a),e+o}(s),u=!0),"Infinity"===s&&u&&(s=""),u&&!r&&"number"==typeof o&&(s=function(t,e,r){if(-1!==["","-"].indexOf(t))return t;var o=-1!==t.indexOf(".")&&e,n=R(t),a=n.beforeDecimal,i=n.afterDecimal,s=n.hasNagation,u=parseFloat("0."+(i||"0")),l=(i.length<=e?"0."+i:u.toFixed(e)).split(".");return(s?"-":"")+a.split("").reverse().reduce(function(t,e,r){return t.length>r?(Number(t[0])+Number(e)).toString()+t.substring(1,t.length):e+t},l[0])+(o?".":"")+d(l[1]||"",Math.min(e,i.length),r)}(s,o,n)),u?this.formatNumString(s):this.formatInput(s))},t.prototype.formatNegation=function(t){void 0===t&&(t="");var e=this.props.allowNegative,r=new RegExp("(-)"),o=new RegExp("(-)(.)*(-)"),n=r.test(t),a=o.test(t);return t=t.replace(/-/g,""),n&&!a&&e&&(t="-"+t),t},t.prototype.formatInput=function(t){return void 0===t&&(t=""),this.props.format||(t=this.removePrefixAndSuffix(t),t=this.formatNegation(t)),t=this.removeFormatting(t),this.formatNumString(t)},t.prototype.isCharacterAFormat=function(t,e){var r=this.props,o=r.format,n=r.prefix,a=r.suffix,i=r.decimalScale,s=r.fixedDecimalScale,u=this.getSeparators().decimalSeparator;return"string"==typeof o&&"#"!==o[t]||!(o||!(t<n.length||t>=e.length-a.length||i&&s&&e[t]===u))},t.prototype.correctInputValue=function(t,r,e){var o=this,n=this.props,a=n.format,i=n.allowNegative,s=n.prefix,u=n.suffix,l=n.decimalScale,p=this.getSeparators(),f=p.allowedDecimalSeparators,c=p.decimalSeparator,h=this.state.numAsString||"",m=this.selectionBeforeInput,g=m.selectionStart,d=m.selectionEnd,v=function(t,e){for(var r=0,o=0,n=t.length,a=e.length;t[r]===e[r]&&r<n;)r++;for(;t[n-1-o]===e[a-1-o]&&r<a-o&&r<n-o;)o++;return{start:r,end:n-o}}(r,e),y=v.start,S=v.end;if(!a&&y===S&&-1!==f.indexOf(e[g])){var x=0===l?"":c;return e.substr(0,g)+x+e.substr(g+1,e.length)}var b=a?0:s.length,P=r.length-(a?0:u.length);if(e.length>r.length||!e.length||y===S||0===g&&d===r.length||0===y&&S===r.length||g===b&&d===P)return e;var w=r.substr(y,S-y);if(!![].concat(w).find(function(t,e){return o.isCharacterAFormat(e+y,r)})){var O=r.substr(y),N={},T=[];[].concat(O).forEach(function(t,e){o.isCharacterAFormat(e+y,r)?N[e]=t:e>w.length-1&&T.push(t)}),Object.keys(N).forEach(function(t){T.length>t?T.splice(t,0,N[t]):T.push(N[t])}),e=r.substr(0,y)+T.join("")}if(!a){var F=this.removeFormatting(e),C=R(F,i),D=C.beforeDecimal,E=C.afterDecimal,V=C.addNegation,A=t<e.indexOf(c)+1;if(F.length<h.length&&A&&""===D&&!parseFloat(E))return V?"-":""}return e},t.prototype.updateValue=function(t){var e=t.formattedValue,r=t.input,o=t.setCaretPosition;void 0===o&&(o=!0);var n=t.numAsString,a=t.caretPos,i=this.props.onValueChange,s=this.state.value;if(r)if(o){if(!a){var u=t.inputValue||r.value,l=g(r);r.value=e,a=this.getCaretPosition(u,e,l)}this.setPatchedCaretPosition(r,a,e)}else r.value=e;void 0===n&&(n=this.removeFormatting(e)),e!==s&&(this.setState({value:e,numAsString:n}),i(this.getValueObject(e,n)))},t.prototype.onChange=function(t){var e=t.target,r=e.value,o=this.state,n=this.props,a=n.isAllowed,i=o.value||"",s=g(e);r=this.correctInputValue(s,i,r);var u=this.formatInput(r)||"",l=this.removeFormatting(u),p=a(this.getValueObject(u,l));p||(u=i),this.updateValue({formattedValue:u,numAsString:l,inputValue:r,input:e}),p&&n.onChange(t)},t.prototype.onBlur=function(t){var e=this.props,r=this.state,o=e.format,n=e.onBlur,a=e.allowLeadingZeros,i=r.numAsString,s=r.value;if(this.focusedElm=null,clearTimeout(this.focusTimeout),clearTimeout(this.caretPositionTimeout),!o){isNaN(parseFloat(i))&&(i=""),a||(i=function(t){if(!t)return t;var e="-"===t[0];e&&(t=t.substring(1,t.length));var r=t.split("."),o=r[0].replace(/^0+/,"")||"0",n=r[1]||"";return(e?"-":"")+o+(n?"."+n:"")}(i));var u=this.formatNumString(i);if(u!==s)return this.updateValue({formattedValue:u,numAsString:i,input:t.target,setCaretPosition:!1}),void n(t)}n(t)},t.prototype.onKeyDown=function(t){var e,r=t.target,o=t.key,n=r.selectionStart,a=r.selectionEnd,i=r.value;void 0===i&&(i="");var s=this.props,u=s.decimalScale,l=s.fixedDecimalScale,p=s.prefix,f=s.suffix,c=s.format,h=s.onKeyDown,m=void 0!==u&&l,g=this.getNumberRegex(!1,m),d=new RegExp("-"),v="string"==typeof c;if(this.selectionBeforeInput={selectionStart:n,selectionEnd:a},"ArrowLeft"===o||"Backspace"===o?e=n-1:"ArrowRight"===o?e=n+1:"Delete"===o&&(e=n),void 0!==e&&n===a){var y=e,S=v?c.indexOf("#"):p.length,x=v?c.lastIndexOf("#")+1:i.length-f.length;if("ArrowLeft"===o||"ArrowRight"===o){var b="ArrowLeft"===o?"left":"right";y=this.correctCaretPosition(i,e,b)}else if("Delete"!==o||g.test(i[e])||d.test(i[e])){if("Backspace"===o&&!g.test(i[e]))if(n<=S+1&&"-"===i[0]&&void 0===c){var P=i.substring(1);this.updateValue({formattedValue:P,caretPos:y,input:r})}else if(!d.test(i[e])){for(;!g.test(i[y-1])&&S<y;)y--;y=this.correctCaretPosition(i,y,"left")}}else for(;!g.test(i[y])&&y<x;)y++;(y!==e||e<S||x<e)&&(t.preventDefault(),this.setPatchedCaretPosition(r,y,i)),t.isUnitTestRun&&this.setPatchedCaretPosition(r,y,i),h(t)}else h(t)},t.prototype.onMouseUp=function(t){var e=t.target,r=e.selectionStart,o=e.selectionEnd,n=e.value;if(void 0===n&&(n=""),r===o){var a=this.correctCaretPosition(n,r);a!==r&&this.setPatchedCaretPosition(e,a,n)}this.props.onMouseUp(t)},t.prototype.onFocus=function(a){var i=this;a.persist(),this.focusedElm=a.target,this.focusTimeout=setTimeout(function(){var t=a.target,e=t.selectionStart,r=t.selectionEnd,o=t.value;void 0===o&&(o="");var n=i.correctCaretPosition(o,e);n===e||0===e&&r===o.length||i.setPatchedCaretPosition(t,n,o),i.props.onFocus(a)},0)},t.prototype.render=function(){var t=this.props,e=t.type,r=t.displayType,o=t.customInput,n=t.renderText,a=t.getInputRef,i=t.format,s=this.state,u=s.value,l=s.mounted,p=function(e,r){var o={};return Object.keys(e).forEach(function(t){r[t]||(o[t]=e[t])}),o}(this.props,v),f=l&&function(t){return t||!(navigator.platform&&/iPhone|iPod/.test(navigator.platform))}(i)?"numeric":void 0,c=Object.assign({inputMode:f},p,{type:e,value:u,onChange:this.onChange,onKeyDown:this.onKeyDown,onMouseUp:this.onMouseUp,onFocus:this.onFocus,onBlur:this.onBlur});if("text"===r)return n?n(u,p)||null:m.createElement("span",Object.assign({},p,{ref:a}),u);if(o){var h=o;return m.createElement(h,Object.assign({},c,{ref:a}))}return m.createElement("input",Object.assign({},c,{ref:a}))},t}(m.Component);return u.propTypes=v,u.defaultProps=s,u}); |
@@ -151,2 +151,3 @@ "use strict"; | ||
clearTimeout(this.focusTimeout); | ||
clearTimeout(this.caretPositionTimeout); | ||
} | ||
@@ -306,3 +307,3 @@ }, { | ||
(0, _utils.setCaretPosition)(el, caretPos); | ||
setTimeout(function () { | ||
this.caretPositionTimeout = setTimeout(function () { | ||
if (el.value === currentValue) (0, _utils.setCaretPosition)(el, caretPos); | ||
@@ -885,2 +886,3 @@ }, 0); | ||
clearTimeout(this.focusTimeout); | ||
clearTimeout(this.caretPositionTimeout); | ||
@@ -887,0 +889,0 @@ if (!format) { |
{ | ||
"name": "react-number-format", | ||
"description": "React component to format number in an input or as a text.", | ||
"version": "4.6.2", | ||
"version": "4.6.3", | ||
"main": "dist/react-number-format.cjs.js", | ||
@@ -6,0 +6,0 @@ "module": "dist/react-number-format.es.js", |
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
253483
5296