react-number-format
Advanced tools
/*! | ||
* react-number-format - 2.0.0-alpha3 | ||
* react-number-format - 2.0.0-alpha4 | ||
* Author : Sudhanshu Yadav | ||
@@ -103,2 +103,4 @@ * Copyright (c) 2016,2017 to Sudhanshu Yadav - ignitersworld.com , released under the MIT license. | ||
function noop() {}; | ||
function escapeRegExp(str) { | ||
@@ -152,2 +154,4 @@ return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&"); | ||
type: 'text', | ||
onChange: noop, | ||
onKeyDown: noop, | ||
isAllowed: function isAllowed() { | ||
@@ -301,3 +305,3 @@ return true; | ||
key: 'setPatchedCaretPosition', | ||
value: function setPatchedCaretPosition(el, caretPos) { | ||
value: function setPatchedCaretPosition(el, caretPos, lastValue) { | ||
var _this2 = this; | ||
@@ -312,3 +316,3 @@ | ||
setTimeout(function () { | ||
return _this2.setCaretPosition(el, caretPos); | ||
if (el.value === lastValue) _this2.setCaretPosition(el, caretPos); | ||
}, 0); | ||
@@ -437,3 +441,3 @@ } | ||
if (!inputValue[i].match(numRegex) && inputValue[i] !== formattedValue[j]) continue; | ||
if (inputValue[i] === '0' && formattedValue[j].match(numRegex) && formattedValue[j] !== '0') continue; | ||
if (inputValue[i] === '0' && (formattedValue[j] || '').match(numRegex) && formattedValue[j] !== '0') continue; | ||
while (inputValue[i] !== formattedValue[j] && j < formattedValue.length) { | ||
@@ -447,12 +451,12 @@ j++; | ||
}, { | ||
key: 'onChangeHandler', | ||
value: function onChangeHandler(e, callback) { | ||
var _this3 = this; | ||
key: 'onChange', | ||
value: function onChange(e) { | ||
e.persist(); | ||
var el = e.target; | ||
var inputValue = el.value; | ||
var isAllowed = this.props.isAllowed; | ||
var state = this.state, | ||
props = this.props; | ||
var isAllowed = props.isAllowed; | ||
var lastValue = this.state.value; | ||
var lastValue = state.value; | ||
@@ -477,19 +481,17 @@ var _formatInput2 = this.formatInput(inputValue), | ||
//set the value imperatively, this is required for IE fix | ||
el.value = formattedValue; | ||
//reset again after setState so if formattedValue is other then | ||
this.setPatchedCaretPosition(el, cursorPos, lastValue); | ||
//change the state | ||
this.setState({ value: formattedValue }, function () { | ||
if (formattedValue !== lastValue) { | ||
this.setState({ value: formattedValue }, function () { | ||
props.onChange(e, value); | ||
}); | ||
} | ||
//reset again after setState so if formattedValue is other then | ||
_this3.setPatchedCaretPosition(el, cursorPos); | ||
if (callback && formattedValue !== lastValue) callback(e, value); | ||
}); | ||
return value; | ||
} | ||
}, { | ||
key: 'onChange', | ||
value: function onChange(e) { | ||
this.onChangeHandler(e, this.props.onChange); | ||
} | ||
}, { | ||
key: 'onKeyDown', | ||
@@ -523,3 +525,3 @@ value: function onKeyDown(e) { | ||
if (this.props.onKeyDown) this.props.onKeyDown(e); | ||
this.props.onKeyDown(e); | ||
} | ||
@@ -526,0 +528,0 @@ }, { |
/*! | ||
* react-number-format - 2.0.0-alpha3 | ||
* react-number-format - 2.0.0-alpha4 | ||
* 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)}function u(e){return e.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g,"\\$&")}function s(e){return e.replace(/^0+/,"")||"0"}function f(e,t){for(var r="",n=0;n<=t-1;n++)r+=e[n]||"0";return r}function l(e,t){var r={};return Object.keys(e).forEach(function(n){t[n]||(r[n]=e[n])}),r}var p=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},c=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}}(),d=r(2),h=n(d),v=r(7),m=n(v),y={thousandSeparator:h.default.oneOfType([h.default.string,h.default.oneOf([!0])]),decimalSeparator:h.default.string,decimalPrecision:h.default.number,displayType:h.default.oneOf(["input","text"]),prefix:h.default.string,suffix:h.default.string,format:h.default.oneOfType([h.default.string,h.default.func]),mask:h.default.string,value:h.default.oneOfType([h.default.number,h.default.string]),customInput:h.default.func,allowNegative:h.default.bool,onKeyDown:h.default.func,onChange:h.default.func,type:h.default.oneOf(["text","tel"]),isAllowed:h.default.func},g={displayType:"input",decimalSeparator:".",allowNegative:!0,type:"text",isAllowed:function(){return!0}},b=function(e){function t(e){a(this,t);var r=o(this,(t.__proto__||Object.getPrototypeOf(t)).call(this,e)),n=r.optimizeValueProp(e);return r.state={value:r.formatInput(n).formattedValue},r.onChange=r.onChange.bind(r),r.onKeyDown=r.onKeyDown.bind(r),r}return i(t,e),c(t,[{key:"componentDidUpdate",value:function(e,t){this.updateValueIfRequired(e,t)}},{key:"updateValueIfRequired",value:function(e){var t=this.props,r=this.state;if(e!==t){var n=r.value,a=this.optimizeValueProp(t);void 0===a&&(a=n);var o=this.formatInput(a),i=o.formattedValue;i!==n&&this.setState({value:i})}}},{key:"getFloatValue",value:function(e){var t=this.props.decimalSeparator;return parseFloat(e.replace(t,"."))||0}},{key:"optimizeValueProp",value:function(e){var t=this.getSeparators(e),r=t.decimalSeparator,n=e.decimalPrecision,a=e.format,o=e.value;if(a||void 0===o)return o;var i="number"==typeof o;return i&&(o=o.toString()),r&&i&&(o=o.replace(".",r)),0===n?o.split(r)[0]:o}},{key:"getSeparators",value:function(e){e=e||this.props;var t=e,r=t.decimalSeparator,n=e,a=n.thousandSeparator;if(a===!0&&(a=","),r===a)throw new Error("\n Decimal separator can't be same as thousand separator.\n\n thousandSeparator: "+a+' (thousandSeparator = {true} is same as thousandSeparator = ",")\n decimalSeparator: '+r+" (default value for decimalSeparator is .)\n ");return{decimalSeparator:r,thousandSeparator:a}}},{key:"getNumberRegex",value:function(e,t){var r=this.props,n=r.format,a=r.decimalPrecision,o=this.getSeparators(),i=o.decimalSeparator;return new RegExp("\\d"+(!i||0===a||t||n?"":"|"+u(i)),e?"g":void 0)}},{key:"setCaretPosition",value:function(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)}}},{key:"setPatchedCaretPosition",value:function(e,t){var r=this;this.setCaretPosition(e,t),setTimeout(function(){return r.setCaretPosition(e,t)},0)}},{key:"formatWithPattern",value:function(e){var t=this.props,r=t.format,n=t.mask;if(!r)return e;for(var a=r.split("#").length-1,o=0,i=r,u=0,s=e.length;u<s;u++)u<a&&(o=i.indexOf("#"),i=i.replace("#",e[u]));var f=i.lastIndexOf("#");return n?i.replace(/#/g,n):i.substring(0,o+1)+(f!==-1?i.substring(f+1,i.length):"")}},{key:"formatInput",value:function(e){var t=this.props,r=t.prefix,n=t.suffix,a=(t.mask,t.format),o=t.allowNegative,i=t.decimalPrecision,u=this.getSeparators(),l=u.thousandSeparator,p=u.decimalSeparator,c=this.getNumberRegex(!0),d=void 0,h=void 0;"number"==typeof e&&(e+="");var v=new RegExp("(-)"),m=new RegExp("(-)(.)*(-)");o&&!a&&(d=v.test(e),h=m.test(e));var y=e&&e.match(c);if(!y&&h)return{value:"",formattedValue:""};if(!y&&d)return{value:"",formattedValue:"-"};if(!y)return{value:"",formattedValue:""};var g=e.match(c).join(""),b=g;if(a)"string"==typeof a?b=this.formatWithPattern(b):"function"==typeof a&&(b=a(b));else{var x=b.indexOf(p)!==-1||i,P=b.split(p),S=P[0],w=P[1]||"";S=s(S),void 0!==i&&(w=f(w,i)),l&&(S=S.replace(/(\d)(?=(\d{3})+(?!\d))/g,"$1"+l)),r&&(S=r+S),n&&(w+=n),d&&!h&&(S="-"+S),b=S+(x&&p||"")+w}return{value:(d&&!h?"-":"")+b.match(c).join(""),formattedValue:b}}},{key:"getCursorPosition",value:function(e,t,r){var n=this.getNumberRegex(),a=void 0,o=void 0;for(a=0,o=0;o<r;o++)if((e[o].match(n)||e[o]===t[a])&&("0"!==e[o]||!t[a].match(n)||"0"===t[a])){for(;e[o]!==t[a]&&a<t.length;)a++;a++}return a}},{key:"onChangeHandler",value:function(e,t){var r=this;e.persist();var n=e.target,a=n.value,o=this.props.isAllowed,i=this.state.value,u=this.formatInput(a),s=u.formattedValue,f=u.value,l=Math.max(n.selectionStart,n.selectionEnd),p=this.getCursorPosition(a,s,l);return o(s,f,this.getFloatValue(f))||(s=i),this.setState({value:s},function(){r.setPatchedCaretPosition(n,p),t&&s!==i&&t(e,f)}),f}},{key:"onChange",value:function(e){this.onChangeHandler(e,this.props.onChange)}},{key:"onKeyDown",value:function(e){var t=e.target,r=t.selectionStart,n=t.selectionEnd,a=t.value,o=this.props.decimalPrecision,i=e.key,u=this.getNumberRegex(!1,void 0!==o),s=new RegExp("-");if(n-r===0)if("Delete"!==i||u.test(a[r])||s.test(a[r])){if("Backspace"===i&&!u.test(a[r-1])&&!s.test(a[r-1])){e.preventDefault();for(var f=r;!u.test(a[f-1])&&f>0;)f--;this.setPatchedCaretPosition(t,f)}}else{e.preventDefault();for(var l=r;!u.test(a[l])&&l<a.length;)l++;this.setPatchedCaretPosition(t,l)}this.props.onKeyDown&&this.props.onKeyDown(e)}},{key:"render",value:function(){var e=l(this.props,y),t=p({},e,{type:this.props.type,value:this.state.value,onChange:this.onChange,onKeyDown:this.onKeyDown});if("text"===this.props.displayType)return m.default.createElement("span",e,this.state.value);if(this.props.customInput){var r=this.props.customInput;return m.default.createElement(r,t)}return m.default.createElement("input",t)}}]),t}(m.default.Component);b.propTypes=y,b.defaultProps=g,e.exports=b},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};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 f;if(void 0===t)f=new Error("Minified exception occurred; use the non-minified dev environment for the full error message and additional helpful warnings.");else{var l=[r,n,o,i,u,s],p=0;f=new Error(t.replace(/%s/g,function(){return l[p++]})),f.name="Invariant Violation"}throw f.framesToPop=1,f}}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){"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)}function u(){}function s(e){return e.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g,"\\$&")}function f(e){return e.replace(/^0+/,"")||"0"}function l(e,t){for(var r="",n=0;n<=t-1;n++)r+=e[n]||"0";return r}function p(e,t){var r={};return Object.keys(e).forEach(function(n){t[n]||(r[n]=e[n])}),r}var c=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},d=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}}(),h=r(2),v=n(h),m=r(7),y=n(m),g={thousandSeparator:v.default.oneOfType([v.default.string,v.default.oneOf([!0])]),decimalSeparator:v.default.string,decimalPrecision:v.default.number,displayType:v.default.oneOf(["input","text"]),prefix:v.default.string,suffix:v.default.string,format:v.default.oneOfType([v.default.string,v.default.func]),mask:v.default.string,value:v.default.oneOfType([v.default.number,v.default.string]),customInput:v.default.func,allowNegative:v.default.bool,onKeyDown:v.default.func,onChange:v.default.func,type:v.default.oneOf(["text","tel"]),isAllowed:v.default.func},b={displayType:"input",decimalSeparator:".",allowNegative:!0,type:"text",onChange:u,onKeyDown:u,isAllowed:function(){return!0}},x=function(e){function t(e){a(this,t);var r=o(this,(t.__proto__||Object.getPrototypeOf(t)).call(this,e)),n=r.optimizeValueProp(e);return r.state={value:r.formatInput(n).formattedValue},r.onChange=r.onChange.bind(r),r.onKeyDown=r.onKeyDown.bind(r),r}return i(t,e),d(t,[{key:"componentDidUpdate",value:function(e,t){this.updateValueIfRequired(e,t)}},{key:"updateValueIfRequired",value:function(e){var t=this.props,r=this.state;if(e!==t){var n=r.value,a=this.optimizeValueProp(t);void 0===a&&(a=n);var o=this.formatInput(a),i=o.formattedValue;i!==n&&this.setState({value:i})}}},{key:"getFloatValue",value:function(e){var t=this.props.decimalSeparator;return parseFloat(e.replace(t,"."))||0}},{key:"optimizeValueProp",value:function(e){var t=this.getSeparators(e),r=t.decimalSeparator,n=e.decimalPrecision,a=e.format,o=e.value;if(a||void 0===o)return o;var i="number"==typeof o;return i&&(o=o.toString()),r&&i&&(o=o.replace(".",r)),0===n?o.split(r)[0]:o}},{key:"getSeparators",value:function(e){e=e||this.props;var t=e,r=t.decimalSeparator,n=e,a=n.thousandSeparator;if(a===!0&&(a=","),r===a)throw new Error("\n Decimal separator can't be same as thousand separator.\n\n thousandSeparator: "+a+' (thousandSeparator = {true} is same as thousandSeparator = ",")\n decimalSeparator: '+r+" (default value for decimalSeparator is .)\n ");return{decimalSeparator:r,thousandSeparator:a}}},{key:"getNumberRegex",value:function(e,t){var r=this.props,n=r.format,a=r.decimalPrecision,o=this.getSeparators(),i=o.decimalSeparator;return new RegExp("\\d"+(!i||0===a||t||n?"":"|"+s(i)),e?"g":void 0)}},{key:"setCaretPosition",value:function(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)}}},{key:"setPatchedCaretPosition",value:function(e,t,r){var n=this;this.setCaretPosition(e,t),setTimeout(function(){e.value===r&&n.setCaretPosition(e,t)},0)}},{key:"formatWithPattern",value:function(e){var t=this.props,r=t.format,n=t.mask;if(!r)return e;for(var a=r.split("#").length-1,o=0,i=r,u=0,s=e.length;u<s;u++)u<a&&(o=i.indexOf("#"),i=i.replace("#",e[u]));var f=i.lastIndexOf("#");return n?i.replace(/#/g,n):i.substring(0,o+1)+(f!==-1?i.substring(f+1,i.length):"")}},{key:"formatInput",value:function(e){var t=this.props,r=t.prefix,n=t.suffix,a=(t.mask,t.format),o=t.allowNegative,i=t.decimalPrecision,u=this.getSeparators(),s=u.thousandSeparator,p=u.decimalSeparator,c=this.getNumberRegex(!0),d=void 0,h=void 0;"number"==typeof e&&(e+="");var v=new RegExp("(-)"),m=new RegExp("(-)(.)*(-)");o&&!a&&(d=v.test(e),h=m.test(e));var y=e&&e.match(c);if(!y&&h)return{value:"",formattedValue:""};if(!y&&d)return{value:"",formattedValue:"-"};if(!y)return{value:"",formattedValue:""};var g=e.match(c).join(""),b=g;if(a)"string"==typeof a?b=this.formatWithPattern(b):"function"==typeof a&&(b=a(b));else{var x=b.indexOf(p)!==-1||i,P=b.split(p),S=P[0],w=P[1]||"";S=f(S),void 0!==i&&(w=l(w,i)),s&&(S=S.replace(/(\d)(?=(\d{3})+(?!\d))/g,"$1"+s)),r&&(S=r+S),n&&(w+=n),d&&!h&&(S="-"+S),b=S+(x&&p||"")+w}return{value:(d&&!h?"-":"")+b.match(c).join(""),formattedValue:b}}},{key:"getCursorPosition",value:function(e,t,r){var n=this.getNumberRegex(),a=void 0,o=void 0;for(a=0,o=0;o<r;o++)if((e[o].match(n)||e[o]===t[a])&&("0"!==e[o]||!(t[a]||"").match(n)||"0"===t[a])){for(;e[o]!==t[a]&&a<t.length;)a++;a++}return a}},{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=this.formatInput(r),s=u.formattedValue,f=u.value,l=Math.max(t.selectionStart,t.selectionEnd),p=this.getCursorPosition(r,s,l);return o(s,f,this.getFloatValue(f))||(s=i),t.value=s,this.setPatchedCaretPosition(t,p,i),s!==i&&this.setState({value:s},function(){a.onChange(e,f)}),f}},{key:"onKeyDown",value:function(e){var t=e.target,r=t.selectionStart,n=t.selectionEnd,a=t.value,o=this.props.decimalPrecision,i=e.key,u=this.getNumberRegex(!1,void 0!==o),s=new RegExp("-");if(n-r===0)if("Delete"!==i||u.test(a[r])||s.test(a[r])){if("Backspace"===i&&!u.test(a[r-1])&&!s.test(a[r-1])){e.preventDefault();for(var f=r;!u.test(a[f-1])&&f>0;)f--;this.setPatchedCaretPosition(t,f)}}else{e.preventDefault();for(var l=r;!u.test(a[l])&&l<a.length;)l++;this.setPatchedCaretPosition(t,l)}this.props.onKeyDown(e)}},{key:"render",value:function(){var e=p(this.props,g),t=c({},e,{type:this.props.type,value:this.state.value,onChange:this.onChange,onKeyDown:this.onKeyDown});if("text"===this.props.displayType)return y.default.createElement("span",e,this.state.value);if(this.props.customInput){var r=this.props.customInput;return y.default.createElement(r,t)}return y.default.createElement("input",t)}}]),t}(y.default.Component);x.propTypes=g,x.defaultProps=b,e.exports=x},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};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 f;if(void 0===t)f=new Error("Minified exception occurred; use the non-minified dev environment for the full error message and additional helpful warnings.");else{var l=[r,n,o,i,u,s],p=0;f=new Error(t.replace(/%s/g,function(){return l[p++]})),f.name="Invariant Violation"}throw f.framesToPop=1,f}}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}])}); |
@@ -15,2 +15,5 @@ import React from 'react'; | ||
formatExpiryChange(val) { | ||
if(val && val.length > 1 && Number(val[0]) < 1 && Number(val[1]) < 1){ | ||
val = '01'+val.substring(2,val.length); | ||
} | ||
if(val && Number(val[0]) > 1){ | ||
@@ -17,0 +20,0 @@ val = '0'+val; |
@@ -35,2 +35,4 @@ 'use strict'; | ||
function noop() {}; | ||
function escapeRegExp(str) { | ||
@@ -84,2 +86,4 @@ return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&"); | ||
type: 'text', | ||
onChange: noop, | ||
onKeyDown: noop, | ||
isAllowed: function isAllowed() { | ||
@@ -233,3 +237,3 @@ return true; | ||
key: 'setPatchedCaretPosition', | ||
value: function setPatchedCaretPosition(el, caretPos) { | ||
value: function setPatchedCaretPosition(el, caretPos, lastValue) { | ||
var _this2 = this; | ||
@@ -244,3 +248,3 @@ | ||
setTimeout(function () { | ||
return _this2.setCaretPosition(el, caretPos); | ||
if (el.value === lastValue) _this2.setCaretPosition(el, caretPos); | ||
}, 0); | ||
@@ -369,3 +373,3 @@ } | ||
if (!inputValue[i].match(numRegex) && inputValue[i] !== formattedValue[j]) continue; | ||
if (inputValue[i] === '0' && formattedValue[j].match(numRegex) && formattedValue[j] !== '0') continue; | ||
if (inputValue[i] === '0' && (formattedValue[j] || '').match(numRegex) && formattedValue[j] !== '0') continue; | ||
while (inputValue[i] !== formattedValue[j] && j < formattedValue.length) { | ||
@@ -379,12 +383,12 @@ j++; | ||
}, { | ||
key: 'onChangeHandler', | ||
value: function onChangeHandler(e, callback) { | ||
var _this3 = this; | ||
key: 'onChange', | ||
value: function onChange(e) { | ||
e.persist(); | ||
var el = e.target; | ||
var inputValue = el.value; | ||
var isAllowed = this.props.isAllowed; | ||
var state = this.state, | ||
props = this.props; | ||
var isAllowed = props.isAllowed; | ||
var lastValue = this.state.value; | ||
var lastValue = state.value; | ||
@@ -409,19 +413,17 @@ var _formatInput2 = this.formatInput(inputValue), | ||
//set the value imperatively, this is required for IE fix | ||
el.value = formattedValue; | ||
//reset again after setState so if formattedValue is other then | ||
this.setPatchedCaretPosition(el, cursorPos, lastValue); | ||
//change the state | ||
this.setState({ value: formattedValue }, function () { | ||
if (formattedValue !== lastValue) { | ||
this.setState({ value: formattedValue }, function () { | ||
props.onChange(e, value); | ||
}); | ||
} | ||
//reset again after setState so if formattedValue is other then | ||
_this3.setPatchedCaretPosition(el, cursorPos); | ||
if (callback && formattedValue !== lastValue) callback(e, value); | ||
}); | ||
return value; | ||
} | ||
}, { | ||
key: 'onChange', | ||
value: function onChange(e) { | ||
this.onChangeHandler(e, this.props.onChange); | ||
} | ||
}, { | ||
key: 'onKeyDown', | ||
@@ -455,3 +457,3 @@ value: function onKeyDown(e) { | ||
if (this.props.onKeyDown) this.props.onKeyDown(e); | ||
this.props.onKeyDown(e); | ||
} | ||
@@ -458,0 +460,0 @@ }, { |
{ | ||
"name": "react-number-format", | ||
"description": "React component to format number in an input or as a text.", | ||
"version": "2.0.0-alpha3", | ||
"version": "2.0.0-alpha4", | ||
"main": "lib/number_format.js", | ||
@@ -6,0 +6,0 @@ "author": "Sudhanshu Yadav", |
@@ -16,2 +16,4 @@ /** | ||
function noop(){}; | ||
function escapeRegExp(str) { | ||
@@ -71,2 +73,4 @@ return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&"); | ||
type: 'text', | ||
onChange: noop, | ||
onKeyDown: noop, | ||
isAllowed: function() {return true;} | ||
@@ -193,3 +197,3 @@ }; | ||
setPatchedCaretPosition(el, caretPos) { | ||
setPatchedCaretPosition(el, caretPos, lastValue) { | ||
/* | ||
@@ -201,3 +205,5 @@ setting caret position within timeout of 0ms is required for mobile chrome, | ||
this.setCaretPosition(el, caretPos); | ||
setTimeout(() => this.setCaretPosition(el, caretPos), 0); | ||
setTimeout(() => { | ||
if(el.value === lastValue) this.setCaretPosition(el, caretPos); | ||
}, 0); | ||
} | ||
@@ -309,3 +315,3 @@ | ||
if(!inputValue[i].match(numRegex) && inputValue[i] !== formattedValue[j]) continue; | ||
if (inputValue[i] === '0' && formattedValue[j].match(numRegex) && formattedValue[j] !== '0') continue; | ||
if (inputValue[i] === '0' && (formattedValue[j]||'').match(numRegex) && formattedValue[j] !== '0') continue; | ||
while(inputValue[i] !== formattedValue[j] && j<formattedValue.length) j++; | ||
@@ -318,8 +324,9 @@ j++; | ||
onChangeHandler(e,callback) { | ||
onChange(e) { | ||
e.persist(); | ||
const el = e.target; | ||
const inputValue = el.value; | ||
const {isAllowed} = this.props; | ||
const lastValue = this.state.value; | ||
const {state, props} = this; | ||
const {isAllowed} = props; | ||
const lastValue = state.value; | ||
let {formattedValue, value} = this.formatInput(inputValue); | ||
@@ -339,10 +346,14 @@ | ||
//change the state | ||
this.setState({value : formattedValue},()=>{ | ||
//set the value imperatively, this is required for IE fix | ||
el.value = formattedValue; | ||
//reset again after setState so if formattedValue is other then | ||
this.setPatchedCaretPosition(el, cursorPos, lastValue); | ||
//reset again after setState so if formattedValue is other then | ||
this.setPatchedCaretPosition(el, cursorPos); | ||
if(callback && formattedValue !== lastValue) callback(e, value); | ||
}); | ||
//change the state | ||
if (formattedValue !== lastValue) { | ||
this.setState({value : formattedValue},()=>{ | ||
props.onChange(e, value); | ||
}); | ||
} | ||
@@ -352,5 +363,2 @@ return value; | ||
onChange(e) { | ||
this.onChangeHandler(e,this.props.onChange); | ||
} | ||
onKeyDown(e) { | ||
@@ -378,3 +386,3 @@ const el = e.target; | ||
if (this.props.onKeyDown) this.props.onKeyDown(e); | ||
this.props.onKeyDown(e); | ||
} | ||
@@ -381,0 +389,0 @@ render() { |
129411
0.28%2268
0.71%