UNPKG

@lanaco/lnc-react-ui

Version:

React component library

37 lines (32 loc) 6.34 kB
"use strict";const F=require("react/jsx-runtime"),u=require("react"),U=require("./index-Cp66dWMi.cjs"),G=require("./emotion-styled.browser.esm-DwHBgTPn.cjs"),f=require("./utils-BK6B_Y9X.cjs"),j=require("./emotion-element-f0de968e.browser.esm-B7k6i6lC.cjs"),Y=require("./objectWithoutPropertiesLoose-BLga_MQA.cjs"),X=require("./use-isomorphic-layout-effect.browser.esm-CkGv31zs.cjs");function Z(e){const t=Object.create(null,{[Symbol.toStringTag]:{value:"Module"}});if(e){for(const r in e)if(r!=="default"){const n=Object.getOwnPropertyDescriptor(e,r);Object.defineProperty(t,r,n.get?n:{enumerable:!0,get:()=>e[r]})}}return t.default=e,Object.freeze(t)}const S=Z(u),J=G.styled.div` & textarea { ${e=>f.getComponentTypographyCss(e.theme,"Input",e.size,"enabled")} min-height: ${e=>f.getSizeValueWithUnits(e.theme,e.size)}; background-color: ${e=>f.getColorRgbaValue(e.theme,"Input",e.color,"enabled","background")}; border: 1px solid ${e=>f.getColorRgbaValue(e.theme,"Input",e.focused===!0?"primary":e.color,e.disabled===!0?"disabled":"enabled","border")}; border-radius: ${e=>f.getBorderRadiusValueWithUnits(e.theme,"regular")}; color: ${e=>f.getColorRgbaValue(e.theme,"Input",e.focused===!0?"primary":e.color,"enabled","text")}; caret-color: ${e=>f.getColorRgbaValue(e.theme,"Input",e.color,"enabled","caret")}; overflow: ${e=>e.collapseOnBlur&&e.focused===!1?"hidden":"auto"}; transition: height 0.2s ease-out; padding: 0.563rem 0.75rem; min-width: 100%; max-width: 100%; } & textarea::placeholder { color: ${e=>f.getColorRgbaValue(e.theme,"Input",e.color,"enabled","placeholder")}; } & textarea:read-only { cursor: default; } & textarea:focus { ${e=>e.readOnly!==!0?f.getOutlineCss(e.theme):"outline: none"} } & textarea:disabled { /* ${e=>f.getDisabledStateCss(e.theme)} */ ${e=>`background-color: ${f.getColorRgbaValue(e.theme,"Input",e.color,"disabled","background","backgroundOpacity")}; color: ${t=>f.getColorRgbaValue(t.theme,"Input",t.color,"disabled","text")};`} } `;var K=function(t){var r=u.useRef(t);return X.index(function(){r.current=t}),r},W=function(t,r){if(typeof t=="function"){t(r);return}t.current=r},Q=function(t,r){var n=u.useRef();return u.useCallback(function(o){t.current=o,n.current&&W(n.current,null),n.current=r,r&&W(r,o)},[r])},k={"min-height":"0","max-height":"none",height:"0",visibility:"hidden",overflow:"hidden",position:"absolute","z-index":"-1000",top:"0",right:"0",display:"block"},ee=function(t){Object.keys(k).forEach(function(r){t.style.setProperty(r,k[r],"important")})},O=ee,i=null,V=function(t,r){var n=t.scrollHeight;return r.sizingStyle.boxSizing==="border-box"?n+r.borderSize:n-r.paddingSize};function te(e,t,r,n){r===void 0&&(r=1),n===void 0&&(n=1/0),i||(i=document.createElement("textarea"),i.setAttribute("tabindex","-1"),i.setAttribute("aria-hidden","true"),O(i)),i.parentNode===null&&document.body.appendChild(i);var o=e.paddingSize,v=e.borderSize,d=e.sizingStyle,s=d.boxSizing;Object.keys(d).forEach(function(g){var y=g;i.style[y]=d[y]}),O(i),i.value=t;var a=V(i,e);i.value=t,a=V(i,e),i.value="x";var h=i.scrollHeight-o,l=h*r;s==="border-box"&&(l=l+o+v),a=Math.max(l,a);var m=h*n;return s==="border-box"&&(m=m+o+v),a=Math.min(m,a),[a,h]}var _=function(){},re=function(t,r){return t.reduce(function(n,o){return n[o]=r[o],n},{})},ne=["borderBottomWidth","borderLeftWidth","borderRightWidth","borderTopWidth","boxSizing","fontFamily","fontSize","fontStyle","fontWeight","letterSpacing","lineHeight","paddingBottom","paddingLeft","paddingRight","paddingTop","tabSize","textIndent","textRendering","textTransform","width","wordBreak","wordSpacing","scrollbarGutter"],oe=!!document.documentElement.currentStyle,ae=function(t){var r=window.getComputedStyle(t);if(r===null)return null;var n=re(ne,r),o=n.boxSizing;if(o==="")return null;oe&&o==="border-box"&&(n.width=parseFloat(n.width)+parseFloat(n.borderRightWidth)+parseFloat(n.borderLeftWidth)+parseFloat(n.paddingRight)+parseFloat(n.paddingLeft)+"px");var v=parseFloat(n.paddingBottom)+parseFloat(n.paddingTop),d=parseFloat(n.borderBottomWidth)+parseFloat(n.borderTopWidth);return{sizingStyle:n,paddingSize:v,borderSize:d}},ie=ae;function $(e,t,r){var n=K(r);S.useLayoutEffect(function(){var o=function(d){return n.current(d)};if(e)return e.addEventListener(t,o),function(){return e.removeEventListener(t,o)}},[])}var ue=function(t,r){$(document.body,"reset",function(n){t.current.form===n.target&&r(n)})},de=function(t){$(window,"resize",t)},se=function(t){$(document.fonts,"loadingdone",t)},le=["cacheMeasurements","maxRows","minRows","onChange","onHeightChange"],ce=function(t,r){var n=t.cacheMeasurements,o=t.maxRows,v=t.minRows,d=t.onChange,s=d===void 0?_:d,a=t.onHeightChange,h=a===void 0?_:a,l=Y._objectWithoutPropertiesLoose(t,le),m=l.value!==void 0,g=S.useRef(null),y=Q(g,r),E=S.useRef(0),w=S.useRef(),p=function(){var c=g.current,C=n&&w.current?w.current:ie(c);if(C){w.current=C;var R=te(C,c.value||c.placeholder||"x",v,o),x=R[0],T=R[1];E.current!==x&&(E.current=x,c.style.setProperty("height",x+"px","important"),h(x,{rowHeight:T}))}},H=function(c){m||p(),s(c)};return S.useLayoutEffect(p),ue(g,function(){if(!m){var z=g.current.value;requestAnimationFrame(function(){var c=g.current;c&&z!==c.value&&p()})}}),de(p),se(p),S.createElement("textarea",j._extends({},l,{onChange:H,ref:y}))},fe=S.forwardRef(ce);const he=u.forwardRef((e,t)=>{const{disabled:r,readOnly:n,value:o,defaultValue:v,debounceTime:d=0,collapseOnBlur:s=!1,minRows:a=0,maxRows:h,onChange:l,onBlur:m,onFocus:g,className:y="",style:E={},size:w="small",color:p="primary",...H}=e,[z,c]=u.useState(o||v||""),C=j.useTheme(),[R,x]=u.useState(!1),[T,M]=u.useState(a),[q,L]=u.useState(s?a:h);u.useEffect(()=>{o!=null&&c(o)},[o]),u.useEffect(()=>{M(a),s&&R===!1&&L(a)},[a,h]),u.useEffect(()=>{L(s?R===!0?h:a:h)},[s]);const B=u.useCallback(U.debounce((b,I)=>A(b,I),d),[l]),A=(b,I)=>{l&&(l==null||l(b,I))},N=b=>{c(b.target.value),B(b,b.target.value)},P=b=>{x(!0),s&&L(h),g==null||g(b)},D=b=>{x(!1),s&&L(a),m==null||m(b)};return F.jsx(J,{style:E,className:y,theme:C,color:p,size:w,focused:R,disabled:r,readOnly:n,collapseOnBlur:s,children:F.jsx(fe,{ref:t,disabled:r,value:z,readOnly:n,minRows:T,maxRows:q,onChange:N,onFocus:P,onBlur:D,...H})})});module.exports=he;