diff --git a/dist/react-number-format.js b/dist/react-number-format.js index 1ad5322f..5f93dc25 100644 --- a/dist/react-number-format.js +++ b/dist/react-number-format.js @@ -1,5 +1,5 @@ /*! - * react-number-format - 3.0.3 + * react-number-format - 3.1.0 * Author : Sudhanshu Yadav * Copyright (c) 2016,2017 to Sudhanshu Yadav - ignitersworld.com , released under the MIT license. */ @@ -113,7 +113,8 @@ return /******/ (function(modules) { // webpackBootstrap onBlur: _propTypes2.default.func, type: _propTypes2.default.oneOf(['text', 'tel']), isAllowed: _propTypes2.default.func, - renderText: _propTypes2.default.func + renderText: _propTypes2.default.func, + getInputRef: _propTypes2.default.func }; var defaultProps = { @@ -131,7 +132,8 @@ return /******/ (function(modules) { // webpackBootstrap onMouseUp: _utils.noop, onFocus: _utils.noop, onBlur: _utils.noop, - isAllowed: _utils.returnTrue + isAllowed: _utils.returnTrue, + getInputRef: _utils.noop }; var NumberFormat = function (_React$Component) { @@ -708,7 +710,9 @@ return /******/ (function(modules) { // webpackBootstrap }, { key: 'correctInputValue', value: function correctInputValue(caretPos, lastValue, value) { - var format = this.props.format; + var _props9 = this.props, + format = _props9.format, + decimalSeparator = _props9.decimalSeparator; var lastNumStr = this.state.numAsString || ''; @@ -742,7 +746,8 @@ return /******/ (function(modules) { // webpackBootstrap //clear only if something got deleted - if (numericString.length < lastNumStr.length && beforeDecimal === '' && !parseFloat(afterDecimal)) { + var isBeforeDecimalPoint = caretPos < value.indexOf(decimalSeparator) + 1; + if (numericString.length < lastNumStr.length && isBeforeDecimalPoint && beforeDecimal === '' && !parseFloat(afterDecimal)) { return addNegation ? '-' : ''; } } @@ -840,13 +845,13 @@ return /******/ (function(modules) { // webpackBootstrap var selectionStart = el.selectionStart; var expectedCaretPosition = void 0; - var _props9 = this.props, - decimalScale = _props9.decimalScale, - fixedDecimalScale = _props9.fixedDecimalScale, - prefix = _props9.prefix, - suffix = _props9.suffix, - format = _props9.format, - onKeyDown = _props9.onKeyDown; + var _props10 = this.props, + decimalScale = _props10.decimalScale, + fixedDecimalScale = _props10.fixedDecimalScale, + prefix = _props10.prefix, + suffix = _props10.suffix, + format = _props10.format, + onKeyDown = _props10.onKeyDown; var ignoreDecimalSeparator = decimalScale !== undefined && fixedDecimalScale; var numRegex = this.getNumberRegex(false, ignoreDecimalSeparator); @@ -941,16 +946,17 @@ return /******/ (function(modules) { // webpackBootstrap } _this2.props.onFocus(e); - }); + }, 0); } }, { key: 'render', value: function render() { - var _props10 = this.props, - type = _props10.type, - displayType = _props10.displayType, - customInput = _props10.customInput, - renderText = _props10.renderText; + var _props11 = this.props, + type = _props11.type, + displayType = _props11.displayType, + customInput = _props11.customInput, + renderText = _props11.renderText, + getInputRef = _props11.getInputRef; var value = this.state.value; @@ -963,13 +969,14 @@ return /******/ (function(modules) { // webpackBootstrap onKeyDown: this.onKeyDown, onMouseUp: this.onMouseUp, onFocus: this.onFocus, - onBlur: this.onBlur + onBlur: this.onBlur, + ref: getInputRef }); if (displayType === 'text') { return renderText ? renderText(value) || null : _react2.default.createElement( 'span', - otherProps, + _extends({}, otherProps, { ref: getInputRef }), value ); } else if (customInput) { diff --git a/dist/react-number-format.min.js b/dist/react-number-format.min.js index 754df354..030a0c3e 100644 --- a/dist/react-number-format.min.js +++ b/dist/react-number-format.min.js @@ -1,6 +1,6 @@ /*! - * react-number-format - 3.0.3 + * react-number-format - 3.1.0 * 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;t0&&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-c0&&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=t.length-o.length||i&&u&&t[e]===l))}},{key:"checkIfFormatGotDeleted",value:function(e,t,r){for(var n=e;n=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,m=p.afterDecimal,g=p.addNegation;if(c.lengthx;)y--;y=this.correctCaretPosition(o,y,"left")}}else for(;!m.test(o[y])&&yS)&&(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=this;e.persist(),setTimeout(function(){var r=e.target,n=r.selectionStart,a=r.value,o=t.correctCaretPosition(a,n);o!==n&&t.setPatchedCaretPosition(r,o,a),t.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);g.propTypes=d,g.defaultProps=m,e.exports=g},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}])}); \ No newline at end of file +!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;t0&&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("#"),f=i.lastIndexOf("#");t=Math.min(Math.max(t,s),f+1);for(var l=i.substring(t,i.length).indexOf("#"),c=t,p=t+(l===-1?0:l);c>s&&("#"!==i[c]||!(0,h.charIsNumber)(e[c]));)c-=1;var d=!(0,h.charIsNumber)(e[p])||"left"===r&&t!==s||t-c0&&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(),f=s.decimalSeparator;return"string"==typeof n&&"#"!==n[e]||!(n||!(e=t.length-o.length||i&&u&&t[e]===f))}},{key:"checkIfFormatGotDeleted",value:function(e,t,r){for(var n=e;n=t.length||!r.length)return r;var u=e,s=(0,h.splitString)(t,e),f=(0,h.splitString)(r,e),l=s[1].lastIndexOf(f[1]),c=l!==-1?s[1].substring(0,l):"",p=u+c.length;if(this.checkIfFormatGotDeleted(u,p,t)&&(r=t),!a){var d=this.removeFormatting(r),g=this.splitDecimal(d),m=g.beforeDecimal,v=g.afterDecimal,y=g.addNegation,x=ex;)y--;y=this.correctCaretPosition(o,y,"left")}}else for(;!g.test(o[y])&&yS)&&(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=this;e.persist(),setTimeout(function(){var r=e.target,n=r.selectionStart,a=r.value,o=t.correctCaretPosition(a,n);o!==n&&t.setPatchedCaretPosition(r,o,a),t.props.onFocus(e)},0)}},{key:"render",value:function(){var e=this.props,t=e.type,r=e.displayType,n=e.customInput,a=e.renderText,o=e.getInputRef,i=this.state.value,s=(0,h.omit)(this.props,d),f=u({},s,{type:t,value:i,onChange:this.onChange,onKeyDown:this.onKeyDown,onMouseUp:this.onMouseUp,onFocus:this.onFocus,onBlur:this.onBlur,ref:o});if("text"===r)return a?a(i)||null:p.default.createElement("span",u({},s,{ref:o}),i);if(n){var l=n;return p.default.createElement(l,f)}return p.default.createElement("input",f)}}]),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 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],c=0;f=new Error(t.replace(/%s/g,function(){return l[c++]})),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){"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 f(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 l(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=f,t.omit=l,t.setCaretPosition=c}])}); \ No newline at end of file diff --git a/lib/number_format.js b/lib/number_format.js index 5a831347..531eb479 100644 --- a/lib/number_format.js +++ b/lib/number_format.js @@ -45,7 +45,8 @@ var propTypes = { onBlur: _propTypes2.default.func, type: _propTypes2.default.oneOf(['text', 'tel']), isAllowed: _propTypes2.default.func, - renderText: _propTypes2.default.func + renderText: _propTypes2.default.func, + getInputRef: _propTypes2.default.func }; var defaultProps = { @@ -63,7 +64,8 @@ var defaultProps = { onMouseUp: _utils.noop, onFocus: _utils.noop, onBlur: _utils.noop, - isAllowed: _utils.returnTrue + isAllowed: _utils.returnTrue, + getInputRef: _utils.noop }; var NumberFormat = function (_React$Component) { @@ -640,7 +642,9 @@ var NumberFormat = function (_React$Component) { }, { key: 'correctInputValue', value: function correctInputValue(caretPos, lastValue, value) { - var format = this.props.format; + var _props9 = this.props, + format = _props9.format, + decimalSeparator = _props9.decimalSeparator; var lastNumStr = this.state.numAsString || ''; @@ -674,7 +678,8 @@ var NumberFormat = function (_React$Component) { //clear only if something got deleted - if (numericString.length < lastNumStr.length && beforeDecimal === '' && !parseFloat(afterDecimal)) { + var isBeforeDecimalPoint = caretPos < value.indexOf(decimalSeparator) + 1; + if (numericString.length < lastNumStr.length && isBeforeDecimalPoint && beforeDecimal === '' && !parseFloat(afterDecimal)) { return addNegation ? '-' : ''; } } @@ -772,13 +777,13 @@ var NumberFormat = function (_React$Component) { var selectionStart = el.selectionStart; var expectedCaretPosition = void 0; - var _props9 = this.props, - decimalScale = _props9.decimalScale, - fixedDecimalScale = _props9.fixedDecimalScale, - prefix = _props9.prefix, - suffix = _props9.suffix, - format = _props9.format, - onKeyDown = _props9.onKeyDown; + var _props10 = this.props, + decimalScale = _props10.decimalScale, + fixedDecimalScale = _props10.fixedDecimalScale, + prefix = _props10.prefix, + suffix = _props10.suffix, + format = _props10.format, + onKeyDown = _props10.onKeyDown; var ignoreDecimalSeparator = decimalScale !== undefined && fixedDecimalScale; var numRegex = this.getNumberRegex(false, ignoreDecimalSeparator); @@ -873,16 +878,17 @@ var NumberFormat = function (_React$Component) { } _this2.props.onFocus(e); - }); + }, 0); } }, { key: 'render', value: function render() { - var _props10 = this.props, - type = _props10.type, - displayType = _props10.displayType, - customInput = _props10.customInput, - renderText = _props10.renderText; + var _props11 = this.props, + type = _props11.type, + displayType = _props11.displayType, + customInput = _props11.customInput, + renderText = _props11.renderText, + getInputRef = _props11.getInputRef; var value = this.state.value; @@ -895,13 +901,14 @@ var NumberFormat = function (_React$Component) { onKeyDown: this.onKeyDown, onMouseUp: this.onMouseUp, onFocus: this.onFocus, - onBlur: this.onBlur + onBlur: this.onBlur, + ref: getInputRef }); if (displayType === 'text') { return renderText ? renderText(value) || null : _react2.default.createElement( 'span', - otherProps, + _extends({}, otherProps, { ref: getInputRef }), value ); } else if (customInput) { diff --git a/package.json b/package.json index a3bf31b5..5ad9b44a 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "react-number-format", "description": "React component to format number in an input or as a text.", - "version": "3.0.3", + "version": "3.1.0", "main": "lib/number_format.js", "author": "Sudhanshu Yadav", "license": "MIT", @@ -24,7 +24,7 @@ ], "scripts": { "start": "webpack-dev-server --content-base example/ --config webpack.dev.config.js --watch-poll --progress --inline --hot", - "bundle": "cross-env NODE_ENV=production npm run compile && cross-env NODE_ENV=production webpack --config webpack.bundle.config.js && cross-env NODE_ENV=production karma start", + "bundle": "cross-env NODE_ENV=production npm run compile && cross-env NODE_ENV=production webpack --config webpack.bundle.config.js && cross-env NODE_ENV=production TEST_BROWSER=ChromeHeadless karma start", "compile": "cross-env NODE_ENV=production babel src --out-dir lib", "test": "cross-env NODE_ENV=test karma start", "lint": "cross-env eslint src/**"