-
Notifications
You must be signed in to change notification settings - Fork 162
/
Copy pathreact-currency-input.min.js
2 lines (2 loc) · 5.77 KB
/
react-currency-input.min.js
1
2
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t(require("prop-types"),require("react"),require("react-dom")):"function"==typeof define&&define.amd?define(["prop-types","react","react-dom"],t):e["react-currency-input"]=t(e.PropTypes,e.React,e.ReactDOM)}(this,function(e,t,g){"use strict";e=e&&e.hasOwnProperty("default")?e.default:e;var n="default"in t?t.default:t;function o(e,t,n,i,a,o,s){if(void 0===t&&(t=2),void 0===n&&(n="."),void 0===i&&(i=","),void 0===a&&(a=!1),void 0===o&&(o=""),void 0===s&&(s=""),t<0&&(t=0),20<t&&(t=20),null==e)return{value:0,maskedValue:""};if(0==(e=String(e)).length)return{value:0,maskedValue:""};var r=e.match(/\d/g)||["0"],p=!1;if(a){p=(e.match(/-/g)||[]).length%2==1;for(var l=!0,u=0;u<r.length;u+=1)if("0"!==r[u]){l=!1;break}l&&(p=!1)}for(;r.length<=t;)r.unshift("0");0<t&&r.splice(r.length-t,0,"."),r=Number(r.join("")).toFixed(t).split("");var h=Number(r.join("")),c=r.length-t-1;0<t?r[c]=n:c=r.length;for(var d=c-3;0<d;d-=3)r.splice(d,0,i);return 0<o.length&&r.unshift(o),0<s.length&&r.push(s),a&&p&&(r.unshift("-"),h=-h),{value:h,maskedValue:r.join("").trim()}}g=g&&g.hasOwnProperty("default")?g.default:g,Object.assign=Object.assign||function(e){for(var t=arguments,n=1;n<arguments.length;n++){var i=t[n];for(var a in i)Object.prototype.hasOwnProperty.call(i,a)&&(e[a]=i[a])}return e},Number.parseFloat=parseFloat;var i=function(t){function e(e){t.call(this,e),this.prepareProps=this.prepareProps.bind(this),this.handleChange=this.handleChange.bind(this),this.handleFocus=this.handleFocus.bind(this),this.state=this.prepareProps(this.props),this.inputSelectionStart=1,this.inputSelectionEnd=1}return t&&(e.__proto__=t),((e.prototype=Object.create(t&&t.prototype)).constructor=e).prototype.getMaskedValue=function(){return this.state.maskedValue},e.prototype.prepareProps=function(e){var t=Object.assign({},e);delete t.onChange,delete t.onChangeEvent,delete t.value,delete t.decimalSeparator,delete t.thousandSeparator,delete t.precision,delete t.inputType,delete t.allowNegative,delete t.allowEmpty,delete t.prefix,delete t.suffix,delete t.selectAllOnFocus,delete t.autoFocus,delete t.maxValue,delete t.minValue;var n=e.value;null===n?n=e.allowEmpty?null:"":("string"==typeof n&&("."===e.thousandSeparator&&(n=n.replace(/\./g,"")),"."!=e.decimalSeparator&&(n=n.replace(new RegExp(e.decimalSeparator,"g"),".")),n=n.replace(/[^0-9-.]/g,""),n=Number.parseFloat(n)),n=Number(n).toLocaleString(void 0,{style:"decimal",minimumFractionDigits:e.precision,maximumFractionDigits:e.precision}));var i=o(n,e.precision,e.decimalSeparator,e.thousandSeparator,e.allowNegative,e.prefix,e.suffix);return{maskedValue:i.maskedValue,value:i.value,customProps:t}},e.prototype.componentWillReceiveProps=function(e){this.setState(this.prepareProps(e))},e.prototype.componentDidMount=function(){var e,t,n=g.findDOMNode(this.theInput);this.props.autoFocus?(this.theInput.focus(),e=t=this.state.maskedValue.length-this.props.suffix.length):(t=Math.min(n.selectionEnd,this.theInput.value.length-this.props.suffix.length),e=Math.min(n.selectionStart,t)),n.setSelectionRange(e,t)},e.prototype.componentWillUpdate=function(){var e=g.findDOMNode(this.theInput);this.inputSelectionStart=e.selectionStart,this.inputSelectionEnd=e.selectionEnd},e.prototype.componentDidUpdate=function(e,t){var n=this.props.decimalSeparator,i=g.findDOMNode(this.theInput),a=(this.theInput.value.match(/-/g)||[]).length%2==1,o=this.props.prefix.length+(a?1:0),s=Math.max(o,Math.min(this.inputSelectionEnd,this.theInput.value.length-this.props.suffix.length)),r=Math.max(o,Math.min(this.inputSelectionEnd,s)),p=/[-[\]{}()*+?.,\\^$|#\s]/g,l=new RegExp(n.replace(p,"\\$&")+"|"+this.props.thousandSeparator.replace(p,"\\$&"),"g"),u=(this.state.maskedValue.match(l)||[]).length,h=(t.maskedValue.match(l)||[]).length,c=Math.max(u-h,0);s+=c,r+=c;var d=Number(this.props.precision),f=this.props.suffix.length+this.props.prefix.length+(0<d?n.length:0)+d+1;this.state.maskedValue.length==f&&(r=s=this.theInput.value.length-this.props.suffix.length),i.setSelectionRange(r,s),this.inputSelectionStart=r,this.inputSelectionEnd=s},e.prototype.handleChange=function(e){var t=this;e.preventDefault();var n=o(e.target.value,this.props.precision,this.props.decimalSeparator,this.props.thousandSeparator,this.props.allowNegative,this.props.prefix,this.props.suffix),i=n.maskedValue,a=n.value;this.props.maxValue&&a>this.props.maxValue||this.props.minValue&&a<this.props.maxValue||(e.persist(),this.setState({maskedValue:i,value:a},function(){t.props.onChange(i,a,e),t.props.onChangeEvent(e,i,a)}))},e.prototype.handleFocus=function(e){if(this.theInput){var t=this.theInput.value.length-this.props.suffix.length,n=(this.theInput.value.match(/-/g)||[]).length%2==1,i=this.props.prefix.length+(n?1:0);this.props.selectAllOnFocus&&e.target.setSelectionRange(i,t),this.inputSelectionStart=i,this.inputSelectionEnd=t}},e.prototype.handleBlur=function(e){this.inputSelectionStart=0,this.inputSelectionEnd=0},e.prototype.render=function(){var t=this;return n.createElement("input",Object.assign({},{ref:function(e){t.theInput=e},type:this.props.inputType,value:this.state.maskedValue,onChange:this.handleChange,onFocus:this.handleFocus,onMouseUp:this.handleFocus},this.state.customProps))},e}(t.Component);return i.propTypes={onChange:e.func,value:e.oneOfType([e.number,e.string]),decimalSeparator:e.string,thousandSeparator:e.string,precision:e.oneOfType([e.number,e.string]),inputType:e.string,allowNegative:e.bool,allowEmpty:e.bool,prefix:e.string,suffix:e.string,selectAllOnFocus:e.bool},i.defaultProps={onChange:function(e,t,n){},onChangeEvent:function(e,t,n){},autoFocus:!1,value:"0",decimalSeparator:".",thousandSeparator:",",precision:"2",inputType:"text",allowNegative:!1,prefix:"",suffix:"",selectAllOnFocus:!1,maxValue:void 0,minValue:void 0},i});
//# sourceMappingURL=react-currency-input.min.js.map