react-number-format
Advanced tools
Comparing version 3.0.0-alpha3 to 3.0.0-beta
/*! | ||
* react-number-format - 3.0.0-alpha3 | ||
* react-number-format - 3.0.0-beta | ||
* Author : Sudhanshu Yadav | ||
@@ -290,3 +290,23 @@ * Copyright (c) 2016,2017 to Sudhanshu Yadav - ignitersworld.com , released under the MIT license. | ||
} | ||
}, { | ||
key: 'splitDecimal', | ||
value: function splitDecimal(numStr) { | ||
var allowNegative = this.props.allowNegative; | ||
var hasNagation = numStr[0] === '-'; | ||
var addNegation = hasNagation && allowNegative; | ||
numStr = numStr.replace('-', ''); | ||
var parts = numStr.split('.'); | ||
var beforeDecimal = parts[0]; | ||
var afterDecimal = parts[1] || ''; | ||
return { | ||
beforeDecimal: beforeDecimal, | ||
afterDecimal: afterDecimal, | ||
hasNagation: hasNagation, | ||
addNegation: addNegation | ||
}; | ||
} | ||
/** Misc methods end **/ | ||
@@ -519,3 +539,2 @@ | ||
fixedDecimalScale = _props5.fixedDecimalScale, | ||
allowNegative = _props5.allowNegative, | ||
prefix = _props5.prefix, | ||
@@ -528,24 +547,12 @@ suffix = _props5.suffix; | ||
// Check if its negative number and remove negation for futher formatting | ||
var hasNagation = numStr[0] === '-'; | ||
var addNegation = hasNagation && allowNegative; | ||
numStr = numStr.replace('-', ''); | ||
var hasDecimalSeparator = numStr.indexOf('.') !== -1 || decimalScale && fixedDecimalScale; | ||
var parts = numStr.split('.'); | ||
var beforeDecimal = parts[0]; | ||
var afterDecimal = parts[1] || ''; | ||
var _splitDecimal = this.splitDecimal(numStr), | ||
beforeDecimal = _splitDecimal.beforeDecimal, | ||
afterDecimal = _splitDecimal.afterDecimal, | ||
addNegation = _splitDecimal.addNegation; // eslint-disable-line prefer-const | ||
//if beforeDecimal is empty and after decimal is 0 clear the input while keeping the negation sign | ||
if (beforeDecimal === '' && !parseFloat(afterDecimal)) { | ||
return addNegation ? '-' : ''; | ||
} | ||
//apply decimal precision if its defined | ||
//remove leading zeros from number before decimal | ||
//beforeDecimal = removeLeadingZero(beforeDecimal); | ||
//apply decimal precision if its defined | ||
if (decimalScale !== undefined) afterDecimal = (0, _utils.limitToScale)(afterDecimal, decimalScale, fixedDecimalScale); | ||
@@ -710,2 +717,6 @@ | ||
value: function correctInputValue(caretPos, lastValue, value) { | ||
var format = this.props.format; | ||
var lastNumStr = this.state.numAsString || ''; | ||
//don't do anyhting if something got added, or if value is empty string (when whole input is cleared) | ||
@@ -728,2 +739,20 @@ if (value.length >= lastValue.length || !value.length) { | ||
//for numbers check if beforeDecimal got deleted and there is nothing after decimal, | ||
//clear all numbers in such case while keeping the - sign | ||
if (!format) { | ||
var numericString = this.removeFormatting(value); | ||
var _splitDecimal2 = this.splitDecimal(numericString), | ||
beforeDecimal = _splitDecimal2.beforeDecimal, | ||
afterDecimal = _splitDecimal2.afterDecimal, | ||
addNegation = _splitDecimal2.addNegation; // eslint-disable-line prefer-const | ||
//clear only if something got deleted | ||
if (numericString.length < lastNumStr.length && beforeDecimal === '' && !parseFloat(afterDecimal)) { | ||
return addNegation ? '-' : ''; | ||
} | ||
} | ||
return value; | ||
@@ -767,3 +796,3 @@ } | ||
//get the caret position | ||
var caretPos = this.getCaretPosition(inputValue, formattedValue, currentCaretPosition); | ||
var caretPos = this.getCaretPosition(inputValue, formattedValue, Math.min(formattedValue.length, currentCaretPosition)); | ||
@@ -770,0 +799,0 @@ //set caret position |
/*! | ||
* react-number-format - 3.0.0-alpha3 | ||
* react-number-format - 3.0.0-beta | ||
* 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 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},g={displayType:"input",decimalSeparator:".",fixedDecimalScale:!1,prefix:"",suffix:"",allowNegative:!0,isNumericString:!1,type:"text",onValueChange:h.noop,onChange:h.noop,onKeyDown:h.noop,onMouseUp:h.noop,onFocus:h.noop,onBlur:h.noop,isAllowed:h.returnTrue},m=function(e){function t(e){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.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).formattedValue: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.allowNegative,a=t.prefix,i=t.suffix,u=this.getSeparators(),s=u.thousandSeparator,l=u.decimalSeparator,f="-"===e[0],c=f&&o;e=e.replace("-","");var p=e.indexOf(".")!==-1||r&&n,d=e.split("."),g=d[0],m=d[1]||"";return""!==g||parseFloat(m)?(void 0!==r&&(m=(0,h.limitToScale)(m,r,n)),s&&(g=g.replace(/(\d)(?=(\d{3})+(?!\d))/g,"$1"+s)),a&&(g=a+g),i&&(m+=i),c&&(g="-"+g),e=g+(p&&l||"")+m):c?"-":""}},{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=""),{value:e,formattedValue: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.formattedValue}},{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){if(r.length>=t.length||!r.length)return r;var n=e,o=(0,h.splitString)(t,e),a=(0,h.splitString)(r,e),i=o[1].lastIndexOf(a[1]),u=i!==-1?o[1].substring(0,i):"",s=n+u.length;return this.checkIfFormatGotDeleted(n,s,t)&&(r=t),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=s.formattedValue,f=void 0===l?"":l,c=s.value,p={formattedValue:f,value:c,floatValue:parseFloat(c)};a(p)||(f=i),t.value=f;var h=this.getCaretPosition(r,f,u);return this.setPatchedCaretPosition(t,h,f),f!==i?this.setState({value:f,numAsString:this.removeFormatting(f)},function(){o.onValueChange(p),o.onChange(e)}):o.onChange(e),c}},{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=u.formattedValue,l=u.value,f={formattedValue:s,value:l,floatValue:parseFloat(l)};s!==i?this.setState({value:s,numAsString:a},function(){r.onValueChange(f),e(t)}):e(t)}}},{key:"onKeyDown",value:function e(t){var r=t.target,n=t.key,o=r.selectionEnd,a=r.value,i=r.selectionStart,u=void 0,s=this.props,l=s.decimalScale,f=s.fixedDecimalScale,c=s.prefix,p=s.suffix,h=s.format,e=s.onKeyDown,d=void 0!==l&&f,g=this.getNumberRegex(!1,d),m=new RegExp("-"),v="string"==typeof h;if("ArrowLeft"===n||"Backspace"===n?u=i-1:"ArrowRight"===n?u=i+1:"Delete"===n&&(u=i),void 0===u||i!==o)return void e(t);var y=u,x=v?h.indexOf("#"):c.length,S=v?h.lastIndexOf("#")+1:a.length-p.length;if("ArrowLeft"===n||"ArrowRight"===n){var b="ArrowLeft"===n?"left":"right";y=this.correctCaretPosition(a,u,b)}else if("Delete"!==n||g.test(a[u])||m.test(a[u])){if("Backspace"===n&&!g.test(a[u])&&!m.test(a[u])){for(;!g.test(a[y-1])&&y>x;)y--;y=this.correctCaretPosition(a,y,"left")}}else for(;!g.test(a[y])&&y<S;)y++;(y!==u||u<x||u>S)&&(t.preventDefault(),this.setPatchedCaretPosition(r,y,a)),t.isUnitTestRun&&this.setPatchedCaretPosition(r,y,a),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=e.target,r=t.selectionStart,n=t.value,o=this.correctCaretPosition(n,r);o!==r&&this.setPatchedCaretPosition(t,o,n),this.props.onFocus(e)}},{key:"render",value:function(){var e=this.props,t=e.type,r=e.displayType,n=e.customInput,o=e.renderText,a=this.state.value,i=(0,h.omit)(this.props,d),s=u({},i,{type:t,value:a,onChange:this.onChange,onKeyDown:this.onKeyDown,onMouseUp:this.onMouseUp,onFocus:this.onFocus,onBlur:this.onBlur});if("text"===r)return o?o(a)||null:p.default.createElement("span",i,a);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),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){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 u(e,t){return[e.substring(0,t),e.substring(t)]}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.split("."),o=parseFloat("0."+(n[1]||"0")).toFixed(t).split("."),a=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},o[0]),i=s(o[1]||"",(n[1]||"").length,r);return a+(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=o,t.escapeRegExp=a,t.fixLeadingZero=i,t.splitString=u,t.limitToScale=s,t.roundToPrecision=l,t.omit=f,t.setCaretPosition=c}])}); | ||
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t(require("react")):"function"==typeof define&&define.amd?define(["react"],t):"object"==typeof exports?exports.NumberFormat=t(require("react")):e.NumberFormat=t(e.React)}(this,function(e){return function(e){function t(n){if(r[n])return r[n].exports;var a=r[n]={exports:{},id:n,loaded:!1};return e[n].call(a.exports,a,a.exports,t),a.loaded=!0,a.exports}var r={};return t.m=e,t.c=r,t.p="",t(0)}([function(e,t,r){e.exports=r(1)},function(e,t,r){"use strict";function n(e){return e&&e.__esModule?e:{default:e}}function a(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function o(e,t){if(!e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return!t||"object"!=typeof t&&"function"!=typeof t?e:t}function i(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Super expression must either be null or a function, not "+typeof t);e.prototype=Object.create(t&&t.prototype,{constructor:{value:e,enumerable:!1,writable:!0,configurable:!0}}),t&&(Object.setPrototypeOf?Object.setPrototypeOf(e,t):e.__proto__=t)}var u=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var r=arguments[t];for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&(e[n]=r[n])}return e},s=function(){function e(e,t){for(var r=0;r<t.length;r++){var n=t[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,n.key,n)}}return function(t,r,n){return r&&e(t.prototype,r),n&&e(t,n),t}}(),l=r(2),f=n(l),c=r(7),p=n(c),h=r(8),d={thousandSeparator:f.default.oneOfType([f.default.string,f.default.oneOf([!0])]),decimalSeparator:f.default.string,decimalScale:f.default.number,fixedDecimalScale:f.default.bool,displayType:f.default.oneOf(["input","text"]),prefix:f.default.string,suffix:f.default.string,format:f.default.oneOfType([f.default.string,f.default.func]),removeFormatting:f.default.func,mask:f.default.oneOfType([f.default.string,f.default.arrayOf(f.default.string)]),value:f.default.oneOfType([f.default.number,f.default.string]),isNumericString:f.default.bool,customInput:f.default.func,allowNegative:f.default.bool,onValueChange:f.default.func,onKeyDown:f.default.func,onMouseUp:f.default.func,onChange:f.default.func,onFocus:f.default.func,onBlur:f.default.func,type:f.default.oneOf(["text","tel"]),isAllowed:f.default.func,renderText:f.default.func},m={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},g=function(e){function t(e){a(this,t);var r=o(this,(t.__proto__||Object.getPrototypeOf(t)).call(this,e));r.validateProps();var n=r.formatValueProp();return r.state={value:n,numAsString:r.removeFormatting(n)},r.onChange=r.onChange.bind(r),r.onKeyDown=r.onKeyDown.bind(r),r.onMouseUp=r.onMouseUp.bind(r),r.onFocus=r.onFocus.bind(r),r.onBlur=r.onBlur.bind(r),r}return i(t,e),s(t,[{key:"componentDidUpdate",value:function(e){this.updateValueIfRequired(e)}},{key:"updateValueIfRequired",value:function(e){var t=this.props,r=this.state;if(e!==t){this.validateProps();var n=r.value,a=r.numAsString||"",o=void 0===t.value?this.formatNumString(a).formattedValue:this.formatValueProp();o!==n&&this.setState({value:o,numAsString:this.removeFormatting(o)})}}},{key:"getFloatString",value:function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"",t=this.getSeparators(),r=t.decimalSeparator,n=this.getNumberRegex(!0),a="-"===e[0];a&&(e=e.replace("-","")),e=(e.match(n)||[]).join("").replace(r,".");var o=e.indexOf(".");return o!==-1&&(e=e.substring(0,o)+"."+e.substring(o+1,e.length).replace(new RegExp((0,h.escapeRegExp)(r),"g"),"")),a&&(e="-"+e),e}},{key:"getNumberRegex",value:function(e,t){var r=this.props,n=r.format,a=r.decimalScale,o=this.getSeparators(),i=o.decimalSeparator;return new RegExp("\\d"+(!i||0===a||t||n?"":"|"+(0,h.escapeRegExp)(i)),e?"g":void 0)}},{key:"getSeparators",value:function(){var e=this.props.decimalSeparator,t=this.props.thousandSeparator;return t===!0&&(t=","),{decimalSeparator:e,thousandSeparator:t}}},{key:"getMaskAtIndex",value:function(e){var t=this.props.mask,r=void 0===t?" ":t;return"string"==typeof r?r:r[e]||" "}},{key:"validateProps",value:function(){var e=this.props.mask,t=this.getSeparators(),r=t.decimalSeparator,n=t.thousandSeparator;if(r===n)throw new Error("\n Decimal separator can't be same as thousand separator.\n\n thousandSeparator: "+n+' (thousandSeparator = {true} is same as thousandSeparator = ",")\n decimalSeparator: '+r+" (default value for decimalSeparator is .)\n ");if(e){var a="string"===e?e:e.toString();if(a.match(/\d/g))throw new Error("\n Mask "+e+" should not contain numeric character;\n ")}}},{key:"splitDecimal",value:function(e){var t=this.props.allowNegative,r="-"===e[0],n=r&&t;e=e.replace("-","");var a=e.split("."),o=a[0],i=a[1]||"";return{beforeDecimal:o,afterDecimal:i,hasNagation:r,addNegation:n}}},{key:"setPatchedCaretPosition",value:function(e,t,r){(0,h.setCaretPosition)(e,t),setTimeout(function(){e.value===r&&(0,h.setCaretPosition)(e,t)},0)}},{key:"correctCaretPosition",value:function(e,t,r){var n=this.props,a=n.prefix,o=n.suffix,i=n.format;if(!i){var u="-"===e[0];return Math.min(Math.max(t,a.length+(u?1:0)),e.length-o.length)}if("function"==typeof i)return t;if("#"===i[t]&&(0,h.charIsNumber)(e[t]))return t;if("#"===i[t-1]&&(0,h.charIsNumber)(e[t-1]))return t;var s=i.indexOf("#"),l=i.lastIndexOf("#");t=Math.min(Math.max(t,s),l+1);for(var f=i.substring(t,i.length).indexOf("#"),c=t,p=t+(f===-1?0:f);c>s&&("#"!==i[c]||!(0,h.charIsNumber)(e[c]));)c-=1;var d=!(0,h.charIsNumber)(e[p])||"left"===r&&t!==s||t-c<p-t;return d?c+1:p}},{key:"getCaretPosition",value:function(e,t,r){var n=this.props.format,a=this.state.value,o=this.getNumberRegex(!0),i=(e.match(o)||[]).join(""),u=(t.match(o)||[]).join(""),s=void 0,l=void 0;for(s=0,l=0;l<r;l++){var f=e[l],c=t[s]||"";if((f.match(o)||f===c)&&("0"!==f||!c.match(o)||"0"===c||i.length===u.length)){for(;f!==t[s]&&s<t.length;)s++;s++}}return"string"!=typeof n||a||(s=t.length),s=this.correctCaretPosition(t,s)}},{key:"removePrefixAndSuffix",value:function(e){var t=this.props,r=t.format,n=t.prefix,a=t.suffix;if(!r&&e){var o="-"===e[0];o&&(e=e.substring(1,e.length)),e=n&&0===e.indexOf(n)?e.substring(n.length,e.length):e;var i=e.lastIndexOf(a);e=a&&i!==-1&&i===e.length-a.length?e.substring(0,i):e,o&&(e="-"+e)}return e}},{key:"removePatternFormatting",value:function(e){for(var t=this.props.format,r=t.split("#").filter(function(e){return""!==e}),n=0,a="",o=0,i=r.length;o<=i;o++){var u=r[o]||"",s=o===i?e.length:e.indexOf(u,n);if(s===-1){a=e;break}a+=e.substring(n,s),n=s+u.length}return(a.match(/\d/g)||[]).join("")}},{key:"removeFormatting",value:function e(t){var r=this.props,n=r.format,e=r.removeFormatting;return t?(n?t="string"==typeof n?this.removePatternFormatting(t):"function"==typeof e?e(t):(t.match(/\d/g)||[]).join(""):(t=this.removePrefixAndSuffix(t),t=this.getFloatString(t)),t):t}},{key:"formatWithPattern",value:function(e){for(var t=this.props.format,r=0,n=t.split(""),a=0,o=t.length;a<o;a++)"#"===t[a]&&(n[a]=e[r]||this.getMaskAtIndex(r),r+=1);return n.join("")}},{key:"formatAsNumber",value:function(e){var t=this.props,r=t.decimalScale,n=t.fixedDecimalScale,a=t.prefix,o=t.suffix,i=this.getSeparators(),u=i.thousandSeparator,s=i.decimalSeparator,l=e.indexOf(".")!==-1||r&&n,f=this.splitDecimal(e),c=f.beforeDecimal,p=f.afterDecimal,d=f.addNegation;return void 0!==r&&(p=(0,h.limitToScale)(p,r,n)),u&&(c=c.replace(/(\d)(?=(\d{3})+(?!\d))/g,"$1"+u)),a&&(c=a+c),o&&(p+=o),d&&(c="-"+c),e=c+(l&&s||"")+p}},{key:"formatNumString",value:function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"",t=this.props.format,r=e;return""===e?r="":"-"!==e||t?r="string"==typeof t?this.formatWithPattern(r):"function"==typeof t?t(r):this.formatAsNumber(r):(r="-",e=""),{value:e,formattedValue: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.formattedValue}},{key:"formatNegation",value:function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"",t=this.props.allowNegative,r=new RegExp("(-)"),n=new RegExp("(-)(.)*(-)"),a=r.test(e),o=n.test(e);return e=e.replace(/-/g,""),a&&!o&&t&&(e="-"+e),e}},{key:"formatInput",value:function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"",t=this.props.format;return t||(e=this.formatNegation(e)),e=this.removeFormatting(e),this.formatNumString(e)}},{key:"isCharacterAFormat",value:function(e,t){var r=this.props,n=r.format,a=r.prefix,o=r.suffix,i=r.decimalScale,u=r.fixedDecimalScale,s=this.getSeparators(),l=s.decimalSeparator;return"string"==typeof n&&"#"!==n[e]||!(n||!(e<a.length||e>=t.length-o.length||i&&u&&t[e]===l))}},{key:"checkIfFormatGotDeleted",value:function(e,t,r){for(var n=e;n<t;n++)if(this.isCharacterAFormat(n,r))return!0;return!1}},{key:"correctInputValue",value:function(e,t,r){var n=this.props.format,a=this.state.numAsString||"";if(r.length>=t.length||!r.length)return r;var o=e,i=(0,h.splitString)(t,e),u=(0,h.splitString)(r,e),s=i[1].lastIndexOf(u[1]),l=s!==-1?i[1].substring(0,s):"",f=o+l.length;if(this.checkIfFormatGotDeleted(o,f,t)&&(r=t),!n){var c=this.removeFormatting(r),p=this.splitDecimal(c),d=p.beforeDecimal,m=p.afterDecimal,g=p.addNegation;if(c.length<a.length&&""===d&&!parseFloat(m))return g?"-":""}return r}},{key:"onChange",value:function(e){e.persist();var t=e.target,r=t.value,n=this.state,a=this.props,o=a.isAllowed,i=n.value||"",u=Math.max(t.selectionStart,t.selectionEnd);r=this.correctInputValue(u,i,r);var s=this.formatInput(r),l=s.formattedValue,f=void 0===l?"":l,c=s.value,p={formattedValue:f,value:c,floatValue:parseFloat(c)};o(p)||(f=i),t.value=f;var h=this.getCaretPosition(r,f,Math.min(f.length,u));return this.setPatchedCaretPosition(t,h,f),f!==i?this.setState({value:f,numAsString:this.removeFormatting(f)},function(){a.onValueChange(p),a.onChange(e)}):a.onChange(e),c}},{key:"onBlur",value:function e(t){var r=this.props,n=this.state,a=r.format,e=r.onBlur,o=n.numAsString,i=n.value;if(!a){o=(0,h.fixLeadingZero)(o);var u=this.formatNumString(o),s=u.formattedValue,l=u.value,f={formattedValue:s,value:l,floatValue:parseFloat(l)};s!==i?this.setState({value:s,numAsString:o},function(){r.onValueChange(f),e(t)}):e(t)}}},{key:"onKeyDown",value:function e(t){var r=t.target,n=t.key,a=r.selectionEnd,o=r.value,i=r.selectionStart,u=void 0,s=this.props,l=s.decimalScale,f=s.fixedDecimalScale,c=s.prefix,p=s.suffix,h=s.format,e=s.onKeyDown,d=void 0!==l&&f,m=this.getNumberRegex(!1,d),g=new RegExp("-"),v="string"==typeof h;if("ArrowLeft"===n||"Backspace"===n?u=i-1:"ArrowRight"===n?u=i+1:"Delete"===n&&(u=i),void 0===u||i!==a)return void e(t);var y=u,x=v?h.indexOf("#"):c.length,S=v?h.lastIndexOf("#")+1:o.length-p.length;if("ArrowLeft"===n||"ArrowRight"===n){var b="ArrowLeft"===n?"left":"right";y=this.correctCaretPosition(o,u,b)}else if("Delete"!==n||m.test(o[u])||g.test(o[u])){if("Backspace"===n&&!m.test(o[u])&&!g.test(o[u])){for(;!m.test(o[y-1])&&y>x;)y--;y=this.correctCaretPosition(o,y,"left")}}else for(;!m.test(o[y])&&y<S;)y++;(y!==u||u<x||u>S)&&(t.preventDefault(),this.setPatchedCaretPosition(r,y,o)),t.isUnitTestRun&&this.setPatchedCaretPosition(r,y,o),this.props.onKeyDown(t)}},{key:"onMouseUp",value:function(e){var t=e.target,r=t.selectionStart,n=t.selectionEnd,a=t.value;if(r===n){var o=this.correctCaretPosition(a,r);o!==r&&this.setPatchedCaretPosition(t,o,a)}this.props.onMouseUp(e)}},{key:"onFocus",value:function(e){var t=e.target,r=t.selectionStart,n=t.value,a=this.correctCaretPosition(n,r);a!==r&&this.setPatchedCaretPosition(t,a,n),this.props.onFocus(e)}},{key:"render",value:function(){var e=this.props,t=e.type,r=e.displayType,n=e.customInput,a=e.renderText,o=this.state.value,i=(0,h.omit)(this.props,d),s=u({},i,{type:t,value:o,onChange:this.onChange,onKeyDown:this.onKeyDown,onMouseUp:this.onMouseUp,onFocus:this.onFocus,onBlur:this.onBlur});if("text"===r)return a?a(o)||null:p.default.createElement("span",i,o);if(n){var l=n;return p.default.createElement(l,s)}return p.default.createElement("input",s)}}]),t}(p.default.Component);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}])}); |
@@ -222,3 +222,23 @@ 'use strict'; | ||
} | ||
}, { | ||
key: 'splitDecimal', | ||
value: function splitDecimal(numStr) { | ||
var allowNegative = this.props.allowNegative; | ||
var hasNagation = numStr[0] === '-'; | ||
var addNegation = hasNagation && allowNegative; | ||
numStr = numStr.replace('-', ''); | ||
var parts = numStr.split('.'); | ||
var beforeDecimal = parts[0]; | ||
var afterDecimal = parts[1] || ''; | ||
return { | ||
beforeDecimal: beforeDecimal, | ||
afterDecimal: afterDecimal, | ||
hasNagation: hasNagation, | ||
addNegation: addNegation | ||
}; | ||
} | ||
/** Misc methods end **/ | ||
@@ -451,3 +471,2 @@ | ||
fixedDecimalScale = _props5.fixedDecimalScale, | ||
allowNegative = _props5.allowNegative, | ||
prefix = _props5.prefix, | ||
@@ -460,24 +479,12 @@ suffix = _props5.suffix; | ||
// Check if its negative number and remove negation for futher formatting | ||
var hasNagation = numStr[0] === '-'; | ||
var addNegation = hasNagation && allowNegative; | ||
numStr = numStr.replace('-', ''); | ||
var hasDecimalSeparator = numStr.indexOf('.') !== -1 || decimalScale && fixedDecimalScale; | ||
var parts = numStr.split('.'); | ||
var beforeDecimal = parts[0]; | ||
var afterDecimal = parts[1] || ''; | ||
var _splitDecimal = this.splitDecimal(numStr), | ||
beforeDecimal = _splitDecimal.beforeDecimal, | ||
afterDecimal = _splitDecimal.afterDecimal, | ||
addNegation = _splitDecimal.addNegation; // eslint-disable-line prefer-const | ||
//if beforeDecimal is empty and after decimal is 0 clear the input while keeping the negation sign | ||
if (beforeDecimal === '' && !parseFloat(afterDecimal)) { | ||
return addNegation ? '-' : ''; | ||
} | ||
//apply decimal precision if its defined | ||
//remove leading zeros from number before decimal | ||
//beforeDecimal = removeLeadingZero(beforeDecimal); | ||
//apply decimal precision if its defined | ||
if (decimalScale !== undefined) afterDecimal = (0, _utils.limitToScale)(afterDecimal, decimalScale, fixedDecimalScale); | ||
@@ -642,2 +649,6 @@ | ||
value: function correctInputValue(caretPos, lastValue, value) { | ||
var format = this.props.format; | ||
var lastNumStr = this.state.numAsString || ''; | ||
//don't do anyhting if something got added, or if value is empty string (when whole input is cleared) | ||
@@ -660,2 +671,20 @@ if (value.length >= lastValue.length || !value.length) { | ||
//for numbers check if beforeDecimal got deleted and there is nothing after decimal, | ||
//clear all numbers in such case while keeping the - sign | ||
if (!format) { | ||
var numericString = this.removeFormatting(value); | ||
var _splitDecimal2 = this.splitDecimal(numericString), | ||
beforeDecimal = _splitDecimal2.beforeDecimal, | ||
afterDecimal = _splitDecimal2.afterDecimal, | ||
addNegation = _splitDecimal2.addNegation; // eslint-disable-line prefer-const | ||
//clear only if something got deleted | ||
if (numericString.length < lastNumStr.length && beforeDecimal === '' && !parseFloat(afterDecimal)) { | ||
return addNegation ? '-' : ''; | ||
} | ||
} | ||
return value; | ||
@@ -699,3 +728,3 @@ } | ||
//get the caret position | ||
var caretPos = this.getCaretPosition(inputValue, formattedValue, currentCaretPosition); | ||
var caretPos = this.getCaretPosition(inputValue, formattedValue, Math.min(formattedValue.length, currentCaretPosition)); | ||
@@ -702,0 +731,0 @@ //set caret position |
{ | ||
"name": "react-number-format", | ||
"description": "React component to format number in an input or as a text.", | ||
"version": "3.0.0-alpha3", | ||
"version": "3.0.0-beta", | ||
"main": "lib/number_format.js", | ||
@@ -6,0 +6,0 @@ "author": "Sudhanshu Yadav", |
@@ -48,3 +48,3 @@ # react-number-format | ||
formattedValue: '$23,234,235.56', //value after applying formatting | ||
value: '23234235.56', //non formatted value as string, but it maintains the decimalSeparator provided, so if , is decimal separator then value will be 23234235,56 | ||
value: '23234235.56', //non formatted value as numeric string 23234235.56 | ||
floatValue: 23234235.56 //floating point representation. For big numbers it can have exponential syntax | ||
@@ -177,3 +177,2 @@ } | ||
### Live Demo | ||
@@ -185,2 +184,5 @@ [http://codepen.io/s-yadav/pen/bpKNMa](http://codepen.io/s-yadav/pen/bpKNMa) | ||
### Migrate v2 to v3 | ||
[Migrate](/MIGRATE.md) | ||
### Major Updates | ||
@@ -191,3 +193,2 @@ ### v3.0.0-alpha | ||
- decimalScale by default will not add 0s to match provided decimalScale value like decimalPrecision. You have to set fixedDecimalScale to true. | ||
- onChange api been changed. Now it receives [values object](#values-object) as second parameter. | ||
- mask can be now array of string in which case mask at specific index will be mapped with the # of the pattern. | ||
@@ -194,0 +195,0 @@ - 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. |
@@ -207,2 +207,20 @@ //@flow | ||
splitDecimal(numStr: string) { | ||
const {allowNegative} = this.props; | ||
const hasNagation = numStr[0] === '-'; | ||
const addNegation = hasNagation && allowNegative; | ||
numStr = numStr.replace('-', ''); | ||
const parts = numStr.split('.'); | ||
const beforeDecimal = parts[0]; | ||
const afterDecimal = parts[1] || ''; | ||
return { | ||
beforeDecimal, | ||
afterDecimal, | ||
hasNagation, | ||
addNegation | ||
} | ||
} | ||
/** Misc methods end **/ | ||
@@ -400,24 +418,8 @@ | ||
formatAsNumber(numStr: string) { | ||
const {decimalScale, fixedDecimalScale, allowNegative, prefix, suffix} = this.props; | ||
const {decimalScale, fixedDecimalScale, prefix, suffix} = this.props; | ||
const {thousandSeparator, decimalSeparator} = this.getSeparators(); | ||
// Check if its negative number and remove negation for futher formatting | ||
const hasNagation = numStr[0] === '-'; | ||
const addNegation = hasNagation && allowNegative; | ||
numStr = numStr.replace('-', ''); | ||
const hasDecimalSeparator = numStr.indexOf('.') !== -1 || (decimalScale && fixedDecimalScale); | ||
let {beforeDecimal, afterDecimal, addNegation} = this.splitDecimal(numStr); // eslint-disable-line prefer-const | ||
const parts = numStr.split('.'); | ||
let beforeDecimal = parts[0]; | ||
let afterDecimal = parts[1] || ''; | ||
//if beforeDecimal is empty and after decimal is 0 clear the input while keeping the negation sign | ||
if (beforeDecimal === '' && !parseFloat(afterDecimal)) { | ||
return addNegation ? '-' : ''; | ||
} | ||
//remove leading zeros from number before decimal | ||
//beforeDecimal = removeLeadingZero(beforeDecimal); | ||
//apply decimal precision if its defined | ||
@@ -555,2 +557,5 @@ if (decimalScale !== undefined) afterDecimal = limitToScale(afterDecimal, decimalScale, fixedDecimalScale); | ||
correctInputValue(caretPos: number, lastValue: string, value: string) { | ||
const {format} = this.props; | ||
const lastNumStr = this.state.numAsString || ''; | ||
//don't do anyhting if something got added, or if value is empty string (when whole input is cleared) | ||
@@ -573,2 +578,14 @@ if (value.length >= lastValue.length || !value.length) { | ||
//for numbers check if beforeDecimal got deleted and there is nothing after decimal, | ||
//clear all numbers in such case while keeping the - sign | ||
if (!format) { | ||
const numericString = this.removeFormatting(value); | ||
let {beforeDecimal, afterDecimal, addNegation} = this.splitDecimal(numericString); // eslint-disable-line prefer-const | ||
//clear only if something got deleted | ||
if (numericString.length < lastNumStr.length && beforeDecimal === '' && !parseFloat(afterDecimal)) { | ||
return addNegation ? '-' : ''; | ||
} | ||
} | ||
return value; | ||
@@ -606,3 +623,3 @@ } | ||
//get the caret position | ||
const caretPos = this.getCaretPosition(inputValue, formattedValue, currentCaretPosition); | ||
const caretPos = this.getCaretPosition(inputValue, formattedValue, Math.min(formattedValue.length, currentCaretPosition)); | ||
@@ -609,0 +626,0 @@ //set caret position |
@@ -387,2 +387,9 @@ import React from 'react'; | ||
it('should allow typing decimalSeparator if input is empty', () => { | ||
const wrapper = shallow(<NumberFormat thousandSeparator={','} decimalSeparator={'.'} prefix={'$'}/> ); | ||
simulateKeyInput(wrapper.find('input'), '.', 0); | ||
expect(wrapper.state().value).toEqual('$.'); | ||
}) | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
357006
32
4061
219