react-number-format
Advanced tools
Comparing version 3.1.6 to 3.2.0
/*! | ||
* react-number-format - 3.1.6 | ||
* react-number-format - 3.2.0 | ||
* Author : Sudhanshu Yadav | ||
@@ -323,7 +323,10 @@ * Copyright (c) 2016,2018 to Sudhanshu Yadav - ignitersworld.com , released under the MIT license. | ||
//caret position should be between 0 and value length | ||
caretPos = (0, _utils.clamp)(caretPos, 0, value.length); | ||
//in case of format as number limit between prefix and suffix | ||
if (!format) { | ||
var hasNegation = value[0] === '-'; | ||
return Math.min(Math.max(caretPos, prefix.length + (hasNegation ? 1 : 0)), value.length - suffix.length); | ||
return (0, _utils.clamp)(caretPos, prefix.length + (hasNegation ? 1 : 0), value.length - suffix.length); | ||
} | ||
@@ -347,3 +350,3 @@ | ||
//limit the cursor between the first # position and the last # position | ||
caretPos = Math.min(Math.max(caretPos, firstHashPosition), lastHashPosition + 1); | ||
caretPos = (0, _utils.clamp)(caretPos, firstHashPosition, lastHashPosition + 1); | ||
@@ -792,3 +795,3 @@ var nextPos = format.substring(caretPos, format.length).indexOf('#'); | ||
this.setState({ value: formattedValue, numAsString: numAsString }, function () { | ||
props.onValueChange(valueObj); | ||
props.onValueChange(valueObj, e); | ||
props.onChange(e); | ||
@@ -824,3 +827,3 @@ }); | ||
this.setState({ value: formattedValue, numAsString: numAsString }, function () { | ||
props.onValueChange(valueObj); | ||
props.onValueChange(valueObj, e); | ||
onBlur(e); | ||
@@ -1240,2 +1243,3 @@ }); | ||
exports.findChangedIndex = findChangedIndex; | ||
exports.clamp = clamp; | ||
@@ -1382,2 +1386,9 @@ | ||
/* | ||
Returns a number whose value is limited to the given range | ||
*/ | ||
function clamp(num, min, max) { | ||
return Math.min(Math.max(num, min), max); | ||
} | ||
/***/ }) | ||
@@ -1384,0 +1395,0 @@ /******/ ]) |
/*! | ||
* react-number-format - 3.1.6 | ||
* react-number-format - 3.2.0 | ||
* Author : Sudhanshu Yadav | ||
* Copyright (c) 2016,2018 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 o=r[n]={exports:{},id:n,loaded:!1};return e[n].call(o.exports,o,o.exports,t),o.loaded=!0,o.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 o(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function a(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,getInputRef: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,getInputRef:h.noop},m=function(e){function t(e){o(this,t);var r=a(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.selectionBeforeInput={selectionStart:0,selectionEnd:0},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,o=r.numAsString||"",a=void 0===t.value?this.formatNumString(o):this.formatValueProp();a!==n&&this.setState({value:a,numAsString:this.removeFormatting(a)})}}},{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),o="-"===e[0];o&&(e=e.replace("-","")),e=(e.match(n)||[]).join("").replace(r,".");var a=e.indexOf(".");return a!==-1&&(e=e.substring(0,a)+"."+e.substring(a+1,e.length).replace(new RegExp((0,h.escapeRegExp)(r),"g"),"")),o&&(e="-"+e),e}},{key:"getNumberRegex",value:function(e,t){var r=this.props,n=r.format,o=r.decimalScale,a=this.getSeparators(),i=a.decimalSeparator;return new RegExp("\\d"+(!i||0===o||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 o="string"===e?e:e.toString();if(o.match(/\d/g))throw new Error("\n Mask "+e+" should not contain numeric character;\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,o=n.prefix,a=n.suffix,i=n.format;if(!i){var u="-"===e[0];return Math.min(Math.max(t,o.length+(u?1:0)),e.length-a.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,o=this.state.value,a=this.getNumberRegex(!0),i=(e.match(a)||[]).join(""),u=(t.match(a)||[]).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(a)||f===c)&&("0"!==f||!c.match(a)||"0"===c||i.length===u.length)){for(;f!==t[s]&&s<t.length;)s++;s++}}return"string"!=typeof n||o||(s=t.length),s=this.correctCaretPosition(t,s)}},{key:"removePrefixAndSuffix",value:function(e){var t=this.props,r=t.format,n=t.prefix,o=t.suffix;if(!r&&e){var a="-"===e[0];a&&(e=e.substring(1,e.length)),e=n&&0===e.indexOf(n)?e.substring(n.length,e.length):e;var i=e.lastIndexOf(o);e=o&&i!==-1&&i===e.length-o.length?e.substring(0,i):e,a&&(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,o="",a=0,i=r.length;a<=i;a++){var u=r[a]||"",s=a===i?e.length:e.indexOf(u,n);if(s===-1){o=e;break}o+=e.substring(n,s),n=s+u.length}return(o.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(""),o=0,a=t.length;o<a;o++)"#"===t[o]&&(n[o]=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,o=t.prefix,a=t.suffix,i=t.allowNegative,u=this.getSeparators(),s=u.thousandSeparator,l=u.decimalSeparator,f=e.indexOf(".")!==-1||r&&n,c=(0,h.splitDecimal)(e,i),p=c.beforeDecimal,d=c.afterDecimal,g=c.addNegation;return void 0!==r&&(d=(0,h.limitToScale)(d,r,n)),s&&(p=p.replace(/(\d)(?=(\d{3})+(?!\d))/g,"$1"+s)),o&&(p=o+p),a&&(d+=a),g&&(p="-"+p),e=p+(f&&l||"")+d}},{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,o=this.props,a=o.value,i=o.isNumericString;if(void 0===a)return"";"number"==typeof a&&(a=a.toString(),i=!0),i&&!t&&"number"==typeof r&&(a=(0,h.roundToPrecision)(a,r,n));var u=i?this.formatNumString(a):this.formatInput(a);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("(-)(.)*(-)"),o=r.test(e),a=n.test(e);return e=e.replace(/-/g,""),o&&!a&&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,o=r.prefix,a=r.suffix,i=r.decimalScale,u=r.fixedDecimalScale,s=this.getSeparators(),l=s.decimalSeparator;return"string"==typeof n&&"#"!==n[e]||!(n||!(e<o.length||e>=t.length-a.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,o=n.format,a=n.decimalSeparator,i=n.allowNegative,u=this.state.numAsString||"",s=this.selectionBeforeInput,l=s.selectionStart,f=s.selectionEnd,c=(0,h.findChangedIndex)(t,r),p=c.start,d=c.end;if(r.length>t.length||!r.length||p===d||0===p&&d===t.length||0===l&&f===t.length)return r;if(this.checkIfFormatGotDeleted(p,d,t)&&(r=t),!o){var g=this.removeFormatting(r),m=(0,h.splitDecimal)(g,i),v=m.beforeDecimal,y=m.afterDecimal,x=m.addNegation,S=e<r.indexOf(a)+1;if(g.length<u.length&&S&&""===v&&!parseFloat(y))return x?"-":""}return r}},{key:"onChange",value:function(e){e.persist();var t=e.target,r=t.value,n=this.state,o=this.props,a=o.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)};a(f)||(s=i),t.value=s;var c=this.getCaretPosition(r,s,u);this.setPatchedCaretPosition(t,c,s),s!==i?this.setState({value:s,numAsString:l},function(){o.onValueChange(f),o.onChange(e)}):o.onChange(e)}},{key:"onBlur",value:function e(t){var r=this.props,n=this.state,o=r.format,e=r.onBlur,a=n.numAsString,i=n.value;if(!o){a=(0,h.fixLeadingZero)(a);var u=this.formatNumString(a),s={formattedValue:u,value:a,floatValue:parseFloat(a)};if(u!==i)return t.persist(),void this.setState({value:u,numAsString:a},function(){r.onValueChange(s),e(t)})}e(t)}},{key:"onKeyDown",value:function e(t){var r=t.target,n=t.key,o=r.selectionStart,a=r.selectionEnd,i=r.value,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(this.selectionBeforeInput={selectionStart:o,selectionEnd:a},"ArrowLeft"===n||"Backspace"===n?u=o-1:"ArrowRight"===n?u=o+1:"Delete"===n&&(u=o),void 0===u||o!==a)return void e(t);var y=u,x=v?h.indexOf("#"):c.length,S=v?h.lastIndexOf("#")+1:i.length-p.length;if("ArrowLeft"===n||"ArrowRight"===n){var b="ArrowLeft"===n?"left":"right";y=this.correctCaretPosition(i,u,b)}else if("Delete"!==n||g.test(i[u])||m.test(i[u])){if("Backspace"===n&&!g.test(i[u])&&!m.test(i[u])){for(;!g.test(i[y-1])&&y>x;)y--;y=this.correctCaretPosition(i,y,"left")}}else for(;!g.test(i[y])&&y<S;)y++;(y!==u||u<x||u>S)&&(t.preventDefault(),this.setPatchedCaretPosition(r,y,i)),t.isUnitTestRun&&this.setPatchedCaretPosition(r,y,i),this.props.onKeyDown(t)}},{key:"onMouseUp",value:function(e){var t=e.target,r=t.selectionStart,n=t.selectionEnd,o=t.value;if(r===n){var a=this.correctCaretPosition(o,r);a!==r&&this.setPatchedCaretPosition(t,a,o)}this.props.onMouseUp(e)}},{key:"onFocus",value:function(e){var t=this;e.persist(),setTimeout(function(){var r=e.target,n=r.selectionStart,o=r.value,a=t.correctCaretPosition(o,n);a!==n&&t.setPatchedCaretPosition(r,a,o),t.props.onFocus(e)},0)}},{key:"render",value:function(){var e=this.props,t=e.type,r=e.displayType,n=e.customInput,o=e.renderText,a=e.getInputRef,i=this.state.value,s=(0,h.omit)(this.props,d),l=u({},s,{type:t,value:i,onChange:this.onChange,onKeyDown:this.onKeyDown,onMouseUp:this.onMouseUp,onFocus:this.onFocus,onBlur:this.onBlur});if("text"===r)return o?o(i)||null:p.default.createElement("span",u({},s,{ref:a}),i);if(n){var f=n;return p.default.createElement(f,l)}return p.default.createElement("input",u({},l,{ref:a}))}}]),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),o=r(5),a=r(6);e.exports=function(){function e(e,t,r,n,i,u){u!==a&&o(!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,a,i,u,s){if(o(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,a,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 o=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 o(e){return!!(e||"").match(/\d/)}function a(e){return e.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g,"\\$&")}function i(e){var t=!(arguments.length>1&&void 0!==arguments[1])||arguments[1],r="-"===e[0],n=r&&t;e=e.replace("-","");var o=e.split("."),a=o[0],i=o[1]||"";return{beforeDecimal:a,afterDecimal:i,hasNagation:r,addNegation:n}}function u(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",o=r[1]||"";return""+(t?"-":"")+n+(o?"."+o:"")}function s(e,t,r){for(var n="",o=r?"0":"",a=0;a<=t-1;a++)n+=e[a]||o;return n}function l(e,t,r){var n=e.indexOf(".")!==-1&&t,o=i(e),a=o.beforeDecimal,u=o.afterDecimal,l=o.hasNagation,f=parseFloat("0."+(u||"0")).toFixed(t).split("."),c=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},f[0]),p=s(f[1]||"",(u||"").length,r),h=l?"-":"",d=n?".":"";return""+h+c+d+p}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)}}function p(e,t){for(var r=0,n=0,o=e.length,a=t.length;e[r]===t[r]&&r<o;)r++;for(;e[o-1-n]===t[a-1-n]&&a-n>r&&o-n>r;)n++;return{start:r,end:o-n}}Object.defineProperty(t,"__esModule",{value:!0}),t.noop=r,t.returnTrue=n,t.charIsNumber=o,t.escapeRegExp=a,t.splitDecimal=i,t.fixLeadingZero=u,t.limitToScale=s,t.roundToPrecision=l,t.omit=f,t.setCaretPosition=c,t.findChangedIndex=p}])}); | ||
!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 o=r[n]={exports:{},id:n,loaded:!1};return e[n].call(o.exports,o,o.exports,t),o.loaded=!0,o.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 o(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function a(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,getInputRef: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,getInputRef:h.noop},m=function(e){function t(e){o(this,t);var r=a(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.selectionBeforeInput={selectionStart:0,selectionEnd:0},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,o=r.numAsString||"",a=void 0===t.value?this.formatNumString(o):this.formatValueProp();a!==n&&this.setState({value:a,numAsString:this.removeFormatting(a)})}}},{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),o="-"===e[0];o&&(e=e.replace("-","")),e=(e.match(n)||[]).join("").replace(r,".");var a=e.indexOf(".");return a!==-1&&(e=e.substring(0,a)+"."+e.substring(a+1,e.length).replace(new RegExp((0,h.escapeRegExp)(r),"g"),"")),o&&(e="-"+e),e}},{key:"getNumberRegex",value:function(e,t){var r=this.props,n=r.format,o=r.decimalScale,a=this.getSeparators(),i=a.decimalSeparator;return new RegExp("\\d"+(!i||0===o||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 o="string"===e?e:e.toString();if(o.match(/\d/g))throw new Error("\n Mask "+e+" should not contain numeric character;\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,o=n.prefix,a=n.suffix,i=n.format;if(t=(0,h.clamp)(t,0,e.length),!i){var u="-"===e[0];return(0,h.clamp)(t,o.length+(u?1:0),e.length-a.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=(0,h.clamp)(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,o=this.state.value,a=this.getNumberRegex(!0),i=(e.match(a)||[]).join(""),u=(t.match(a)||[]).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(a)||f===c)&&("0"!==f||!c.match(a)||"0"===c||i.length===u.length)){for(;f!==t[s]&&s<t.length;)s++;s++}}return"string"!=typeof n||o||(s=t.length),s=this.correctCaretPosition(t,s)}},{key:"removePrefixAndSuffix",value:function(e){var t=this.props,r=t.format,n=t.prefix,o=t.suffix;if(!r&&e){var a="-"===e[0];a&&(e=e.substring(1,e.length)),e=n&&0===e.indexOf(n)?e.substring(n.length,e.length):e;var i=e.lastIndexOf(o);e=o&&i!==-1&&i===e.length-o.length?e.substring(0,i):e,a&&(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,o="",a=0,i=r.length;a<=i;a++){var u=r[a]||"",s=a===i?e.length:e.indexOf(u,n);if(s===-1){o=e;break}o+=e.substring(n,s),n=s+u.length}return(o.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(""),o=0,a=t.length;o<a;o++)"#"===t[o]&&(n[o]=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,o=t.prefix,a=t.suffix,i=t.allowNegative,u=this.getSeparators(),s=u.thousandSeparator,l=u.decimalSeparator,f=e.indexOf(".")!==-1||r&&n,c=(0,h.splitDecimal)(e,i),p=c.beforeDecimal,d=c.afterDecimal,g=c.addNegation;return void 0!==r&&(d=(0,h.limitToScale)(d,r,n)),s&&(p=p.replace(/(\d)(?=(\d{3})+(?!\d))/g,"$1"+s)),o&&(p=o+p),a&&(d+=a),g&&(p="-"+p),e=p+(f&&l||"")+d}},{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,o=this.props,a=o.value,i=o.isNumericString;if(void 0===a)return"";"number"==typeof a&&(a=a.toString(),i=!0),i&&!t&&"number"==typeof r&&(a=(0,h.roundToPrecision)(a,r,n));var u=i?this.formatNumString(a):this.formatInput(a);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("(-)(.)*(-)"),o=r.test(e),a=n.test(e);return e=e.replace(/-/g,""),o&&!a&&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,o=r.prefix,a=r.suffix,i=r.decimalScale,u=r.fixedDecimalScale,s=this.getSeparators(),l=s.decimalSeparator;return"string"==typeof n&&"#"!==n[e]||!(n||!(e<o.length||e>=t.length-a.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,o=n.format,a=n.decimalSeparator,i=n.allowNegative,u=this.state.numAsString||"",s=this.selectionBeforeInput,l=s.selectionStart,f=s.selectionEnd,c=(0,h.findChangedIndex)(t,r),p=c.start,d=c.end;if(r.length>t.length||!r.length||p===d||0===p&&d===t.length||0===l&&f===t.length)return r;if(this.checkIfFormatGotDeleted(p,d,t)&&(r=t),!o){var g=this.removeFormatting(r),m=(0,h.splitDecimal)(g,i),v=m.beforeDecimal,y=m.afterDecimal,x=m.addNegation,S=e<r.indexOf(a)+1;if(g.length<u.length&&S&&""===v&&!parseFloat(y))return x?"-":""}return r}},{key:"onChange",value:function(e){e.persist();var t=e.target,r=t.value,n=this.state,o=this.props,a=o.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)};a(f)||(s=i),t.value=s;var c=this.getCaretPosition(r,s,u);this.setPatchedCaretPosition(t,c,s),s!==i?this.setState({value:s,numAsString:l},function(){o.onValueChange(f,e),o.onChange(e)}):o.onChange(e)}},{key:"onBlur",value:function e(t){var r=this.props,n=this.state,o=r.format,e=r.onBlur,a=n.numAsString,i=n.value;if(!o){a=(0,h.fixLeadingZero)(a);var u=this.formatNumString(a),s={formattedValue:u,value:a,floatValue:parseFloat(a)};if(u!==i)return t.persist(),void this.setState({value:u,numAsString:a},function(){r.onValueChange(s,t),e(t)})}e(t)}},{key:"onKeyDown",value:function e(t){var r=t.target,n=t.key,o=r.selectionStart,a=r.selectionEnd,i=r.value,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(this.selectionBeforeInput={selectionStart:o,selectionEnd:a},"ArrowLeft"===n||"Backspace"===n?u=o-1:"ArrowRight"===n?u=o+1:"Delete"===n&&(u=o),void 0===u||o!==a)return void e(t);var y=u,x=v?h.indexOf("#"):c.length,S=v?h.lastIndexOf("#")+1:i.length-p.length;if("ArrowLeft"===n||"ArrowRight"===n){var b="ArrowLeft"===n?"left":"right";y=this.correctCaretPosition(i,u,b)}else if("Delete"!==n||g.test(i[u])||m.test(i[u])){if("Backspace"===n&&!g.test(i[u])&&!m.test(i[u])){for(;!g.test(i[y-1])&&y>x;)y--;y=this.correctCaretPosition(i,y,"left")}}else for(;!g.test(i[y])&&y<S;)y++;(y!==u||u<x||u>S)&&(t.preventDefault(),this.setPatchedCaretPosition(r,y,i)),t.isUnitTestRun&&this.setPatchedCaretPosition(r,y,i),this.props.onKeyDown(t)}},{key:"onMouseUp",value:function(e){var t=e.target,r=t.selectionStart,n=t.selectionEnd,o=t.value;if(r===n){var a=this.correctCaretPosition(o,r);a!==r&&this.setPatchedCaretPosition(t,a,o)}this.props.onMouseUp(e)}},{key:"onFocus",value:function(e){var t=this;e.persist(),setTimeout(function(){var r=e.target,n=r.selectionStart,o=r.value,a=t.correctCaretPosition(o,n);a!==n&&t.setPatchedCaretPosition(r,a,o),t.props.onFocus(e)},0)}},{key:"render",value:function(){var e=this.props,t=e.type,r=e.displayType,n=e.customInput,o=e.renderText,a=e.getInputRef,i=this.state.value,s=(0,h.omit)(this.props,d),l=u({},s,{type:t,value:i,onChange:this.onChange,onKeyDown:this.onKeyDown,onMouseUp:this.onMouseUp,onFocus:this.onFocus,onBlur:this.onBlur});if("text"===r)return o?o(i)||null:p.default.createElement("span",u({},s,{ref:a}),i);if(n){var f=n;return p.default.createElement(f,l)}return p.default.createElement("input",u({},l,{ref:a}))}}]),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),o=r(5),a=r(6);e.exports=function(){function e(e,t,r,n,i,u){u!==a&&o(!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,a,i,u,s){if(o(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,a,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 o=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 o(e){return!!(e||"").match(/\d/)}function a(e){return e.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g,"\\$&")}function i(e){var t=!(arguments.length>1&&void 0!==arguments[1])||arguments[1],r="-"===e[0],n=r&&t;e=e.replace("-","");var o=e.split("."),a=o[0],i=o[1]||"";return{beforeDecimal:a,afterDecimal:i,hasNagation:r,addNegation:n}}function u(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",o=r[1]||"";return""+(t?"-":"")+n+(o?"."+o:"")}function s(e,t,r){for(var n="",o=r?"0":"",a=0;a<=t-1;a++)n+=e[a]||o;return n}function l(e,t,r){var n=e.indexOf(".")!==-1&&t,o=i(e),a=o.beforeDecimal,u=o.afterDecimal,l=o.hasNagation,f=parseFloat("0."+(u||"0")).toFixed(t).split("."),c=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},f[0]),p=s(f[1]||"",(u||"").length,r),h=l?"-":"",d=n?".":"";return""+h+c+d+p}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)}}function p(e,t){for(var r=0,n=0,o=e.length,a=t.length;e[r]===t[r]&&r<o;)r++;for(;e[o-1-n]===t[a-1-n]&&a-n>r&&o-n>r;)n++;return{start:r,end:o-n}}function h(e,t,r){return Math.min(Math.max(e,t),r)}Object.defineProperty(t,"__esModule",{value:!0}),t.noop=r,t.returnTrue=n,t.charIsNumber=o,t.escapeRegExp=a,t.splitDecimal=i,t.fixLeadingZero=u,t.limitToScale=s,t.roundToPrecision=l,t.omit=f,t.setCaretPosition=c,t.findChangedIndex=p,t.clamp=h}])}); |
@@ -255,7 +255,10 @@ 'use strict'; | ||
//caret position should be between 0 and value length | ||
caretPos = (0, _utils.clamp)(caretPos, 0, value.length); | ||
//in case of format as number limit between prefix and suffix | ||
if (!format) { | ||
var hasNegation = value[0] === '-'; | ||
return Math.min(Math.max(caretPos, prefix.length + (hasNegation ? 1 : 0)), value.length - suffix.length); | ||
return (0, _utils.clamp)(caretPos, prefix.length + (hasNegation ? 1 : 0), value.length - suffix.length); | ||
} | ||
@@ -279,3 +282,3 @@ | ||
//limit the cursor between the first # position and the last # position | ||
caretPos = Math.min(Math.max(caretPos, firstHashPosition), lastHashPosition + 1); | ||
caretPos = (0, _utils.clamp)(caretPos, firstHashPosition, lastHashPosition + 1); | ||
@@ -724,3 +727,3 @@ var nextPos = format.substring(caretPos, format.length).indexOf('#'); | ||
this.setState({ value: formattedValue, numAsString: numAsString }, function () { | ||
props.onValueChange(valueObj); | ||
props.onValueChange(valueObj, e); | ||
props.onChange(e); | ||
@@ -756,3 +759,3 @@ }); | ||
this.setState({ value: formattedValue, numAsString: numAsString }, function () { | ||
props.onValueChange(valueObj); | ||
props.onValueChange(valueObj, e); | ||
onBlur(e); | ||
@@ -759,0 +762,0 @@ }); |
@@ -17,2 +17,3 @@ 'use strict'; | ||
exports.findChangedIndex = findChangedIndex; | ||
exports.clamp = clamp; | ||
@@ -157,2 +158,9 @@ | ||
return { start: i, end: prevLength - j }; | ||
} | ||
/* | ||
Returns a number whose value is limited to the given range | ||
*/ | ||
function clamp(num, min, max) { | ||
return Math.min(Math.max(num, min), max); | ||
} |
{ | ||
"name": "react-number-format", | ||
"description": "React component to format number in an input or as a text.", | ||
"version": "3.1.6", | ||
"version": "3.2.0", | ||
"main": "lib/number_format.js", | ||
@@ -6,0 +6,0 @@ "author": "Sudhanshu Yadav", |
@@ -54,3 +54,3 @@ # react-number-format | ||
| customInput | Component Reference | input | This allow supporting custom inputs with number format. | | ||
| onValueChange | (values) => {} | none | onValueChange handler accepts [values object](#values-object) | | ||
| onValueChange | (values, e) => {} | none | onValueChange handler accepts [values object](#values-object) and event object (change / blur) as second parameter | | ||
| isAllowed | ([values](#values-object)) => true or false | none | A checker function to check if input value is valid or not | | ||
@@ -86,2 +86,4 @@ | renderText | (formattedValue) => React Element | null | A renderText method useful if you want to render formattedValue in different element other than span. | | ||
7. onValueChange is not same as onChange. It gets called on different events. So don't make assumption about the event object (second parameter). It can be change event or blur event. | ||
### Examples | ||
@@ -88,0 +90,0 @@ #### Prefix and thousand separator : Format currency as text |
@@ -16,3 +16,4 @@ //@flow | ||
splitDecimal, | ||
findChangedIndex | ||
findChangedIndex, | ||
clamp | ||
} from './utils'; | ||
@@ -236,6 +237,9 @@ | ||
//caret position should be between 0 and value length | ||
caretPos = clamp(caretPos, 0, value.length); | ||
//in case of format as number limit between prefix and suffix | ||
if (!format) { | ||
const hasNegation = value[0] === '-'; | ||
return Math.min(Math.max(caretPos, prefix.length + (hasNegation ? 1 : 0)), (value.length - suffix.length)); | ||
return clamp(caretPos, prefix.length + (hasNegation ? 1 : 0), value.length - suffix.length); | ||
} | ||
@@ -259,3 +263,3 @@ | ||
//limit the cursor between the first # position and the last # position | ||
caretPos = Math.min(Math.max(caretPos, firstHashPosition), lastHashPosition + 1); | ||
caretPos = clamp(caretPos, firstHashPosition, lastHashPosition + 1); | ||
@@ -625,3 +629,3 @@ const nextPos = format.substring(caretPos, format.length).indexOf('#'); | ||
this.setState({value : formattedValue, numAsString}, () => { | ||
props.onValueChange(valueObj); | ||
props.onValueChange(valueObj, e); | ||
props.onChange(e); | ||
@@ -653,3 +657,3 @@ }); | ||
this.setState({value : formattedValue, numAsString}, () => { | ||
props.onValueChange(valueObj); | ||
props.onValueChange(valueObj, e); | ||
onBlur(e); | ||
@@ -656,0 +660,0 @@ }); |
@@ -136,1 +136,8 @@ //@flow | ||
} | ||
/* | ||
Returns a number whose value is limited to the given range | ||
*/ | ||
export function clamp(num: number, min: number, max: number) { | ||
return Math.min(Math.max(num, min), max); | ||
} |
@@ -5,2 +5,3 @@ import React from 'react'; | ||
import {simulateKeyInput, simulateMousUpEvent, simulateFocusEvent, shallow} from '../test_util'; | ||
import {cardExpiry} from '../../custom_formatters/card_expiry'; | ||
@@ -237,2 +238,9 @@ describe('Test character insertion', () => { | ||
}); | ||
it('should not move caret positon from left most to right most if left key pressed. #154', () => { | ||
const wrapper = shallow(<NumberFormat format={cardExpiry} value="11/11"/>); | ||
caretPos = undefined; | ||
simulateKeyInput(wrapper.find('input'), 'ArrowLeft', 0, 0, setSelectionRange); | ||
expect(caretPos).toEqual(0); | ||
}); | ||
}) | ||
@@ -310,2 +318,3 @@ | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
373426
4355
264