UNPKG

@lanaco/lnc-react-ui

Version:

React component library

53 lines (45 loc) 5.27 kB
"use strict";const m=require("react/jsx-runtime"),b=require("react"),I=require("./emotion-styled.browser.esm-DwHBgTPn.cjs"),r=require("./utils-BK6B_Y9X.cjs"),J=require("./emotion-element-f0de968e.browser.esm-B7k6i6lC.cjs"),L=I.styled.div` display: flex; align-items: center; min-height: ${e=>r.getSizeValueWithUnits(e.theme,e.size)}; max-height: ${e=>r.getSizeValueWithUnits(e.theme,e.size)}; background-color: ${e=>r.getColorRgbaValue(e.theme,"Input",e.color,"enabled","background")}; border: 1px solid ${e=>r.getColorRgbaValue(e.theme,"Input",e.focused===!0?"primary":e.color,"enabled","border")}; border-radius: ${e=>r.getBorderRadiusValueWithUnits(e.theme,"regular")}; ${e=>e.focused===!0&&e.readOnly!==!0?r.getOutlineCss(e.theme):""} /* ${e=>e.isDisabled===!0?r.getDisabledStateCss(e.theme):""} */ ${e=>e.isDisabled===!0&&`background-color: ${r.getColorRgbaValue(e.theme,"Input",e.color,"disabled","background","backgroundOpacity")}; color: ${u=>r.getColorRgbaValue(u.theme,"Input",u.color,"disabled","text")};`} ${e=>e.isDisabled===!0?"border: 1px solid "+r.getColorRgbaValue(e.theme,"Input",e.color,"disabled","border"):""} & input { background: none; border: none; outline: none; padding-top: 0.625rem; padding-bottom: 0.625rem; width: 100%; ${e=>r.getComponentTypographyCss(e.theme,"Input",e.size,"enabled")} min-height: ${e=>r.getSizeValueWithUnits(e.theme,e.size)}; max-height: ${e=>r.getSizeValueWithUnits(e.theme,e.size)}; color: ${e=>r.getColorRgbaValue(e.theme,"Input",e.focused===!0?"primary":e.color,"enabled","text")}; caret-color: ${e=>r.getColorRgbaValue(e.theme,"Input",e.color,"enabled","caret")}; padding-right: ${e=>e.suffix?"0":"0.75rem"}; padding-left: ${e=>e.prefix?"0":"0.75rem"}; } & input:disabled { /* ${e=>r.getDisabledStateCss(e.theme)} */ ${e=>`background-color: ${r.getColorRgbaValue(e.theme,"Input",e.color,"disabled","background","backgroundOpacity")}; color: ${u=>r.getColorRgbaValue(u.theme,"Input",u.color,"disabled","text")};`} background: none; } & input:read-only { cursor: default; } & input::placeholder { color: ${e=>r.getColorRgbaValue(e.theme,"Input",e.color,"enabled","placeholder")}; } `,U=I.styled.span` padding: 0 12px; color: ${e=>r.getColorRgbaValue(e.theme,"Input",e.focused===!0?"primary":e.color,"enabled","prefix")}; `,M=I.styled(U)``,Q=b.forwardRef((e,u)=>{const{disabled:j,readOnly:p,prefix:f,suffix:x,decimalSeparator:a=",",decimalScale:c=2,allowNegative:_=!0,onKeyDown:B=()=>{},onBlur:$=()=>{},onFocus:y=()=>{},onChange:z=()=>{},value:V,className:F="",style:K={},size:T="small",color:C="primary",onInputChange:N=()=>{},allowNull:R=!0,...E}=e,k=J.useTheme(),[w,D]=b.useState(!1),W=l=>{var t,s;if(isNaN(+l)){if(R===!0&&(l==null||l===""))return""}else{const o=(t=l==null?void 0:l.toString())==null?void 0:t.split(".");if(!(o!=null&&o.length)>0)return"0"+a+new Array(c+1).join("0");if((o==null?void 0:o.length)===1)return o.at(0)+a+new Array(c+1).join("0");if((o==null?void 0:o.length)>1){let n=(s=o==null?void 0:o.at(1))==null?void 0:s.toString();if((n==null?void 0:n.length)===c)return o.at(0)+a+n;if((n==null?void 0:n.length)<c)return o.at(0)+a+o.at(1)+new Array(c-n.length+1).join("0");if((n==null?void 0:n.length)>c)return o.at(0)+a+n.slice(0,c)}}return"0"+a+new Array(c+1).join("0")},q=W(V),[S,h]=b.useState(q),i=b.useRef(q);b.useEffect(()=>{const l=W(V);i.current=l,h(i.current)},[V]);const P=l=>{D(!0),y==null||y(l)},G=l=>{D(!1),$==null||$(l)},H=l=>{var s,o;let t=(s=i.current)==null?void 0:s.replace(a,"");if(l.key==="Delete"||l.key==="Backspace")if(+t!=0){t=t.slice(0,(t==null?void 0:t.length)-1);let n=t.slice(0,(t==null?void 0:t.length)-c),d=t.slice((t==null?void 0:t.length)-c);(n==null?void 0:n.length)===0?n="0":n==="-"&&+t!=0?n="-0":n==="-"&&+t==0&&(n="0");let g=[n,a,d].join("");i.current=g,h(i.current)}else+t==0&&R===!0&&(i==null?void 0:i.current)!==""&&(i.current="",h(i.current),O(l));else if(l.key==="-"&&_===!0&&+t!=0)((o=i==null?void 0:i.current)==null?void 0:o.slice(1))==="-"?i.current=i.current.slice(1,i.current.length):i.current="-"+i.current,h(i.current);else if(!isNaN(l.key)){(t==null?void 0:t.length)===0?t="0"+new Array(c).join("0")+l.key:t=t+l.key;let n=t.slice(0,(t==null?void 0:t.length)-c),d=t.slice((t==null?void 0:t.length)-c);(n==null?void 0:n.length)>1&&(n=n.replace(/^0+/,""),(n==null?void 0:n.length)===0&&(n="0"));let g=[n,a,d].join("");i.current=g,h(i.current)}B(l)},O=l=>{var d,g;if((i==null?void 0:i.current)===""){l.target.value="",N(l,null),z(l,null);return}let t=(d=i==null?void 0:i.current)==null?void 0:d.split(a),s=+(t==null?void 0:t.at(0)),o=((g=t==null?void 0:t.at(1))==null?void 0:g.length)>0?`0.${t==null?void 0:t.at(1)}`:0,n=s+ +o;l.target.value=n,N(l,n),z(l,n)};return m.jsxs(L,{style:K,className:F,theme:k,color:C,size:T,prefix:f,suffix:x,focused:w,disabled:j,readOnly:p,children:[f&&m.jsx(U,{theme:k,color:C,focused:w,className:"lnc-input-prefix",children:f}),m.jsx("input",{ref:u,type:"text",...E,onKeyDown:H,onBlur:G,value:S,className:"lnc-ui-decimal-v2-input",onFocus:P,disabled:j||p,onChange:l=>{O(l)}}),x&&m.jsx(M,{theme:k,color:C,focused:w,className:"lnc-input-suffix",children:x})]})});module.exports=Q;