react-simple-currency
Version:
A react input wrapper to mask and handle currency values
1 lines • 4.55 kB
JavaScript
!function(e){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=e();else if("function"==typeof define&&define.amd)define([],e);else{var t;t="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:this,t.SimpleCurrencyInput=e()}}(function(){return function e(t,n,r){function o(u,a){if(!n[u]){if(!t[u]){var s="function"==typeof require&&require;if(!a&&s)return s(u,!0);if(i)return i(u,!0);var p=new Error("Cannot find module '"+u+"'");throw p.code="MODULE_NOT_FOUND",p}var l=n[u]={exports:{}};t[u][0].call(l.exports,function(e){var n=t[u][1][e];return o(n?n:e)},l,l.exports,e,t,n,r)}return n[u].exports}for(var i="function"==typeof require&&require,u=0;u<r.length;u++)o(r[u]);return o}({1:[function(e,t,n){(function(r){"use strict";function o(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}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)}Object.defineProperty(n,"__esModule",{value:!0});var u=function(){function e(e,t){for(var n=0;n<t.length;n++){var r=t[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}return function(t,n,r){return n&&e(t.prototype,n),r&&e(t,r),t}}(),a=function(e,t,n){for(var r=!0;r;){var o=e,i=t,u=n;r=!1,null===o&&(o=Function.prototype);var a=Object.getOwnPropertyDescriptor(o,i);if(void 0!==a){if("value"in a)return a.value;var s=a.get;if(void 0===s)return;return s.call(u)}var p=Object.getPrototypeOf(o);if(null===p)return;e=p,t=i,n=u,r=!0,a=p=void 0}},s="undefined"!=typeof window?window.React:"undefined"!=typeof r?r.React:null,p=e("prop-types"),l=function(e){function t(e){o(this,t),a(Object.getPrototypeOf(t.prototype),"constructor",this).call(this,e),this.onInputType=this.onInputType.bind(this),this.formattedRawValue=this.formattedRawValue.bind(this),this.getRawValue=this.getRawValue.bind(this),this.state={rawValue:this.props.value,tabIndex:this.props.tabIndex,readOnly:this.props.readOnly}}return i(t,e),u(t,[{key:"componentWillMount",value:function(){this.notifyParentWithRawValue(this.state.rawValue)}},{key:"componentWillReceiveProps",value:function(e){(e.value||0===e.value)&&this.setState({rawValue:e.value})}},{key:"onInputType",value:function(e){var t=e.target.value,n=this.getRawValue(t);n||(n=0),this.notifyParentWithRawValue(n),this.setState({rawValue:n})}},{key:"notifyParentWithRawValue",value:function(e){var t=this.formattedRawValue(e);this.props.onInputChange(e,t)}},{key:"getRawValue",value:function(e){var t=e;t=c(t,this.props.delimiter),t=c(t,this.props.separator),t=c(t,this.props.unit);var n=parseInt(t);return n}},{key:"formattedRawValue",value:function(e){var t="0".length+this.props.precision,n="";if(n=""+e,n.length<t){var r=t-n.length;n=""+f(r)+n}var o=n.slice(0,n.length-this.props.precision),i=n.slice(n.length-this.props.precision);if(o.length>3){for(var u=o.split("").reverse(),a="",s=u.length-1;s>=0;s--){var p=u[s],l=s%3===0?this.props.delimiter:"";a=""+a+p+l}a=a.substring(0,a.length-1),o=a}return n=o+this.props.separator+i,this.props.unit&&(n=this.props.unit+" "+n),n}},{key:"render",value:function(){return s.createElement("input",{id:this.props.id,className:this.props.className,onBlur:this.props.onInputBlur,onFocus:this.props.onInputFocus,onChange:this.onInputType,value:this.formattedRawValue(this.state.rawValue),disabled:this.props.disabled,autoFocus:this.props.autoFocus,tabIndex:this.state.tabIndex,readOnly:this.state.readOnly})}}]),t}(s.Component),f=function(e){var t="",n=0;for(n=0;n<e;n++)t+="0";return t},c=function(e,t){t=t.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g,"\\$&");var n=new RegExp(t,"g");return e.replace(n,"")};l.propTypes={id:p.string,autoFocus:p.bool,delimiter:p.string,disabled:p.bool,onInputChange:p.func,onInputBlur:p.func,onInputFocus:p.func,precision:p.number,readOnly:p.bool,separator:p.string,tabIndex:p.number,unit:p.string,value:p.number.isRequired},l.defaultProps={value:0,precision:2,separator:".",delimiter:",",unit:"",disabled:!1,autoFocus:!1,onInputChange:function(){},onInputBlur:function(){},onInputFocus:function(){}},n["default"]=l,t.exports=n["default"]}).call(this,"undefined"!=typeof global?global:"undefined"!=typeof self?self:"undefined"!=typeof window?window:{})},{"prop-types":void 0}]},{},[1])(1)});