@lanaco/lnc-react-ui
Version:
React component library
34 lines (29 loc) • 6.79 kB
JavaScript
;const O=require("react/jsx-runtime"),a=require("./index-BPVoEhTF.cjs"),d=require("react"),X=require("./index-DaiUAQVF.cjs"),Z=require("./emotion-styled.browser.esm-Cle7tAM9.cjs"),m=require("./utils-NZLCh-J5.cjs"),k=require("./emotion-element-f0de968e.browser.esm-B7k6i6lC.cjs"),J=require("./objectWithoutPropertiesLoose-BLga_MQA.cjs"),K=require("./use-isomorphic-layout-effect.browser.esm-CkGv31zs.cjs");function Q(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=Q(d),ee=Z.newStyled.div`
& textarea {
${e=>m.getComponentTypographyCss(e.theme,"Input",e.size,"enabled")}
min-height: ${e=>m.getSizeValueWithUnits(e.theme,e.size)};
background-color: ${e=>m.getColorRgbaValue(e.theme,"Input",e.color,"enabled","background")};
border: 1px solid
${e=>m.getColorRgbaValue(e.theme,"Input",e.focused===!0?"primary":e.color,e.disabled===!0?"disabled":"enabled","border")};
border-radius: ${e=>m.getBorderRadiusValueWithUnits(e.theme,"regular")};
color: ${e=>m.getColorRgbaValue(e.theme,"Input",e.focused===!0?"primary":e.color,"enabled","text")};
caret-color: ${e=>m.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=>m.getColorRgbaValue(e.theme,"Input",e.color,"enabled","placeholder")};
}
& textarea:read-only {
cursor: default;
}
& textarea:focus {
${e=>e.readOnly!==!0?m.getOutlineCss(e.theme):"outline: none"}
}
& textarea:disabled {
${e=>m.getDisabledStateCss(e.theme)}
}
`;var te=function(t){var r=d.useRef(t);return K.index(function(){r.current=t}),r},$=function(t,r){if(typeof t=="function"){t(r);return}t.current=r},re=function(t,r){var n=d.useRef();return d.useCallback(function(o){t.current=o,n.current&&$(n.current,null),n.current=r,r&&$(r,o)},[r])},W={"min-height":"0","max-height":"none",height:"0",visibility:"hidden",overflow:"hidden",position:"absolute","z-index":"-1000",top:"0",right:"0",display:"block"},ne=function(t){Object.keys(W).forEach(function(r){t.style.setProperty(r,W[r],"important")})},_=ne,u=null,j=function(t,r){var n=t.scrollHeight;return r.sizingStyle.boxSizing==="border-box"?n+r.borderSize:n-r.paddingSize};function oe(e,t,r,n){r===void 0&&(r=1),n===void 0&&(n=1/0),u||(u=document.createElement("textarea"),u.setAttribute("tabindex","-1"),u.setAttribute("aria-hidden","true"),_(u)),u.parentNode===null&&document.body.appendChild(u);var o=e.paddingSize,b=e.borderSize,l=e.sizingStyle,T=l.boxSizing;Object.keys(l).forEach(function(g){var v=g;u.style[v]=l[v]}),_(u),u.value=t;var i=j(u,e);u.value=t,i=j(u,e),u.value="x";var s=u.scrollHeight-o,c=s*r;T==="border-box"&&(c=c+o+b),i=Math.max(c,i);var f=s*n;return T==="border-box"&&(f=f+o+b),i=Math.min(f,i),[i,s]}var V=function(){},ae=function(t,r){return t.reduce(function(n,o){return n[o]=r[o],n},{})},ie=["borderBottomWidth","borderLeftWidth","borderRightWidth","borderTopWidth","boxSizing","fontFamily","fontSize","fontStyle","fontWeight","letterSpacing","lineHeight","paddingBottom","paddingLeft","paddingRight","paddingTop","tabSize","textIndent","textRendering","textTransform","width","wordBreak","wordSpacing","scrollbarGutter"],ue=!!document.documentElement.currentStyle,se=function(t){var r=window.getComputedStyle(t);if(r===null)return null;var n=ae(ie,r),o=n.boxSizing;if(o==="")return null;ue&&o==="border-box"&&(n.width=parseFloat(n.width)+parseFloat(n.borderRightWidth)+parseFloat(n.borderLeftWidth)+parseFloat(n.paddingRight)+parseFloat(n.paddingLeft)+"px");var b=parseFloat(n.paddingBottom)+parseFloat(n.paddingTop),l=parseFloat(n.borderBottomWidth)+parseFloat(n.borderTopWidth);return{sizingStyle:n,paddingSize:b,borderSize:l}},de=se;function I(e,t,r){var n=te(r);S.useLayoutEffect(function(){var o=function(l){return n.current(l)};if(e)return e.addEventListener(t,o),function(){return e.removeEventListener(t,o)}},[])}var le=function(t,r){I(document.body,"reset",function(n){t.current.form===n.target&&r(n)})},ce=function(t){I(window,"resize",t)},fe=function(t){I(document.fonts,"loadingdone",t)},ge=["cacheMeasurements","maxRows","minRows","onChange","onHeightChange"],he=function(t,r){var n=t.cacheMeasurements,o=t.maxRows,b=t.minRows,l=t.onChange,T=l===void 0?V:l,i=t.onHeightChange,s=i===void 0?V:i,c=J._objectWithoutPropertiesLoose(t,ge),f=c.value!==void 0,g=S.useRef(null),v=re(g,r),L=S.useRef(0),w=S.useRef(),x=function(){var h=g.current,R=n&&w.current?w.current:de(h);if(R){w.current=R;var P=oe(R,h.value||h.placeholder||"x",b,o),y=P[0],E=P[1];L.current!==y&&(L.current=y,h.style.setProperty("height",y+"px","important"),s(y,{rowHeight:E}))}},H=function(h){f||x(),T(h)};return S.useLayoutEffect(x),le(g,function(){if(!f){var z=g.current.value;requestAnimationFrame(function(){var h=g.current;h&&z!==h.value&&x()})}}),ce(x),fe(x),S.createElement("textarea",k._extends({},c,{onChange:H,ref:v}))},pe=S.forwardRef(he);const B=d.forwardRef((e,t)=>{const{disabled:r,readOnly:n,value:o,defaultValue:b,debounceTime:l=0,type:T="textarea",collapseOnBlur:i=!1,minRows:s=0,maxRows:c,onChange:f,onBlur:g,onFocus:v,className:L="",style:w={},size:x="small",color:H="primary",...z}=e,[h,R]=d.useState(o||b||""),P=k.useTheme(),[y,E]=d.useState(!1),[q,M]=d.useState(s),[A,C]=d.useState(i?s:c);d.useEffect(()=>{o!=null&&R(o)},[o]),d.useEffect(()=>{M(s),i&&y===!1&&C(s)},[s,c]),d.useEffect(()=>{C(i?y===!0?c:s:c)},[i]);const N=d.useCallback(X.debounce((p,F)=>D(p,F),l),[f]),D=(p,F)=>{f&&(f==null||f(p,F))},U=p=>{R(p.target.value),N(p,p.target.value)},G=p=>{E(!0),i&&C(c),v==null||v(p)},Y=p=>{E(!1),i&&C(s),g==null||g(p)};return O.jsx(ee,{style:w,className:L,theme:P,color:H,size:x,focused:y,disabled:r,readOnly:n,collapseOnBlur:i,children:O.jsx(pe,{ref:t,disabled:r,value:h,readOnly:n,minRows:q,maxRows:A,onChange:U,onFocus:G,onBlur:Y,...z})})});B.propTypes={id:a.PropTypes.string,defaultValue:a.PropTypes.string,value:a.PropTypes.string,disabled:a.PropTypes.bool,readOnly:a.PropTypes.bool,debounceTime:a.PropTypes.number,placeholder:a.PropTypes.string,tabIndex:a.PropTypes.number,collapseOnBlur:a.PropTypes.bool,minRows:a.PropTypes.number,maxRows:a.PropTypes.number,onChange:a.PropTypes.func,onBlur:a.PropTypes.func,onFocus:a.PropTypes.func,className:a.PropTypes.string,style:a.PropTypes.object,size:a.PropTypes.oneOf(["small","medium","large"]),color:a.PropTypes.oneOf(["primary","secondary","success","danger","warning","information","neutral","gray"])};module.exports=B;