react-currency-input
Version:
React component for inputing currency amounts
3 lines (2 loc) • 5.78 kB
JavaScript
!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,n){"use strict";e=e&&e.hasOwnProperty("default")?e.default:e;var i="default"in t?t.default:t;function a(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),t>20&&(t=20),null===e||void 0===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");t>0&&r.splice(r.length-t,0,"."),r=Number(r.join("")).toFixed(t).split("");var h=Number(r.join("")),c=r.length-t-1;t>0?r[c]=n:c=r.length;for(var d=c-3;d>0;d-=3)r.splice(d,0,i);return o.length>0&&r.unshift(o),s.length>0&&r.push(s),a&&p&&(r.unshift("-"),h=-h),{value:h,maskedValue:r.join("").trim()}}n=n&&n.hasOwnProperty("default")?n.default:n,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 o=function(e){function t(t){e.call(this,t),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 e&&(t.__proto__=e),t.prototype=Object.create(e&&e.prototype),t.prototype.constructor=t,t.prototype.getMaskedValue=function(){return this.state.maskedValue},t.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;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=a(n,e.precision,e.decimalSeparator,e.thousandSeparator,e.allowNegative,e.prefix,e.suffix);return{maskedValue:i.maskedValue,value:i.value,customProps:t}},t.prototype.componentWillReceiveProps=function(e){this.setState(this.prepareProps(e))},t.prototype.componentDidMount=function(){var e,t,i=n.findDOMNode(this.theInput);this.props.autoFocus?(this.theInput.focus(),e=t=this.state.maskedValue.length-this.props.suffix.length):(t=Math.min(i.selectionEnd,this.theInput.value.length-this.props.suffix.length),e=Math.min(i.selectionStart,t)),i.setSelectionRange(e,t)},t.prototype.componentWillUpdate=function(){var e=n.findDOMNode(this.theInput);this.inputSelectionStart=e.selectionStart,this.inputSelectionEnd=e.selectionEnd},t.prototype.componentDidUpdate=function(e,t){var i=this.props.decimalSeparator,a=n.findDOMNode(this.theInput),o=(this.theInput.value.match(/-/g)||[]).length%2==1,s=this.props.prefix.length+(o?1:0),r=Math.max(s,Math.min(this.inputSelectionEnd,this.theInput.value.length-this.props.suffix.length)),p=Math.max(s,Math.min(this.inputSelectionEnd,r)),l=/[-[\]{}()*+?.,\\^$|#\s]/g,u=new RegExp(i.replace(l,"\\$&")+"|"+this.props.thousandSeparator.replace(l,"\\$&"),"g"),h=(this.state.maskedValue.match(u)||[]).length,c=(t.maskedValue.match(u)||[]).length,d=Math.max(h-c,0);r+=d,p+=d;var f=Number(this.props.precision),g=this.props.suffix.length+this.props.prefix.length+(f>0?i.length:0)+f+1;this.state.maskedValue.length==g&&(p=r=this.theInput.value.length-this.props.suffix.length),a.setSelectionRange(p,r),this.inputSelectionStart=p,this.inputSelectionEnd=r},t.prototype.handleChange=function(e){var t=this;e.preventDefault();var n=a(e.target.value,this.props.precision,this.props.decimalSeparator,this.props.thousandSeparator,this.props.allowNegative,this.props.prefix,this.props.suffix),i=n.maskedValue,o=n.value;e.persist(),this.setState({maskedValue:i,value:o},function(){t.props.onChange(i,o,e),t.props.onChangeEvent(e,i,o)})},t.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}},t.prototype.handleBlur=function(e){this.inputSelectionStart=0,this.inputSelectionEnd=0},t.prototype.render=function(){var e=this;return i.createElement("input",Object.assign({},{ref:function(t){e.theInput=t},type:this.props.inputType,value:this.state.maskedValue,onChange:this.handleChange,onFocus:this.handleFocus,onMouseUp:this.handleFocus},this.state.customProps))},t}(t.Component);return o.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},o.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},o});
//# sourceMappingURL=react-currency-input.min.js.map