Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

react-number-format

Package Overview
Dependencies
Maintainers
1
Versions
120
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-number-format - npm Package Compare versions

Comparing version 3.0.0-beta2 to 3.0.0-beta3

4

dist/react-number-format.js
/*!
* react-number-format - 3.0.0-beta2
* react-number-format - 3.0.0-beta3
* Author : Sudhanshu Yadav

@@ -406,3 +406,3 @@ * Copyright (c) 2016,2017 to Sudhanshu Yadav - ignitersworld.com , released under the MIT license.

if (typeof format === 'string' && !stateValue) {
if (typeof format === 'string' && !stateValue || caretPos === formattedValue.length) {
//set it to the maximum value so it goes after the last number

@@ -409,0 +409,0 @@ j = formattedValue.length;

/*!
* react-number-format - 3.0.0-beta2
* react-number-format - 3.0.0-beta3
* Author : Sudhanshu Yadav
* Copyright (c) 2016,2017 to Sudhanshu Yadav - ignitersworld.com , released under the MIT license.
*/
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t(require("react")):"function"==typeof define&&define.amd?define(["react"],t):"object"==typeof exports?exports.NumberFormat=t(require("react")):e.NumberFormat=t(e.React)}(this,function(e){return function(e){function t(n){if(r[n])return r[n].exports;var a=r[n]={exports:{},id:n,loaded:!1};return e[n].call(a.exports,a,a.exports,t),a.loaded=!0,a.exports}var r={};return t.m=e,t.c=r,t.p="",t(0)}([function(e,t,r){e.exports=r(1)},function(e,t,r){"use strict";function n(e){return e&&e.__esModule?e:{default:e}}function a(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function o(e,t){if(!e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return!t||"object"!=typeof t&&"function"!=typeof t?e:t}function i(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Super expression must either be null or a function, not "+typeof t);e.prototype=Object.create(t&&t.prototype,{constructor:{value:e,enumerable:!1,writable:!0,configurable:!0}}),t&&(Object.setPrototypeOf?Object.setPrototypeOf(e,t):e.__proto__=t)}var u=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var r=arguments[t];for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&(e[n]=r[n])}return e},s=function(){function e(e,t){for(var r=0;r<t.length;r++){var n=t[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,n.key,n)}}return function(t,r,n){return r&&e(t.prototype,r),n&&e(t,n),t}}(),l=r(2),f=n(l),c=r(7),p=n(c),h=r(8),d={thousandSeparator:f.default.oneOfType([f.default.string,f.default.oneOf([!0])]),decimalSeparator:f.default.string,decimalScale:f.default.number,fixedDecimalScale:f.default.bool,displayType:f.default.oneOf(["input","text"]),prefix:f.default.string,suffix:f.default.string,format:f.default.oneOfType([f.default.string,f.default.func]),removeFormatting:f.default.func,mask:f.default.oneOfType([f.default.string,f.default.arrayOf(f.default.string)]),value:f.default.oneOfType([f.default.number,f.default.string]),isNumericString:f.default.bool,customInput:f.default.func,allowNegative:f.default.bool,onValueChange:f.default.func,onKeyDown:f.default.func,onMouseUp:f.default.func,onChange:f.default.func,onFocus:f.default.func,onBlur:f.default.func,type:f.default.oneOf(["text","tel"]),isAllowed:f.default.func,renderText:f.default.func},g={displayType:"input",decimalSeparator:".",fixedDecimalScale:!1,prefix:"",suffix:"",allowNegative:!0,isNumericString:!1,type:"text",onValueChange:h.noop,onChange:h.noop,onKeyDown:h.noop,onMouseUp:h.noop,onFocus:h.noop,onBlur:h.noop,isAllowed:h.returnTrue},m=function(e){function t(e){a(this,t);var r=o(this,(t.__proto__||Object.getPrototypeOf(t)).call(this,e));r.validateProps();var n=r.formatValueProp();return r.state={value:n,numAsString:r.removeFormatting(n)},r.onChange=r.onChange.bind(r),r.onKeyDown=r.onKeyDown.bind(r),r.onMouseUp=r.onMouseUp.bind(r),r.onFocus=r.onFocus.bind(r),r.onBlur=r.onBlur.bind(r),r}return i(t,e),s(t,[{key:"componentDidUpdate",value:function(e){this.updateValueIfRequired(e)}},{key:"updateValueIfRequired",value:function(e){var t=this.props,r=this.state;if(e!==t){this.validateProps();var n=r.value,a=r.numAsString||"",o=void 0===t.value?this.formatNumString(a):this.formatValueProp();o!==n&&this.setState({value:o,numAsString:this.removeFormatting(o)})}}},{key:"getFloatString",value:function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"",t=this.getSeparators(),r=t.decimalSeparator,n=this.getNumberRegex(!0),a="-"===e[0];a&&(e=e.replace("-","")),e=(e.match(n)||[]).join("").replace(r,".");var o=e.indexOf(".");return o!==-1&&(e=e.substring(0,o)+"."+e.substring(o+1,e.length).replace(new RegExp((0,h.escapeRegExp)(r),"g"),"")),a&&(e="-"+e),e}},{key:"getNumberRegex",value:function(e,t){var r=this.props,n=r.format,a=r.decimalScale,o=this.getSeparators(),i=o.decimalSeparator;return new RegExp("\\d"+(!i||0===a||t||n?"":"|"+(0,h.escapeRegExp)(i)),e?"g":void 0)}},{key:"getSeparators",value:function(){var e=this.props.decimalSeparator,t=this.props.thousandSeparator;return t===!0&&(t=","),{decimalSeparator:e,thousandSeparator:t}}},{key:"getMaskAtIndex",value:function(e){var t=this.props.mask,r=void 0===t?" ":t;return"string"==typeof r?r:r[e]||" "}},{key:"validateProps",value:function(){var e=this.props.mask,t=this.getSeparators(),r=t.decimalSeparator,n=t.thousandSeparator;if(r===n)throw new Error("\n Decimal separator can't be same as thousand separator.\n\n thousandSeparator: "+n+' (thousandSeparator = {true} is same as thousandSeparator = ",")\n decimalSeparator: '+r+" (default value for decimalSeparator is .)\n ");if(e){var a="string"===e?e:e.toString();if(a.match(/\d/g))throw new Error("\n Mask "+e+" should not contain numeric character;\n ")}}},{key:"splitDecimal",value:function(e){var t=this.props.allowNegative,r="-"===e[0],n=r&&t;e=e.replace("-","");var a=e.split("."),o=a[0],i=a[1]||"";return{beforeDecimal:o,afterDecimal:i,hasNagation:r,addNegation:n}}},{key:"setPatchedCaretPosition",value:function(e,t,r){(0,h.setCaretPosition)(e,t),setTimeout(function(){e.value===r&&(0,h.setCaretPosition)(e,t)},0)}},{key:"correctCaretPosition",value:function(e,t,r){var n=this.props,a=n.prefix,o=n.suffix,i=n.format;if(!i){var u="-"===e[0];return Math.min(Math.max(t,a.length+(u?1:0)),e.length-o.length)}if("function"==typeof i)return t;if("#"===i[t]&&(0,h.charIsNumber)(e[t]))return t;if("#"===i[t-1]&&(0,h.charIsNumber)(e[t-1]))return t;var s=i.indexOf("#"),l=i.lastIndexOf("#");t=Math.min(Math.max(t,s),l+1);for(var f=i.substring(t,i.length).indexOf("#"),c=t,p=t+(f===-1?0:f);c>s&&("#"!==i[c]||!(0,h.charIsNumber)(e[c]));)c-=1;var d=!(0,h.charIsNumber)(e[p])||"left"===r&&t!==s||t-c<p-t;return d?c+1:p}},{key:"getCaretPosition",value:function(e,t,r){var n=this.props.format,a=this.state.value,o=this.getNumberRegex(!0),i=(e.match(o)||[]).join(""),u=(t.match(o)||[]).join(""),s=void 0,l=void 0;for(s=0,l=0;l<r;l++){var f=e[l],c=t[s]||"";if((f.match(o)||f===c)&&("0"!==f||!c.match(o)||"0"===c||i.length===u.length)){for(;f!==t[s]&&s<t.length;)s++;s++}}return"string"!=typeof n||a||(s=t.length),s=this.correctCaretPosition(t,s)}},{key:"removePrefixAndSuffix",value:function(e){var t=this.props,r=t.format,n=t.prefix,a=t.suffix;if(!r&&e){var o="-"===e[0];o&&(e=e.substring(1,e.length)),e=n&&0===e.indexOf(n)?e.substring(n.length,e.length):e;var i=e.lastIndexOf(a);e=a&&i!==-1&&i===e.length-a.length?e.substring(0,i):e,o&&(e="-"+e)}return e}},{key:"removePatternFormatting",value:function(e){for(var t=this.props.format,r=t.split("#").filter(function(e){return""!==e}),n=0,a="",o=0,i=r.length;o<=i;o++){var u=r[o]||"",s=o===i?e.length:e.indexOf(u,n);if(s===-1){a=e;break}a+=e.substring(n,s),n=s+u.length}return(a.match(/\d/g)||[]).join("")}},{key:"removeFormatting",value:function e(t){var r=this.props,n=r.format,e=r.removeFormatting;return t?(n?t="string"==typeof n?this.removePatternFormatting(t):"function"==typeof e?e(t):(t.match(/\d/g)||[]).join(""):(t=this.removePrefixAndSuffix(t),t=this.getFloatString(t)),t):t}},{key:"formatWithPattern",value:function(e){for(var t=this.props.format,r=0,n=t.split(""),a=0,o=t.length;a<o;a++)"#"===t[a]&&(n[a]=e[r]||this.getMaskAtIndex(r),r+=1);return n.join("")}},{key:"formatAsNumber",value:function(e){var t=this.props,r=t.decimalScale,n=t.fixedDecimalScale,a=t.prefix,o=t.suffix,i=this.getSeparators(),u=i.thousandSeparator,s=i.decimalSeparator,l=e.indexOf(".")!==-1||r&&n,f=this.splitDecimal(e),c=f.beforeDecimal,p=f.afterDecimal,d=f.addNegation;return void 0!==r&&(p=(0,h.limitToScale)(p,r,n)),u&&(c=c.replace(/(\d)(?=(\d{3})+(?!\d))/g,"$1"+u)),a&&(c=a+c),o&&(p+=o),d&&(c="-"+c),e=c+(l&&s||"")+p}},{key:"formatNumString",value:function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"",t=this.props.format,r=e;return""===e?r="":"-"!==e||t?r="string"==typeof t?this.formatWithPattern(r):"function"==typeof t?t(r):this.formatAsNumber(r):(r="-",e=""),r}},{key:"formatValueProp",value:function(){var e=this.props,t=e.format,r=e.decimalScale,n=e.fixedDecimalScale,a=this.props,o=a.value,i=a.isNumericString;if(void 0===o)return"";"number"==typeof o&&(o=o.toString(),i=!0),i&&!t&&"number"==typeof r&&(o=(0,h.roundToPrecision)(o,r,n));var u=i?this.formatNumString(o):this.formatInput(o);return u}},{key:"formatNegation",value:function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"",t=this.props.allowNegative,r=new RegExp("(-)"),n=new RegExp("(-)(.)*(-)"),a=r.test(e),o=n.test(e);return e=e.replace(/-/g,""),a&&!o&&t&&(e="-"+e),e}},{key:"formatInput",value:function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"",t=this.props.format;return t||(e=this.formatNegation(e)),e=this.removeFormatting(e),this.formatNumString(e)}},{key:"isCharacterAFormat",value:function(e,t){var r=this.props,n=r.format,a=r.prefix,o=r.suffix,i=r.decimalScale,u=r.fixedDecimalScale,s=this.getSeparators(),l=s.decimalSeparator;return"string"==typeof n&&"#"!==n[e]||!(n||!(e<a.length||e>=t.length-o.length||i&&u&&t[e]===l))}},{key:"checkIfFormatGotDeleted",value:function(e,t,r){for(var n=e;n<t;n++)if(this.isCharacterAFormat(n,r))return!0;return!1}},{key:"correctInputValue",value:function(e,t,r){var n=this.props.format,a=this.state.numAsString||"";if(r.length>=t.length||!r.length)return r;var o=e,i=(0,h.splitString)(t,e),u=(0,h.splitString)(r,e),s=i[1].lastIndexOf(u[1]),l=s!==-1?i[1].substring(0,s):"",f=o+l.length;if(this.checkIfFormatGotDeleted(o,f,t)&&(r=t),!n){var c=this.removeFormatting(r),p=this.splitDecimal(c),d=p.beforeDecimal,g=p.afterDecimal,m=p.addNegation;if(c.length<a.length&&""===d&&!parseFloat(g))return m?"-":""}return r}},{key:"onChange",value:function(e){e.persist();var t=e.target,r=t.value,n=this.state,a=this.props,o=a.isAllowed,i=n.value||"",u=Math.max(t.selectionStart,t.selectionEnd);r=this.correctInputValue(u,i,r);var s=this.formatInput(r)||"",l=this.removeFormatting(s),f={formattedValue:s,value:l,floatValue:parseFloat(l)};o(f)||(s=i),t.value=s;var c=this.getCaretPosition(r,s,Math.min(s.length,u));this.setPatchedCaretPosition(t,c,s),s!==i?this.setState({value:s,numAsString:l},function(){a.onValueChange(f),a.onChange(e)}):a.onChange(e)}},{key:"onBlur",value:function e(t){var r=this.props,n=this.state,a=r.format,e=r.onBlur,o=n.numAsString,i=n.value;if(!a){o=(0,h.fixLeadingZero)(o);var u=this.formatNumString(o),s={formattedValue:u,value:o,floatValue:parseFloat(o)};if(u!==i)return void this.setState({value:u,numAsString:o},function(){r.onValueChange(s),e(t)})}e(t)}},{key:"onKeyDown",value:function e(t){var r=t.target,n=t.key,a=r.selectionEnd,o=r.value,i=r.selectionStart,u=void 0,s=this.props,l=s.decimalScale,f=s.fixedDecimalScale,c=s.prefix,p=s.suffix,h=s.format,e=s.onKeyDown,d=void 0!==l&&f,g=this.getNumberRegex(!1,d),m=new RegExp("-"),v="string"==typeof h;if("ArrowLeft"===n||"Backspace"===n?u=i-1:"ArrowRight"===n?u=i+1:"Delete"===n&&(u=i),void 0===u||i!==a)return void e(t);var y=u,x=v?h.indexOf("#"):c.length,S=v?h.lastIndexOf("#")+1:o.length-p.length;if("ArrowLeft"===n||"ArrowRight"===n){var b="ArrowLeft"===n?"left":"right";y=this.correctCaretPosition(o,u,b)}else if("Delete"!==n||g.test(o[u])||m.test(o[u])){if("Backspace"===n&&!g.test(o[u])&&!m.test(o[u])){for(;!g.test(o[y-1])&&y>x;)y--;y=this.correctCaretPosition(o,y,"left")}}else for(;!g.test(o[y])&&y<S;)y++;(y!==u||u<x||u>S)&&(t.preventDefault(),this.setPatchedCaretPosition(r,y,o)),t.isUnitTestRun&&this.setPatchedCaretPosition(r,y,o),this.props.onKeyDown(t)}},{key:"onMouseUp",value:function(e){var t=e.target,r=t.selectionStart,n=t.selectionEnd,a=t.value;if(r===n){var o=this.correctCaretPosition(a,r);o!==r&&this.setPatchedCaretPosition(t,o,a)}this.props.onMouseUp(e)}},{key:"onFocus",value:function(e){var t=e.target,r=t.selectionStart,n=t.value,a=this.correctCaretPosition(n,r);a!==r&&this.setPatchedCaretPosition(t,a,n),this.props.onFocus(e)}},{key:"render",value:function(){var e=this.props,t=e.type,r=e.displayType,n=e.customInput,a=e.renderText,o=this.state.value,i=(0,h.omit)(this.props,d),s=u({},i,{type:t,value:o,onChange:this.onChange,onKeyDown:this.onKeyDown,onMouseUp:this.onMouseUp,onFocus:this.onFocus,onBlur:this.onBlur});if("text"===r)return a?a(o)||null:p.default.createElement("span",i,o);if(n){var l=n;return p.default.createElement(l,s)}return p.default.createElement("input",s)}}]),t}(p.default.Component);m.propTypes=d,m.defaultProps=g,e.exports=m},function(e,t,r){e.exports=r(3)()},function(e,t,r){"use strict";var n=r(4),a=r(5),o=r(6);e.exports=function(){function e(e,t,r,n,i,u){u!==o&&a(!1,"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")}function t(){return e}e.isRequired=e;var r={array:e,bool:e,func:e,number:e,object:e,string:e,symbol:e,any:e,arrayOf:t,element:e,instanceOf:t,node:e,objectOf:t,oneOf:t,oneOfType:t,shape:t,exact:t};return r.checkPropTypes=n,r.PropTypes=r,r}},function(e,t){"use strict";function r(e){return function(){return e}}var n=function(){};n.thatReturns=r,n.thatReturnsFalse=r(!1),n.thatReturnsTrue=r(!0),n.thatReturnsNull=r(null),n.thatReturnsThis=function(){return this},n.thatReturnsArgument=function(e){return e},e.exports=n},function(e,t,r){"use strict";function n(e,t,r,n,o,i,u,s){if(a(t),!e){var l;if(void 0===t)l=new Error("Minified exception occurred; use the non-minified dev environment for the full error message and additional helpful warnings.");else{var f=[r,n,o,i,u,s],c=0;l=new Error(t.replace(/%s/g,function(){return f[c++]})),l.name="Invariant Violation"}throw l.framesToPop=1,l}}var a=function(e){};e.exports=n},function(e,t){"use strict";var r="SECRET_DO_NOT_PASS_THIS_OR_YOU_WILL_BE_FIRED";e.exports=r},function(t,r){t.exports=e},function(e,t){"use strict";function r(){}function n(){return!0}function a(e){return!!(e||"").match(/\d/)}function o(e){return e.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g,"\\$&")}function i(e){if(!e)return e;var t="-"===e[0];t&&(e=e.substring(1,e.length));var r=e.split("."),n=r[0].replace(/^0+/,"")||"0",a=r[1]||"";return""+(t?"-":"")+n+(a?"."+a:"")}function u(e,t){return[e.substring(0,t),e.substring(t)]}function s(e,t,r){for(var n="",a=r?"0":"",o=0;o<=t-1;o++)n+=e[o]||a;return n}function l(e,t,r){var n=e.split("."),a=parseFloat("0."+(n[1]||"0")).toFixed(t).split("."),o=n[0].split("").reverse().reduce(function(e,t,r){return e.length>r?(Number(e[0])+Number(t)).toString()+e.substring(1,e.length):t+e},a[0]),i=s(a[1]||"",(n[1]||"").length,r);return o+(i?"."+i:"")}function f(e,t){var r={};return Object.keys(e).forEach(function(n){t[n]||(r[n]=e[n])}),r}function c(e,t){if(e.value=e.value,null!==e){if(e.createTextRange){var r=e.createTextRange();return r.move("character",t),r.select(),!0}return e.selectionStart||0===e.selectionStart?(e.focus(),e.setSelectionRange(t,t),!0):(e.focus(),!1)}}Object.defineProperty(t,"__esModule",{value:!0}),t.noop=r,t.returnTrue=n,t.charIsNumber=a,t.escapeRegExp=o,t.fixLeadingZero=i,t.splitString=u,t.limitToScale=s,t.roundToPrecision=l,t.omit=f,t.setCaretPosition=c}])});
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t(require("react")):"function"==typeof define&&define.amd?define(["react"],t):"object"==typeof exports?exports.NumberFormat=t(require("react")):e.NumberFormat=t(e.React)}(this,function(e){return function(e){function t(n){if(r[n])return r[n].exports;var a=r[n]={exports:{},id:n,loaded:!1};return e[n].call(a.exports,a,a.exports,t),a.loaded=!0,a.exports}var r={};return t.m=e,t.c=r,t.p="",t(0)}([function(e,t,r){e.exports=r(1)},function(e,t,r){"use strict";function n(e){return e&&e.__esModule?e:{default:e}}function a(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function o(e,t){if(!e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return!t||"object"!=typeof t&&"function"!=typeof t?e:t}function i(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Super expression must either be null or a function, not "+typeof t);e.prototype=Object.create(t&&t.prototype,{constructor:{value:e,enumerable:!1,writable:!0,configurable:!0}}),t&&(Object.setPrototypeOf?Object.setPrototypeOf(e,t):e.__proto__=t)}var u=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var r=arguments[t];for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&(e[n]=r[n])}return e},s=function(){function e(e,t){for(var r=0;r<t.length;r++){var n=t[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,n.key,n)}}return function(t,r,n){return r&&e(t.prototype,r),n&&e(t,n),t}}(),l=r(2),f=n(l),c=r(7),p=n(c),h=r(8),d={thousandSeparator:f.default.oneOfType([f.default.string,f.default.oneOf([!0])]),decimalSeparator:f.default.string,decimalScale:f.default.number,fixedDecimalScale:f.default.bool,displayType:f.default.oneOf(["input","text"]),prefix:f.default.string,suffix:f.default.string,format:f.default.oneOfType([f.default.string,f.default.func]),removeFormatting:f.default.func,mask:f.default.oneOfType([f.default.string,f.default.arrayOf(f.default.string)]),value:f.default.oneOfType([f.default.number,f.default.string]),isNumericString:f.default.bool,customInput:f.default.func,allowNegative:f.default.bool,onValueChange:f.default.func,onKeyDown:f.default.func,onMouseUp:f.default.func,onChange:f.default.func,onFocus:f.default.func,onBlur:f.default.func,type:f.default.oneOf(["text","tel"]),isAllowed:f.default.func,renderText:f.default.func},g={displayType:"input",decimalSeparator:".",fixedDecimalScale:!1,prefix:"",suffix:"",allowNegative:!0,isNumericString:!1,type:"text",onValueChange:h.noop,onChange:h.noop,onKeyDown:h.noop,onMouseUp:h.noop,onFocus:h.noop,onBlur:h.noop,isAllowed:h.returnTrue},m=function(e){function t(e){a(this,t);var r=o(this,(t.__proto__||Object.getPrototypeOf(t)).call(this,e));r.validateProps();var n=r.formatValueProp();return r.state={value:n,numAsString:r.removeFormatting(n)},r.onChange=r.onChange.bind(r),r.onKeyDown=r.onKeyDown.bind(r),r.onMouseUp=r.onMouseUp.bind(r),r.onFocus=r.onFocus.bind(r),r.onBlur=r.onBlur.bind(r),r}return i(t,e),s(t,[{key:"componentDidUpdate",value:function(e){this.updateValueIfRequired(e)}},{key:"updateValueIfRequired",value:function(e){var t=this.props,r=this.state;if(e!==t){this.validateProps();var n=r.value,a=r.numAsString||"",o=void 0===t.value?this.formatNumString(a):this.formatValueProp();o!==n&&this.setState({value:o,numAsString:this.removeFormatting(o)})}}},{key:"getFloatString",value:function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"",t=this.getSeparators(),r=t.decimalSeparator,n=this.getNumberRegex(!0),a="-"===e[0];a&&(e=e.replace("-","")),e=(e.match(n)||[]).join("").replace(r,".");var o=e.indexOf(".");return o!==-1&&(e=e.substring(0,o)+"."+e.substring(o+1,e.length).replace(new RegExp((0,h.escapeRegExp)(r),"g"),"")),a&&(e="-"+e),e}},{key:"getNumberRegex",value:function(e,t){var r=this.props,n=r.format,a=r.decimalScale,o=this.getSeparators(),i=o.decimalSeparator;return new RegExp("\\d"+(!i||0===a||t||n?"":"|"+(0,h.escapeRegExp)(i)),e?"g":void 0)}},{key:"getSeparators",value:function(){var e=this.props.decimalSeparator,t=this.props.thousandSeparator;return t===!0&&(t=","),{decimalSeparator:e,thousandSeparator:t}}},{key:"getMaskAtIndex",value:function(e){var t=this.props.mask,r=void 0===t?" ":t;return"string"==typeof r?r:r[e]||" "}},{key:"validateProps",value:function(){var e=this.props.mask,t=this.getSeparators(),r=t.decimalSeparator,n=t.thousandSeparator;if(r===n)throw new Error("\n Decimal separator can't be same as thousand separator.\n\n thousandSeparator: "+n+' (thousandSeparator = {true} is same as thousandSeparator = ",")\n decimalSeparator: '+r+" (default value for decimalSeparator is .)\n ");if(e){var a="string"===e?e:e.toString();if(a.match(/\d/g))throw new Error("\n Mask "+e+" should not contain numeric character;\n ")}}},{key:"splitDecimal",value:function(e){var t=this.props.allowNegative,r="-"===e[0],n=r&&t;e=e.replace("-","");var a=e.split("."),o=a[0],i=a[1]||"";return{beforeDecimal:o,afterDecimal:i,hasNagation:r,addNegation:n}}},{key:"setPatchedCaretPosition",value:function(e,t,r){(0,h.setCaretPosition)(e,t),setTimeout(function(){e.value===r&&(0,h.setCaretPosition)(e,t)},0)}},{key:"correctCaretPosition",value:function(e,t,r){var n=this.props,a=n.prefix,o=n.suffix,i=n.format;if(!i){var u="-"===e[0];return Math.min(Math.max(t,a.length+(u?1:0)),e.length-o.length)}if("function"==typeof i)return t;if("#"===i[t]&&(0,h.charIsNumber)(e[t]))return t;if("#"===i[t-1]&&(0,h.charIsNumber)(e[t-1]))return t;var s=i.indexOf("#"),l=i.lastIndexOf("#");t=Math.min(Math.max(t,s),l+1);for(var f=i.substring(t,i.length).indexOf("#"),c=t,p=t+(f===-1?0:f);c>s&&("#"!==i[c]||!(0,h.charIsNumber)(e[c]));)c-=1;var d=!(0,h.charIsNumber)(e[p])||"left"===r&&t!==s||t-c<p-t;return d?c+1:p}},{key:"getCaretPosition",value:function(e,t,r){var n=this.props.format,a=this.state.value,o=this.getNumberRegex(!0),i=(e.match(o)||[]).join(""),u=(t.match(o)||[]).join(""),s=void 0,l=void 0;for(s=0,l=0;l<r;l++){var f=e[l],c=t[s]||"";if((f.match(o)||f===c)&&("0"!==f||!c.match(o)||"0"===c||i.length===u.length)){for(;f!==t[s]&&s<t.length;)s++;s++}}return("string"==typeof n&&!a||r===t.length)&&(s=t.length),s=this.correctCaretPosition(t,s)}},{key:"removePrefixAndSuffix",value:function(e){var t=this.props,r=t.format,n=t.prefix,a=t.suffix;if(!r&&e){var o="-"===e[0];o&&(e=e.substring(1,e.length)),e=n&&0===e.indexOf(n)?e.substring(n.length,e.length):e;var i=e.lastIndexOf(a);e=a&&i!==-1&&i===e.length-a.length?e.substring(0,i):e,o&&(e="-"+e)}return e}},{key:"removePatternFormatting",value:function(e){for(var t=this.props.format,r=t.split("#").filter(function(e){return""!==e}),n=0,a="",o=0,i=r.length;o<=i;o++){var u=r[o]||"",s=o===i?e.length:e.indexOf(u,n);if(s===-1){a=e;break}a+=e.substring(n,s),n=s+u.length}return(a.match(/\d/g)||[]).join("")}},{key:"removeFormatting",value:function e(t){var r=this.props,n=r.format,e=r.removeFormatting;return t?(n?t="string"==typeof n?this.removePatternFormatting(t):"function"==typeof e?e(t):(t.match(/\d/g)||[]).join(""):(t=this.removePrefixAndSuffix(t),t=this.getFloatString(t)),t):t}},{key:"formatWithPattern",value:function(e){for(var t=this.props.format,r=0,n=t.split(""),a=0,o=t.length;a<o;a++)"#"===t[a]&&(n[a]=e[r]||this.getMaskAtIndex(r),r+=1);return n.join("")}},{key:"formatAsNumber",value:function(e){var t=this.props,r=t.decimalScale,n=t.fixedDecimalScale,a=t.prefix,o=t.suffix,i=this.getSeparators(),u=i.thousandSeparator,s=i.decimalSeparator,l=e.indexOf(".")!==-1||r&&n,f=this.splitDecimal(e),c=f.beforeDecimal,p=f.afterDecimal,d=f.addNegation;return void 0!==r&&(p=(0,h.limitToScale)(p,r,n)),u&&(c=c.replace(/(\d)(?=(\d{3})+(?!\d))/g,"$1"+u)),a&&(c=a+c),o&&(p+=o),d&&(c="-"+c),e=c+(l&&s||"")+p}},{key:"formatNumString",value:function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"",t=this.props.format,r=e;return""===e?r="":"-"!==e||t?r="string"==typeof t?this.formatWithPattern(r):"function"==typeof t?t(r):this.formatAsNumber(r):(r="-",e=""),r}},{key:"formatValueProp",value:function(){var e=this.props,t=e.format,r=e.decimalScale,n=e.fixedDecimalScale,a=this.props,o=a.value,i=a.isNumericString;if(void 0===o)return"";"number"==typeof o&&(o=o.toString(),i=!0),i&&!t&&"number"==typeof r&&(o=(0,h.roundToPrecision)(o,r,n));var u=i?this.formatNumString(o):this.formatInput(o);return u}},{key:"formatNegation",value:function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"",t=this.props.allowNegative,r=new RegExp("(-)"),n=new RegExp("(-)(.)*(-)"),a=r.test(e),o=n.test(e);return e=e.replace(/-/g,""),a&&!o&&t&&(e="-"+e),e}},{key:"formatInput",value:function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"",t=this.props.format;return t||(e=this.formatNegation(e)),e=this.removeFormatting(e),this.formatNumString(e)}},{key:"isCharacterAFormat",value:function(e,t){var r=this.props,n=r.format,a=r.prefix,o=r.suffix,i=r.decimalScale,u=r.fixedDecimalScale,s=this.getSeparators(),l=s.decimalSeparator;return"string"==typeof n&&"#"!==n[e]||!(n||!(e<a.length||e>=t.length-o.length||i&&u&&t[e]===l))}},{key:"checkIfFormatGotDeleted",value:function(e,t,r){for(var n=e;n<t;n++)if(this.isCharacterAFormat(n,r))return!0;return!1}},{key:"correctInputValue",value:function(e,t,r){var n=this.props.format,a=this.state.numAsString||"";if(r.length>=t.length||!r.length)return r;var o=e,i=(0,h.splitString)(t,e),u=(0,h.splitString)(r,e),s=i[1].lastIndexOf(u[1]),l=s!==-1?i[1].substring(0,s):"",f=o+l.length;if(this.checkIfFormatGotDeleted(o,f,t)&&(r=t),!n){var c=this.removeFormatting(r),p=this.splitDecimal(c),d=p.beforeDecimal,g=p.afterDecimal,m=p.addNegation;if(c.length<a.length&&""===d&&!parseFloat(g))return m?"-":""}return r}},{key:"onChange",value:function(e){e.persist();var t=e.target,r=t.value,n=this.state,a=this.props,o=a.isAllowed,i=n.value||"",u=Math.max(t.selectionStart,t.selectionEnd);r=this.correctInputValue(u,i,r);var s=this.formatInput(r)||"",l=this.removeFormatting(s),f={formattedValue:s,value:l,floatValue:parseFloat(l)};o(f)||(s=i),t.value=s;var c=this.getCaretPosition(r,s,Math.min(s.length,u));this.setPatchedCaretPosition(t,c,s),s!==i?this.setState({value:s,numAsString:l},function(){a.onValueChange(f),a.onChange(e)}):a.onChange(e)}},{key:"onBlur",value:function e(t){var r=this.props,n=this.state,a=r.format,e=r.onBlur,o=n.numAsString,i=n.value;if(!a){o=(0,h.fixLeadingZero)(o);var u=this.formatNumString(o),s={formattedValue:u,value:o,floatValue:parseFloat(o)};if(u!==i)return void this.setState({value:u,numAsString:o},function(){r.onValueChange(s),e(t)})}e(t)}},{key:"onKeyDown",value:function e(t){var r=t.target,n=t.key,a=r.selectionEnd,o=r.value,i=r.selectionStart,u=void 0,s=this.props,l=s.decimalScale,f=s.fixedDecimalScale,c=s.prefix,p=s.suffix,h=s.format,e=s.onKeyDown,d=void 0!==l&&f,g=this.getNumberRegex(!1,d),m=new RegExp("-"),v="string"==typeof h;if("ArrowLeft"===n||"Backspace"===n?u=i-1:"ArrowRight"===n?u=i+1:"Delete"===n&&(u=i),void 0===u||i!==a)return void e(t);var y=u,x=v?h.indexOf("#"):c.length,S=v?h.lastIndexOf("#")+1:o.length-p.length;if("ArrowLeft"===n||"ArrowRight"===n){var b="ArrowLeft"===n?"left":"right";y=this.correctCaretPosition(o,u,b)}else if("Delete"!==n||g.test(o[u])||m.test(o[u])){if("Backspace"===n&&!g.test(o[u])&&!m.test(o[u])){for(;!g.test(o[y-1])&&y>x;)y--;y=this.correctCaretPosition(o,y,"left")}}else for(;!g.test(o[y])&&y<S;)y++;(y!==u||u<x||u>S)&&(t.preventDefault(),this.setPatchedCaretPosition(r,y,o)),t.isUnitTestRun&&this.setPatchedCaretPosition(r,y,o),this.props.onKeyDown(t)}},{key:"onMouseUp",value:function(e){var t=e.target,r=t.selectionStart,n=t.selectionEnd,a=t.value;if(r===n){var o=this.correctCaretPosition(a,r);o!==r&&this.setPatchedCaretPosition(t,o,a)}this.props.onMouseUp(e)}},{key:"onFocus",value:function(e){var t=e.target,r=t.selectionStart,n=t.value,a=this.correctCaretPosition(n,r);a!==r&&this.setPatchedCaretPosition(t,a,n),this.props.onFocus(e)}},{key:"render",value:function(){var e=this.props,t=e.type,r=e.displayType,n=e.customInput,a=e.renderText,o=this.state.value,i=(0,h.omit)(this.props,d),s=u({},i,{type:t,value:o,onChange:this.onChange,onKeyDown:this.onKeyDown,onMouseUp:this.onMouseUp,onFocus:this.onFocus,onBlur:this.onBlur});if("text"===r)return a?a(o)||null:p.default.createElement("span",i,o);if(n){var l=n;return p.default.createElement(l,s)}return p.default.createElement("input",s)}}]),t}(p.default.Component);m.propTypes=d,m.defaultProps=g,e.exports=m},function(e,t,r){e.exports=r(3)()},function(e,t,r){"use strict";var n=r(4),a=r(5),o=r(6);e.exports=function(){function e(e,t,r,n,i,u){u!==o&&a(!1,"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")}function t(){return e}e.isRequired=e;var r={array:e,bool:e,func:e,number:e,object:e,string:e,symbol:e,any:e,arrayOf:t,element:e,instanceOf:t,node:e,objectOf:t,oneOf:t,oneOfType:t,shape:t,exact:t};return r.checkPropTypes=n,r.PropTypes=r,r}},function(e,t){"use strict";function r(e){return function(){return e}}var n=function(){};n.thatReturns=r,n.thatReturnsFalse=r(!1),n.thatReturnsTrue=r(!0),n.thatReturnsNull=r(null),n.thatReturnsThis=function(){return this},n.thatReturnsArgument=function(e){return e},e.exports=n},function(e,t,r){"use strict";function n(e,t,r,n,o,i,u,s){if(a(t),!e){var l;if(void 0===t)l=new Error("Minified exception occurred; use the non-minified dev environment for the full error message and additional helpful warnings.");else{var f=[r,n,o,i,u,s],c=0;l=new Error(t.replace(/%s/g,function(){return f[c++]})),l.name="Invariant Violation"}throw l.framesToPop=1,l}}var a=function(e){};e.exports=n},function(e,t){"use strict";var r="SECRET_DO_NOT_PASS_THIS_OR_YOU_WILL_BE_FIRED";e.exports=r},function(t,r){t.exports=e},function(e,t){"use strict";function r(){}function n(){return!0}function a(e){return!!(e||"").match(/\d/)}function o(e){return e.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g,"\\$&")}function i(e){if(!e)return e;var t="-"===e[0];t&&(e=e.substring(1,e.length));var r=e.split("."),n=r[0].replace(/^0+/,"")||"0",a=r[1]||"";return""+(t?"-":"")+n+(a?"."+a:"")}function u(e,t){return[e.substring(0,t),e.substring(t)]}function s(e,t,r){for(var n="",a=r?"0":"",o=0;o<=t-1;o++)n+=e[o]||a;return n}function l(e,t,r){var n=e.split("."),a=parseFloat("0."+(n[1]||"0")).toFixed(t).split("."),o=n[0].split("").reverse().reduce(function(e,t,r){return e.length>r?(Number(e[0])+Number(t)).toString()+e.substring(1,e.length):t+e},a[0]),i=s(a[1]||"",(n[1]||"").length,r);return o+(i?"."+i:"")}function f(e,t){var r={};return Object.keys(e).forEach(function(n){t[n]||(r[n]=e[n])}),r}function c(e,t){if(e.value=e.value,null!==e){if(e.createTextRange){var r=e.createTextRange();return r.move("character",t),r.select(),!0}return e.selectionStart||0===e.selectionStart?(e.focus(),e.setSelectionRange(t,t),!0):(e.focus(),!1)}}Object.defineProperty(t,"__esModule",{value:!0}),t.noop=r,t.returnTrue=n,t.charIsNumber=a,t.escapeRegExp=o,t.fixLeadingZero=i,t.splitString=u,t.limitToScale=s,t.roundToPrecision=l,t.omit=f,t.setCaretPosition=c}])});

@@ -338,3 +338,3 @@ 'use strict';

if (typeof format === 'string' && !stateValue) {
if (typeof format === 'string' && !stateValue || caretPos === formattedValue.length) {
//set it to the maximum value so it goes after the last number

@@ -341,0 +341,0 @@ j = formattedValue.length;

{
"name": "react-number-format",
"description": "React component to format number in an input or as a text.",
"version": "3.0.0-beta2",
"version": "3.0.0-beta3",
"main": "lib/number_format.js",

@@ -6,0 +6,0 @@ "author": "Sudhanshu Yadav",

@@ -309,5 +309,5 @@ //@flow

if (typeof format === 'string' && !stateValue) {
if ((typeof format === 'string' && !stateValue) || caretPos === formattedValue.length) {
//set it to the maximum value so it goes after the last number
j = formattedValue.length
j = formattedValue.length;
}

@@ -314,0 +314,0 @@

@@ -173,2 +173,14 @@ import React from 'react';

});
it('should maintain correct caret positon while removing the last character and suffix is not defined. Issue #105', () => {
wrapper.setProps({
suffix: '',
prefix: '$',
value: '$2,342,343'
});
wrapper.update();
simulateKeyInput(wrapper.find('input'), 'Backspace', 10, 10, setSelectionRange);
expect(wrapper.state().value).toEqual('$234,234');
expect(caretPos).toEqual(8);
})
})

@@ -175,0 +187,0 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc