primereact
Version:
PrimeReact is an open source UI library for React featuring a rich set of 90+ components, a theme designer, various theme alternatives such as Material, Bootstrap, Tailwind, premium templates and professional support. In addition, it integrates with Prime
2 lines (1 loc) • 16 kB
JavaScript
;Object.defineProperty(exports,"__esModule",{value:!0});var e=require("react"),t=require("primereact/hooks"),r=require("primereact/utils");function n(e){if(e&&e.__esModule)return e;var t=Object.create(null);return e&&Object.keys(e).forEach((function(r){if("default"!==r){var n=Object.getOwnPropertyDescriptor(e,r);Object.defineProperty(t,r,n.get?n:{enumerable:!0,get:function(){return e[r]}})}})),t.default=e,Object.freeze(t)}var o=n(e);function l(){return l=Object.assign?Object.assign.bind():function(e){for(var t=1;t<arguments.length;t++){var r=arguments[t];for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&(e[n]=r[n])}return e},l.apply(this,arguments)}function i(e){return i="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},i(e)}function s(e,t){if("object"!==i(e)||null===e)return e;var r=e[Symbol.toPrimitive];if(void 0!==r){var n=r.call(e,t||"default");if("object"!==i(n))return n;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===t?String:Number)(e)}function c(e){var t=s(e,"string");return"symbol"===i(t)?t:String(t)}function u(e,t,r){return(t=c(t))in e?Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[t]=r,e}function a(e){if(Array.isArray(e))return e}function f(e,t){var r=null==e?null:"undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(null!=r){var n,o,l,i,s=[],c=!0,u=!1;try{if(l=(r=r.call(e)).next,0===t){if(Object(r)!==r)return;c=!1}else for(;!(c=(n=l.call(r)).done)&&(s.push(n.value),s.length!==t);c=!0);}catch(e){u=!0,o=e}finally{try{if(!c&&null!=r.return&&(i=r.return(),Object(i)!==i))return}finally{if(u)throw o}}return s}}function m(e,t){(null==t||t>e.length)&&(t=e.length);for(var r=0,n=new Array(t);r<t;r++)n[r]=e[r];return n}function p(e,t){if(e){if("string"==typeof e)return m(e,t);var r=Object.prototype.toString.call(e).slice(8,-1);return"Object"===r&&e.constructor&&(r=e.constructor.name),"Map"===r||"Set"===r?Array.from(e):"Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r)?m(e,t):void 0}}function d(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}function g(e,t){return a(e)||f(e,t)||p(e,t)||d()}var h={defaultProps:{__TYPE:"VirtualScroller",id:null,style:null,className:null,tabIndex:0,items:null,itemSize:0,scrollHeight:null,scrollWidth:null,orientation:"vertical",step:0,numToleratedItems:null,delay:0,resizeDelay:10,appendOnly:!1,inline:!1,lazy:!1,disabled:!1,loaderDisabled:!1,columns:null,loading:void 0,autoSize:!1,showSpacer:!0,showLoader:!1,loadingTemplate:null,loaderIconTemplate:null,itemTemplate:null,contentTemplate:null,onScroll:null,onScrollIndexChange:null,onLazyLoad:null,children:void 0},getProps:function(e){return r.ObjectUtils.getMergedProps(e,h.defaultProps)},getOtherProps:function(e){return r.ObjectUtils.getDiffProps(e,h.defaultProps)}};function v(e,t){var r=Object.keys(e);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(e);t&&(n=n.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),r.push.apply(r,n)}return r}function y(e){for(var t=1;t<arguments.length;t++){var r=null!=arguments[t]?arguments[t]:{};t%2?v(Object(r),!0).forEach((function(t){u(e,t,r[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(r)):v(Object(r)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(r,t))}))}return e}var w=o.memo(o.forwardRef((function(e,n){var i=h.getProps(e),s=t.usePrevious(e)||{},c="vertical"===i.orientation,a="horizontal"===i.orientation,f="both"===i.orientation,m=g(o.useState(f?{rows:0,cols:0}:0),2),p=m[0],d=m[1],v=g(o.useState(f?{rows:0,cols:0}:0),2),w=v[0],b=v[1],S=g(o.useState(0),2),O=S[0],z=S[1],j=g(o.useState(f?{rows:0,cols:0}:0),2),T=j[0],E=j[1],x=g(o.useState(i.numToleratedItems),2),I=x[0],P=x[1],R=g(o.useState(i.loading||!1),2),H=R[0],D=R[1],L=g(o.useState([]),2),M=L[0],U=L[1],N=o.useRef(null),W=o.useRef(null),F=o.useRef(null),A=o.useRef(null),C=o.useRef(f?{top:0,left:0}:0),V=o.useRef(null),k=o.useRef(null),_=o.useRef({}),J=o.useRef({}),X=o.useRef(null),q=o.useRef(null),B=o.useRef(null),Y=o.useRef(null),$=o.useRef(!1),G=o.useRef(null),K=g(t.useResizeListener({listener:function(e){return ge()},when:!i.disabled}),1)[0],Q=g(t.useEventListener({target:"window",type:"orientationchange",listener:function(e){return ge()},when:!i.disabled}),1)[0],Z=function(){return N},ee=function(e){return Math.floor((e+4*I)/(i.step||1))},te=function(e){return!i.step||O!==ee(e)},re=function(e){C.current=f?{top:0,left:0}:0,N.current&&N.current.scrollTo(e)},ne=function(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"auto",r=ie().numToleratedItems,n=ue(),o=function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:0;return e<=(arguments.length>1?arguments[1]:void 0)?0:e},l=function(e,t,r){return e*t+r},s=function(){return re({left:arguments.length>0&&void 0!==arguments[0]?arguments[0]:0,top:arguments.length>1&&void 0!==arguments[1]?arguments[1]:0,behavior:t})},c=f?{rows:0,cols:0}:0,u=!1;f?(s(l((c={rows:o(e[0],r[0]),cols:o(e[1],r[1])}).cols,i.itemSize[1],n.left),l(c.rows,i.itemSize[0],n.top)),u=p.rows!==c.rows||p.cols!==c.cols):(c=o(e,r),a?s(l(c,i.itemSize,n.left),0):s(0,l(c,i.itemSize,n.top)),u=p!==c),$.current=u,d(c)},oe=function(e,t){var r=arguments.length>2&&void 0!==arguments[2]?arguments[2]:"auto";if(t){var n=le(),o=n.first,l=n.viewport,s=function(){return re({left:arguments.length>0&&void 0!==arguments[0]?arguments[0]:0,top:arguments.length>1&&void 0!==arguments[1]?arguments[1]:0,behavior:r})},c="to-end"===t;if("to-start"===t){if(f)l.first.rows-o.rows>e[0]?s(l.first.cols*i.itemSize[1],(l.first.rows-1)*i.itemSize[0]):l.first.cols-o.cols>e[1]&&s((l.first.cols-1)*i.itemSize[1],l.first.rows*i.itemSize[0]);else if(l.first-o>e){var u=(l.first-1)*i.itemSize;a?s(u,0):s(0,u)}}else if(c)if(f)l.last.rows-o.rows<=e[0]+1?s(l.first.cols*i.itemSize[1],(l.first.rows+1)*i.itemSize[0]):l.last.cols-o.cols<=e[1]+1&&s((l.first.cols+1)*i.itemSize[1],l.first.rows*i.itemSize[0]);else if(l.last-o<=e+1){var m=(l.first+1)*i.itemSize;a?s(m,0):s(0,m)}}else ne(e,r)},le=function(){var e=function(e,t){return Math.floor(e/(t||e))},t=p,r=0;if(N.current){var n=N.current,o=n.scrollTop,l=n.scrollLeft;if(f)r={rows:(t={rows:e(o,i.itemSize[0]),cols:e(l,i.itemSize[1])}).rows+T.rows,cols:t.cols+T.cols};else r=(t=e(a?l:o,i.itemSize))+T}return{first:p,last:w,viewport:{first:t,last:r}}},ie=function(){var e=ue(),t=N.current?N.current.offsetWidth-e.left:0,r=N.current?N.current.offsetHeight-e.top:0,n=function(e,t){return Math.ceil(e/(t||e))},o=function(e){return Math.ceil(e/2)},l=f?{rows:n(r,i.itemSize[0]),cols:n(t,i.itemSize[1])}:n(a?t:r,i.itemSize);return{numItemsInViewport:l,numToleratedItems:I||(f?[o(l.rows),o(l.cols)]:o(l))}},se=function(e){i.autoSize&&!e&&Promise.resolve().then((function(){if(W.current){W.current.style.minHeight=W.current.style.minWidth="auto",W.current.style.position="relative",N.current.style.contain="none";var e=[r.DomHandler.getWidth(N.current),r.DomHandler.getHeight(N.current)],t=e[0],n=e[1];(f||a)&&(N.current.style.width=(t<X.current?t:i.scrollWidth||X.current)+"px"),(f||c)&&(N.current.style.height=(n<q.current?n:i.scrollHeight||q.current)+"px"),W.current.style.minHeight=W.current.style.minWidth="",W.current.style.position="",N.current.style.contain=""}}))},ce=function(){return i.items?Math.min((arguments.length>1?arguments[1]:void 0)?(i.columns||i.items[0]).length:i.items.length,arguments.length>0&&void 0!==arguments[0]?arguments[0]:0):0},ue=function(){if(W.current){var e=getComputedStyle(W.current),t=parseFloat(e.paddingLeft)+Math.max(parseFloat(e.left)||0,0),r=parseFloat(e.paddingRight)+Math.max(parseFloat(e.right)||0,0),n=parseFloat(e.paddingTop)+Math.max(parseFloat(e.top)||0,0),o=parseFloat(e.paddingBottom)+Math.max(parseFloat(e.bottom)||0,0);return{left:t,right:r,top:n,bottom:o,x:t+r,y:n+o}}return{left:0,right:0,top:0,bottom:0,x:0,y:0}},ae=function(){if(N.current){var e=N.current.parentElement,t=i.scrollWidth||"".concat(N.current.offsetWidth||e.offsetWidth,"px"),r=i.scrollHeight||"".concat(N.current.offsetHeight||e.offsetHeight,"px"),n=function(e,t){return N.current.style[e]=t};f||a?(n("height",r),n("width",t)):n("height",r)}},fe=function(){var e=i.items;if(e){var t=ue(),r=function(e,t,r){var n=arguments.length>3&&void 0!==arguments[3]?arguments[3]:0;return J.current=y(y({},J.current),u({},"".concat(e),(t||[]).length*r+n+"px"))};f?(r("height",e,i.itemSize[0],t.y),r("width",i.columns||e[1],i.itemSize[1],t.x)):a?r("width",i.columns||e,i.itemSize,t.x):r("height",e,i.itemSize,t.y)}},me=function(e){if(W.current&&!i.appendOnly){var t=e?e.first:p,r=function(e,t){return e*t},n=function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:0,t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:0;A.current&&(A.current.style.top="-".concat(t,"px")),_.current=y(y({},_.current),{transform:"translate3d(".concat(e,"px, ").concat(t,"px, 0)")})};if(f)n(r(t.cols,i.itemSize[1]),r(t.rows,i.itemSize[0]));else{var o=r(t,i.itemSize);a?n(o,0):n(0,o)}}},pe=function(e){var t=e.target,r=ue(),n=function(e,t){return e?e>t?e-t:e:0},o=function(e,t){return Math.floor(e/(t||e))},l=function(e,t,r,n,o,l){return e<=o?o:l?r-n-o:t+o-1},s=function(e,t,r,n,o,l,i){return e<=l?0:Math.max(0,i?e<t?r:e-l:e>t?r:e-2*l)},c=function(e,t,r,n,o,l){var i=t+n+2*o;return e>=o&&(i+=o+1),ce(i,l)},u=n(t.scrollTop,r.top),m=n(t.scrollLeft,r.left),d=f?{rows:0,cols:0}:0,g=w,h=!1,v=C.current;if(f){var y=C.current.top<=u,b=C.current.left<=m;if(!i.appendOnly||i.appendOnly&&(y||b)){var S={rows:o(u,i.itemSize[0]),cols:o(m,i.itemSize[1])},O={rows:l(S.rows,p.rows,w.rows,T.rows,I[0],y),cols:l(S.cols,p.cols,w.cols,T.cols,I[1],b)};d={rows:s(S.rows,O.rows,p.rows,0,0,I[0],y),cols:s(S.cols,O.cols,p.cols,0,0,I[1],b)},g={rows:c(S.rows,d.rows,0,T.rows,I[0]),cols:c(S.cols,d.cols,0,T.cols,I[1],!0)},h=d.rows!==p.rows||g.rows!==w.rows||d.cols!==p.cols||g.cols!==w.cols||$.current,v={top:u,left:m}}}else{var z=a?m:u,j=C.current<=z;if(!i.appendOnly||i.appendOnly&&j){var E=o(z,i.itemSize);g=c(E,d=s(E,l(E,p,w,T,I,j),p,0,0,I,j),0,T,I),h=d!==p||g!==w||$.current,v=z}}return{first:d,last:g,isRangeChanged:h,scrollPos:v}},de=function(e){var t=pe(e),r=t.first,n=t.last,o=t.scrollPos;if(t.isRangeChanged){var l={first:r,last:n};if(me(l),d(r),b(n),C.current=o,i.onScrollIndexChange&&i.onScrollIndexChange(l),i.lazy&&te(r)){var s={first:i.step?Math.min(ee(r)*i.step,i.items.length-i.step):r,last:Math.min(i.step?(ee(r)+1)*i.step:n,i.items.length)};(!G.current||G.current.first!==s.first||G.current.last!==s.last)&&i.onLazyLoad&&i.onLazyLoad(s),G.current=s}}},ge=function(){k.current&&clearTimeout(k.current),k.current=setTimeout((function(){if(N.current){var e=[r.DomHandler.getWidth(N.current),r.DomHandler.getHeight(N.current)],t=e[0],n=e[1],o=t!==X.current,l=n!==q.current;(f?o||l:a?o:!!c&&l)&&(P(i.numToleratedItems),X.current=t,q.current=n,B.current=r.DomHandler.getWidth(W.current),Y.current=r.DomHandler.getHeight(W.current))}}),i.resizeDelay)},he=function(e){var t=(i.items||[]).length,r=f?p.rows+e:p+e;return{index:r,count:t,first:0===r,last:r===t-1,even:r%2==0,odd:r%2!=0,props:i}},ve=function(e,t){var r=M.length;return y({index:e,count:r,first:0===e,last:e===r-1,even:e%2==0,odd:e%2!=0,props:i},t)},ye=function(){var e=i.items;return e&&!H?f?e.slice(i.appendOnly?0:p.rows,w.rows).map((function(e){return i.columns?e:e.slice(i.appendOnly?0:p.cols,w.cols)})):a&&i.columns?e:e.slice(i.appendOnly?0:p,w):[]},we=function(){var e,t,r,n,o;i.disabled||(ae(),e=ie(),t=e.numItemsInViewport,r=e.numToleratedItems,n=function(e,t,r){return ce(e+t+(e<r?2:3)*r,arguments.length>3&&void 0!==arguments[3]&&arguments[3])},o=f?{rows:n(p.rows,t.rows,r[0]),cols:n(p.cols,t.cols,r[1],!0)}:n(p,t,r),E(t),P(r),b(o),i.showLoader&&U(f?Array.from({length:t.rows}).map((function(){return Array.from({length:t.cols})})):Array.from({length:t})),i.lazy&&Promise.resolve().then((function(){G.current={first:i.step?f?{rows:0,cols:p.cols}:0:p,last:Math.min(i.step?i.step:o,i.items.length)},i.onLazyLoad&&i.onLazyLoad(G.current)})),fe())};t.useMountEffect((function(){N.current&&r.DomHandler.isVisible(N.current)&&(W.current=W.current||W.current||r.DomHandler.findSingle(N.current,".p-virtualscroller-content"),we(),K(),Q(),X.current=r.DomHandler.getWidth(N.current),q.current=r.DomHandler.getHeight(N.current),B.current=r.DomHandler.getWidth(W.current),Y.current=r.DomHandler.getHeight(W.current))})),t.useUpdateEffect((function(){we()}),[i.itemSize,i.scrollHeight,i.scrollWidth]),t.useUpdateEffect((function(){i.numToleratedItems!==I&&P(i.numToleratedItems)}),[i.numToleratedItems]),t.useUpdateEffect((function(){i.numToleratedItems===I&&we()}),[I]),t.useUpdateEffect((function(){s.items&&s.items.length===(i.items||[]).length||we();var e=H;i.lazy&&s.loading!==i.loading&&i.loading!==H&&(D(i.loading),e=i.loading),se(e)})),t.useUpdateEffect((function(){C.current=f?{top:0,left:0}:0}),[i.orientation]),o.useImperativeHandle(n,(function(){return{props:i,getElementRef:Z,scrollTo:re,scrollToIndex:ne,scrollInView:oe,getRenderedRange:le}}));var be=function(e){var t=ve(e,arguments.length>1&&void 0!==arguments[1]?arguments[1]:{}),n=r.ObjectUtils.getJSXElement(i.loadingTemplate,t);return o.createElement(o.Fragment,{key:e},n)},Se=function(e,t){var n=he(t),l=r.ObjectUtils.getJSXElement(i.itemTemplate,e,n);return o.createElement(o.Fragment,{key:n.index},l)};if(i.disabled){var Oe=r.ObjectUtils.getJSXElement(i.contentTemplate,{items:i.items,rows:i.items,columns:i.columns});return o.createElement(o.Fragment,null,i.children,Oe)}var ze=h.getOtherProps(i),je=r.classNames("p-virtualscroller",{"p-virtualscroller-inline":i.inline,"p-virtualscroller-both p-both-scroll":f,"p-virtualscroller-horizontal p-horizontal-scroll":a},i.className),Te=function(){if(!i.loaderDisabled&&i.showLoader&&H){var e=r.classNames("p-virtualscroller-loader",{"p-component-overlay":!i.loadingTemplate}),t=o.createElement("i",{className:"p-virtualscroller-loading-icon pi pi-spinner pi-spin"});return i.loadingTemplate?t=M.map((function(e,t){return be(t,f&&{numCols:T.cols})})):i.loaderIconTemplate&&(t=r.ObjectUtils.getJSXElement(i.loaderIconTemplate,{className:"p-virtualscroller-loading-icon",element:t,props:i})),o.createElement("div",{className:e},t)}return null}(),Ee=function(){var e=ye().map(Se),t=r.classNames("p-virtualscroller-content",{"p-virtualscroller-loading":H}),n=o.createElement("div",{ref:W,style:_.current,className:t},e);if(i.contentTemplate){var l={style:_.current,className:t,spacerStyle:J.current,contentRef:function(e){return W.current=r.ObjectUtils.getRefElement(e)},spacerRef:function(e){return F.current=r.ObjectUtils.getRefElement(e)},stickyRef:function(e){return A.current=r.ObjectUtils.getRefElement(e)},items:ye(),getItemOptions:function(e){return he(e)},children:e,element:n,props:i,loading:H,getLoaderOptions:function(e,t){return ve(e,t)},loadingTemplate:i.loadingTemplate,itemSize:i.itemSize,rows:H?i.loaderDisabled?M:[]:ye(),columns:i.columns&&f||a?H&&i.loaderDisabled?f?M[0]:M:i.columns.slice(f?p.cols:p,f?w.cols:w):i.columns,vertical:c,horizontal:a,both:f};return r.ObjectUtils.getJSXElement(i.contentTemplate,l)}return n}(),xe=i.showSpacer?o.createElement("div",{ref:F,style:J.current,className:"p-virtualscroller-spacer"}):null;return o.createElement("div",l({ref:N,className:je,tabIndex:i.tabIndex,style:i.style},ze,{onScroll:function(e){i.onScroll&&i.onScroll(e),i.delay?(V.current&&clearTimeout(V.current),te(p)&&(!H&&i.showLoader&&(pe(e).isRangeChanged||i.step&&te(p))&&D(!0),V.current=setTimeout((function(){de(e),!H||!i.showLoader||i.lazy&&void 0!==i.loading||(D(!1),z(ee(p)))}),i.delay))):de(e)}}),Ee,xe,Te)})));w.displayName="VirtualScroller",exports.VirtualScroller=w;