From 0cd0c9919499892d9d4ce54a4c016d9a2899c173 Mon Sep 17 00:00:00 2001 From: Sudhanshu Yadav Date: Tue, 31 Oct 2017 02:07:08 +0530 Subject: [PATCH] =?UTF-8?q?V3=20Release=20=F0=9F=8E=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 22 ++++++++++++++++++++-- dist/react-number-format.js | 25 ++++++++++++++++--------- dist/react-number-format.min.js | 4 ++-- lib/number_format.js | 23 +++++++++++++++-------- package.json | 2 +- 5 files changed, 54 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index 68336de3..820d0f1c 100644 --- a/README.md +++ b/README.md @@ -16,6 +16,23 @@ Through npm Or get compiled development and production version from ./dist +### Usage +ES6 +```js +import NumberFormat from 'react-number-format'; +``` + +ES5 +```js +const NumberFormat = require('react-number-format'); +``` + +Typescript +```js +import * as NumberFormat from 'react-number-format'; +``` + + ### Props | Props | Options | Default | Description | | ------------- |-------------| -----| -------- | @@ -118,7 +135,7 @@ Mask can also be a array of string. Each item corresponds to the same index #. ```jsx ``` - +![Screencast example](https://media.giphy.com/media/xT9IgojmLf6x3jX0nS/giphy.gif) #### Custom format method : Format credit card expiry time ```jsx @@ -155,6 +172,7 @@ function cardExpiry(val) { ```jsx ``` +![Screencast example](https://media.giphy.com/media/l1J9wJ6ZSONO7cXkI/giphy.gif) ### Custom Inputs You can easily extend your custom input with number format. But custom input should have all input props. @@ -190,7 +208,7 @@ All custom input props and number input props are passed together. - mask can be now array of string in which case mask at specific index will be mapped with the # of the pattern. - Value can be passed as string or number, but if it is passed as string it should be either formatted value or if it is a numeric string, you have to set isNumericString props to true. - Added support for numbers in prefix / suffix / pattern. -- Fixed caret position issues. +- Fixed caret position and formatting issues. - Lot of bugs and stability fixes ([See v3 tracker](https://github.com/s-yadav/react-number-format/issues/99)) ### v2.0.0 diff --git a/dist/react-number-format.js b/dist/react-number-format.js index 80e24d77..3e99e570 100644 --- a/dist/react-number-format.js +++ b/dist/react-number-format.js @@ -1,5 +1,5 @@ /*! - * react-number-format - 3.0.0-beta3 + * react-number-format - 3.0.0 * Author : Sudhanshu Yadav * Copyright (c) 2016,2017 to Sudhanshu Yadav - ignitersworld.com , released under the MIT license. */ @@ -922,17 +922,24 @@ return /******/ (function(modules) { // webpackBootstrap }, { key: 'onFocus', value: function onFocus(e) { - var el = e.target; - var selectionStart = el.selectionStart, - value = el.value; + var _this2 = this; + // Workaround Chrome and Safari bug https://bugs.chromium.org/p/chromium/issues/detail?id=779328 + // (onFocus event target selectionStart is always 0 before setTimeout) + e.persist(); + setTimeout(function () { + var el = e.target; + var selectionStart = el.selectionStart, + value = el.value; - var caretPostion = this.correctCaretPosition(value, selectionStart); - if (caretPostion !== selectionStart) { - this.setPatchedCaretPosition(el, caretPostion, value); - } - this.props.onFocus(e); + var caretPosition = _this2.correctCaretPosition(value, selectionStart); + if (caretPosition !== selectionStart) { + _this2.setPatchedCaretPosition(el, caretPosition, value); + } + + _this2.props.onFocus(e); + }); } }, { key: 'render', diff --git a/dist/react-number-format.min.js b/dist/react-number-format.min.js index 0122fdca..478b8ab3 100644 --- a/dist/react-number-format.min.js +++ b/dist/react-number-format.min.js @@ -1,6 +1,6 @@ /*! - * react-number-format - 3.0.0-beta3 + * react-number-format - 3.0.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,g=p.afterDecimal,m=p.addNegation;if(c.lengthx;)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=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}])}); \ 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("#"),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,g=p.afterDecimal,m=p.addNegation;if(c.lengthx;)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)})}},{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}])}); \ No newline at end of file diff --git a/lib/number_format.js b/lib/number_format.js index fc074e58..40362d29 100644 --- a/lib/number_format.js +++ b/lib/number_format.js @@ -854,17 +854,24 @@ var NumberFormat = function (_React$Component) { }, { key: 'onFocus', value: function onFocus(e) { - var el = e.target; - var selectionStart = el.selectionStart, - value = el.value; + var _this2 = this; + // Workaround Chrome and Safari bug https://bugs.chromium.org/p/chromium/issues/detail?id=779328 + // (onFocus event target selectionStart is always 0 before setTimeout) + e.persist(); + setTimeout(function () { + var el = e.target; + var selectionStart = el.selectionStart, + value = el.value; - var caretPostion = this.correctCaretPosition(value, selectionStart); - if (caretPostion !== selectionStart) { - this.setPatchedCaretPosition(el, caretPostion, value); - } - this.props.onFocus(e); + var caretPosition = _this2.correctCaretPosition(value, selectionStart); + if (caretPosition !== selectionStart) { + _this2.setPatchedCaretPosition(el, caretPosition, value); + } + + _this2.props.onFocus(e); + }); } }, { key: 'render', diff --git a/package.json b/package.json index 9554582d..f88bfb96 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.0-beta3", + "version": "3.0.0", "main": "lib/number_format.js", "author": "Sudhanshu Yadav", "license": "MIT",