UNPKG

@gsretail.com/gui-core

Version:

A skeleton to create your own React component library using Rollup, TypeScript, Sass and Storybook

2 lines (1 loc) 12.1 kB
import e,{useRef as t,useEffect as r,useState as n,useMemo as a}from"react";function o(e,t){var r={};for(var n in e)Object.prototype.hasOwnProperty.call(e,n)&&t.indexOf(n)<0&&(r[n]=e[n]);if(null!=e&&"function"==typeof Object.getOwnPropertySymbols){var a=0;for(n=Object.getOwnPropertySymbols(e);a<n.length;a++)t.indexOf(n[a])<0&&Object.prototype.propertyIsEnumerable.call(e,n[a])&&(r[n[a]]=e[n[a]])}return r}var u;function i(){}function l(e){return!!(e||"").match(/\d/)}function c(e){return null==e}function s(e){return"number"==typeof e&&isNaN(e)}function f(e){return e.replace(/[-[\]/{}()*+?.\\^$|]/g,"\\$&")}function v(e,t){void 0===t&&(t=!0);var r="-"===e[0],n=r&&t,a=(e=e.replace("-","")).split(".");return{beforeDecimal:a[0],afterDecimal:a[1]||"",hasNegation:r,addNegation:n}}function d(e,t,r){for(var n="",a=r?"0":"",o=0;o<=t-1;o++)n+=e[o]||a;return n}function g(e,t){return Array(t+1).join(e)}function m(e){var t=e+"",r="-"===t[0]?"-":"";r&&(t=t.substring(1));var n=t.split(/[eE]/g),a=n[0],o=n[1];if(!(o=Number(o)))return r+a;var u=1+o,i=(a=a.replace(".","")).length;return u<0?a="0."+g("0",Math.abs(u))+a:u>=i?a+=g("0",u-i):a=(a.substring(0,u)||"0")+"."+a.substring(u),r+a}function p(e,t,r){if(-1!==["","-"].indexOf(e))return e;var n=(-1!==e.indexOf(".")||r)&&t,a=v(e),o=a.beforeDecimal,u=a.afterDecimal,i=a.hasNegation,l=parseFloat("0."+(u||"0")),c=(u.length<=t?"0."+u:l.toFixed(t)).split(".");return""+(i?"-":"")+o.split("").reverse().reduce((function(e,t,r){return e.length>r?(Number(e[0])+Number(t)).toString()+e.substring(1,e.length):t+e}),c[0])+(n?".":"")+d(c[1]||"",t,r)}function h(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)}}function S(e,t,r,n){var a,o,u,i=e.length;if(a=t,o=0,u=i,t=Math.min(Math.max(a,o),u),"left"===n){for(;t>=0&&!r[t];)t--;-1===t&&(t=r.indexOf(!0))}else{for(;t<=i&&!r[t];)t++;t>i&&(t=r.lastIndexOf(!0))}return-1===t&&(t=i),t}function b(e){for(var t=Array.from({length:e.length+1}).map((function(){return!0})),r=0,n=t.length;r<n;r++)t[r]=Boolean(l(e[r])||l(e[r-1]));return t}function y(e,r,o,u,l,f){void 0===f&&(f=i);var v=t(),d=function(e){var r=t(e);r.current=e;var n=t((function(){for(var e=[],t=arguments.length;t--;)e[t]=arguments[t];return r.current.apply(r,e)}));return n.current}((function(e){var t,r;return c(e)||s(e)?(r="",t=""):"number"==typeof e||o?(r="number"==typeof e?m(e):e,t=u(r)):(r=l(e,void 0),t=e),{formattedValue:t,numAsString:r}})),g=n((function(){return d(r)})),p=g[0],h=g[1];return a((function(){c(e)?v.current=void 0:(v.current=d(e),h(v.current))}),[e,d]),[p,function(e,t){h({formattedValue:e.formattedValue,numAsString:e.value}),f(e,t)}]}function x(e){return e.replace(/[^0-9]/g,"")}function w(e){return e}function D(a){var c=a.type;void 0===c&&(c="text");var s=a.displayType;void 0===s&&(s="input");var f=a.customInput,v=a.renderText,d=a.getInputRef,g=a.format;void 0===g&&(g=w);var m=a.removeFormatting;void 0===m&&(m=x);var p=a.defaultValue,D=a.valueIsNumericString,V=a.onValueChange,O=a.isAllowed,N=a.onChange;void 0===N&&(N=i);var T=a.onKeyDown;void 0===T&&(T=i);var B=a.onMouseUp;void 0===B&&(B=i);var C=a.onFocus;void 0===C&&(C=i);var A=a.onBlur;void 0===A&&(A=i);var E=a.value,j=a.getCaretBoundary;void 0===j&&(j=b);var F=o(a,["type","displayType","customInput","renderText","getInputRef","format","removeFormatting","defaultValue","valueIsNumericString","onValueChange","isAllowed","onChange","onKeyDown","onMouseUp","onFocus","onBlur","value","getCaretBoundary"]),I=y(E,p,Boolean(D),g,m,V),R=I[0],M=R.formattedValue,k=R.numAsString,P=I[1],K=t();r((function(){var e=g(k);if(void 0===K.current||e!==K.current){var t=G.current;Q({formattedValue:e,numAsString:k,input:t,setCaretPosition:!0,source:u.props,event:void 0})}}));var W=n(!1),L=W[0],U=W[1],G=t(null),$=t({setCaretTimeout:null,focusTimeout:null});r((function(){return U(!0),function(){clearTimeout($.current.setCaretTimeout),clearTimeout($.current.focusTimeout)}}),[]);var Z=g,q=function(e,t){var r=parseFloat(t);return{formattedValue:e,value:t,floatValue:isNaN(r)?void 0:r}},z=function(e,t,r){h(e,t),$.current.setCaretTimeout=setTimeout((function(){e.value===r&&h(e,t)}),0)},H=function(e,t,r){return S(e,t,j(e),r)},J=function(e,t,r){var n=j(t),a=function(e,t,r,n,a){var o=a.findIndex((function(e){return e})),u=e.slice(0,o);t||r.startsWith(u)||(r=u+r,n+=u.length);for(var i=r.length,c=e.length,s={},f=new Array(i),v=0;v<i;v++){f[v]=-1;for(var d=0,g=c;d<g;d++)if(r[v]===e[d]&&!0!==s[d]){f[v]=d,s[d]=!0;break}}for(var m=n;m<i&&(-1===f[m]||!l(r[m]));)m++;var p=m===i||-1===f[m]?c:f[m];for(m=n-1;m>0&&-1===f[m];)m--;var h=-1===m||-1===f[m]?0:f[m]+1;return h>p?p:n-h<p-n?h:p}(t,M,e,r,n);return a=S(t,a,n)},Q=function(e){var t=e.formattedValue;void 0===t&&(t="");var r=e.input,n=e.setCaretPosition;void 0===n&&(n=!0);var a,o=e.source,u=e.event,i=e.numAsString,l=e.caretPos;if(r){if(void 0===l&&n){var c=e.inputValue||r.value,s=(a=r,Math.max(a.selectionStart,a.selectionEnd));r.value=t,l=J(c,t,s)}r.value=t,n&&void 0!==l&&z(r,l,t)}t!==M&&function(e,t){K.current=e.formattedValue,P(e,t)}(q(t,i),{event:u,source:o})},X=!L||"undefined"==typeof navigator||navigator.platform&&/iPhone|iPod/.test(navigator.platform)?void 0:"numeric",Y=Object.assign({inputMode:X},F,{type:c,value:M,onChange:function(e){var t=function(e,t,r){var n=function(e,t){for(var r=0,n=0,a=e.length,o=t.length;e[r]===t[r]&&r<a;)r++;for(;e[a-1-n]===t[o-1-n]&&o-n>r&&a-n>r;)n++;return{from:{start:r,end:a-n},to:{start:r,end:o-n}}}(M,e),a=Object.assign(Object.assign({},n),{lastValue:M}),o=m(e,a),u=Z(o);return!(O&&!O(q(u,o))||(Q({formattedValue:u,numAsString:o,inputValue:e,event:t,source:r,setCaretPosition:!0,input:t.target}),0))}(e.target.value,e,u.event);t&&N(e)},onKeyDown:function(e){var t,r=e.target,n=e.key,a=r.selectionStart,o=r.selectionEnd,u=r.value;if(void 0===u&&(u=""),"ArrowLeft"===n||"Backspace"===n?t=Math.max(a-1,0):"ArrowRight"===n?t=Math.min(a+1,u.length):"Delete"===n&&(t=a),void 0!==t&&a===o){var i=t;if("ArrowLeft"===n||"ArrowRight"===n)i=H(u,t,"ArrowLeft"===n?"left":"right");else"Delete"!==n||l(u[t])?"Backspace"!==n||l(u[t])||(i=H(u,t,"left")):i=H(u,t,"right");i!==t&&z(r,i,u),e.isUnitTestRun&&z(r,i,u),T(e)}else T(e)},onMouseUp:function(e){var t=e.target,r=t.selectionStart,n=t.selectionEnd,a=t.value;if(void 0===a&&(a=""),r===n){var o=H(a,r);o!==r&&z(t,o,a)}B(e)},onFocus:function(e){e.persist();var t=e.target;G.current=t,$.current.focusTimeout=setTimeout((function(){var r=t.selectionStart,n=t.selectionEnd,a=t.value;void 0===a&&(a="");var o=H(a,r);o===r||0===r&&n===a.length||z(t,o,a),C(e)}),0)},onBlur:function(e){G.current=null,clearTimeout($.current.focusTimeout),clearTimeout($.current.setCaretTimeout),A(e)}});if("text"===s)return v?e.createElement(e.Fragment,null,v(M,F)||null):e.createElement("span",Object.assign({},F,{ref:d}),M);if(f){var _=f;return e.createElement(_,Object.assign({},Y,{ref:d}))}return e.createElement("input",Object.assign({},Y,{ref:d}))}function V(e,t){var r=t.decimalScale,n=t.fixedDecimalScale,a=t.prefix;void 0===a&&(a="");var o=t.suffix;void 0===o&&(o="");var u=t.allowNegative;void 0===u&&(u=!0);var i=t.thousandsGroupStyle;if(void 0===i&&(i="thousand"),""===e||"-"===e)return e;var l=O(t),c=l.thousandSeparator,s=l.decimalSeparator,f=0!==r&&-1!==e.indexOf(".")||r&&n,g=v(e,u),m=g.beforeDecimal,p=g.afterDecimal,h=g.addNegation;return void 0!==r&&(p=d(p,r,!!n)),c&&(m=function(e,t,r){var n=function(e){switch(e){case"lakh":return/(\d+?)(?=(\d\d)+(\d)(?!\d))(\.\d+)?/g;case"wan":return/(\d)(?=(\d{4})+(?!\d))/g;default:return/(\d)(?=(\d{3})+(?!\d))/g}}(r),a=e.search(/[1-9]/);return a=-1===a?e.length:a,e.substring(0,a)+e.substring(a,e.length).replace(n,"$1"+t)}(m,c,i)),a&&(m=a+m),o&&(p+=o),h&&(m="-"+m),e=m+(f&&s||"")+p}function O(e){var t=e.decimalSeparator;void 0===t&&(t=".");var r=e.thousandSeparator,n=e.allowedDecimalSeparators;return!0===r&&(r=","),n||(n=[t,"."]),{decimalSeparator:t,thousandSeparator:r,allowedDecimalSeparators:n}}function N(e,t,r){void 0===t&&(t=function(e){return{from:{start:0,end:0},to:{start:0,end:e.length},lastValue:""}}(e));var n=r.allowNegative;void 0===n&&(n=!0);var a=r.prefix;void 0===a&&(a="");var o=r.suffix;void 0===o&&(o="");var u=r.decimalScale,i=t.from,l=t.to,c=l.start,s=l.end,d=O(r),g=d.allowedDecimalSeparators,m=d.decimalSeparator,p=e[s]===m;if(s-c==1&&-1!==g.indexOf(e[c])){var h=0===u?"":m;e=e.substring(0,c)+h+e.substring(c+1,e.length)}var S=!1;a.startsWith("-")?S=e.startsWith("--"):o.startsWith("-")&&e.length===o.length?S=!1:"-"===e[0]&&(S=!0),S&&(e=e.substring(1),c-=1,s-=1);var b=0;e.startsWith(a)?b+=a.length:c<a.length&&(b=c),s-=b;var y=(e=e.substring(b)).length,x=e.length-o.length;e.endsWith(o)?y=x:s>e.length-o.length&&(y=s),e=e.substring(0,y),e=function(e,t){void 0===e&&(e="");var 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}(S?"-"+e:e,n),e=(e.match(function(e,t){return new RegExp("(^-)|[0-9]|"+f(e),t?"g":void 0)}(m,!0))||[]).join("");var w=e.indexOf(m),D=v(e=e.replace(new RegExp(f(m),"g"),(function(e,t){return t===w?".":""})),n),V=D.beforeDecimal,N=D.afterDecimal,T=D.addNegation;return l.end-l.start<i.end-i.start&&""===V&&p&&!parseFloat(N)&&(e=T?"-":""),e}function T(e,t){var r=t.prefix;void 0===r&&(r="");var n=t.suffix;void 0===n&&(n="");var a=Array.from({length:e.length+1}).map((function(){return!0})),o="-"===e[0];a.fill(!1,0,r.length+(o?1:0));var u=e.length;return a.fill(!1,u-n.length+1,u+1),a}function B(e){e.decimalSeparator,e.allowedDecimalSeparators,e.thousandsGroupStyle,e.suffix,e.allowNegative;var t=e.allowLeadingZeros,r=e.onKeyDown;void 0===r&&(r=i);var n=e.onBlur;void 0===n&&(n=i);var a=e.thousandSeparator,l=e.decimalScale,f=e.fixedDecimalScale,v=e.prefix;void 0===v&&(v="");var d=e.defaultValue,g=e.value,S=e.valueIsNumericString,b=e.onValueChange,x=o(e,["decimalSeparator","allowedDecimalSeparators","thousandsGroupStyle","suffix","allowNegative","allowLeadingZeros","onKeyDown","onBlur","thousandSeparator","decimalScale","fixedDecimalScale","prefix","defaultValue","value","valueIsNumericString","onValueChange"]);!function(e){var t=O(e),r=t.thousandSeparator,n=t.decimalSeparator;if(r===n)throw new Error("\n Decimal separator can't be same as thousand separator.\n thousandSeparator: "+r+' (thousandSeparator = {true} is same as thousandSeparator = ",")\n decimalSeparator: '+n+" (default value for decimalSeparator is .)\n ")}(e);var w=function(t){return V(t,e)},D=function(t,r){return N(t,r,e)},B=S;c(g)?c(d)||(B=null!=S?S:"number"==typeof d):B=null!=S?S:"number"==typeof g;var C=function(e){return c(e)||s(e)?e:("number"==typeof e&&(e=m(e)),B&&"number"==typeof l?p(e,l,Boolean(f)):e)},A=y(C(g),C(d),Boolean(B),w,D,b),E=A[0],j=E.numAsString,F=E.formattedValue,I=A[1];return Object.assign(Object.assign({},x),{value:F,valueIsNumericString:!1,onValueChange:I,format:w,removeFormatting:D,getCaretBoundary:function(t){return T(t,e)},onKeyDown:function(t){var n=t.target,o=t.key,u=n.selectionStart,i=n.selectionEnd,c=n.value;if(void 0===c&&(c=""),u===i){"Backspace"===o&&"-"===c[0]&&u===v.length+1&&h(n,1);var s=O(e).decimalSeparator;"Backspace"===o&&c[u-1]===s&&l&&f&&(h(n,u-1),t.preventDefault());var d=!0===a?",":a;"Backspace"===o&&c[u-1]===d&&h(n,u-1),"Delete"===o&&c[u]===d&&h(n,u+1),r(t)}else r(t)},onBlur:function(r){var a=j;if(a.match(/\d/g)||(a=""),t||(a=function(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:"")}(a)),f&&l&&(a=p(a,l,f)),a!==j){var o=V(a,e);I({formattedValue:o,value:a,floatValue:parseFloat(a)},{event:r,source:u.event})}n(r)}})}function C(t){var r=B(t);return e.createElement(D,Object.assign({},r))}!function(e){e.event="event",e.props="prop"}(u||(u={}));export{D as NumberFormatBase,C as NumericFormat,T as getNumericCaretBoundary,V as numericFormatter,N as removeNumericFormat,B as useNumericFormat};