primereact
Version:
PrimeReact is an open source UI library for React featuring a rich set of 80+ 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) • 13.5 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,t,r){return t in e?Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[t]=r,e}function s(e){if(Array.isArray(e))return e}function c(e,t){var r=null==e?null:"undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(null!=r){var n,o,l=[],i=!0,s=!1;try{for(r=r.call(e);!(i=(n=r.next()).done)&&(l.push(n.value),!t||l.length!==t);i=!0);}catch(e){s=!0,o=e}finally{try{i||null==r.return||r.return()}finally{if(s)throw o}}return l}}function a(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 u(e,t){if(e){if("string"==typeof e)return a(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)?a(e,t):void 0}}function f(){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 m(e,t){return s(e)||c(e,t)||u(e,t)||f()}function d(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 p(e){for(var t=1;t<arguments.length;t++){var r=null!=arguments[t]?arguments[t]:{};t%2?d(Object(r),!0).forEach((function(t){i(e,t,r[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(r)):d(Object(r)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(r,t))}))}return e}var g=o.memo(o.forwardRef((function(e,n){var i="vertical"===e.orientation,s="horizontal"===e.orientation,c="both"===e.orientation,a=m(o.useState(c?{rows:0,cols:0}:0),2),u=a[0],f=a[1],d=m(o.useState(c?{rows:0,cols:0}:0),2),h=d[0],v=d[1],w=m(o.useState(c?{rows:0,cols:0}:0),2),y=w[0],S=w[1],b=m(o.useState(e.numToleratedItems),2),z=b[0],O=b[1],T=m(o.useState(e.loading||!1),2),I=T[0],j=T[1],E=m(o.useState([]),2),x=E[0],R=E[1],L=o.useRef(null),P=o.useRef(null),D=o.useRef(null),H=o.useRef(null),U=o.useRef(c?{top:0,left:0}:0),M=o.useRef(null),N=o.useRef(null),W=o.useRef(null),A=o.useRef(null),C=t.usePrevious(e.items),k=t.usePrevious(e.loading),V=m(t.useResizeListener({listener:function(e){return ne()}}),1)[0],_=m(t.useEventListener({target:"window",type:"orientationchange",listener:function(e){return ne()}}),1)[0],J=function(){return L},X=function(e){U.current=c?{top:0,left:0}:0,L.current&&L.current.scrollTo(e)},q=function(t){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"auto",n=K(),o=n.numToleratedItems,l=function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:0,t=arguments.length>1?arguments[1]:void 0;return e<=t?0:e},i=function(e,t){return e*t},a=function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:0,t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:0;return X({left:e,top:t,behavior:r})};if(c){var f={rows:l(t[0],o[0]),cols:l(t[1],o[1])};f.rows===u.rows&&f.cols===u.cols||a(i(f.cols,e.itemSize[1]),i(f.rows,e.itemSize[0]))}else{var m=l(t,o);m!==u&&(s?a(i(m,e.itemSize),0):a(0,i(m,e.itemSize)))}},F=function(t,r){var n=arguments.length>2&&void 0!==arguments[2]?arguments[2]:"auto";if(r){var o=B(),l=o.first,i=o.viewport,a=function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:0,t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:0;return X({left:e,top:t,behavior:n})},u="to-start"===r,f="to-end"===r;if(u){if(c)i.first.rows-l.rows>t[0]?a(i.first.cols*e.itemSize[1],(i.first.rows-1)*e.itemSize[0]):i.first.cols-l.cols>t[1]&&a((i.first.cols-1)*e.itemSize[1],i.first.rows*e.itemSize[0]);else if(i.first-l>t){var m=(i.first-1)*e.itemSize;s?a(m,0):a(0,m)}}else if(f)if(c)i.last.rows-l.rows<=t[0]+1?a(i.first.cols*e.itemSize[1],(i.first.rows+1)*e.itemSize[0]):i.last.cols-l.cols<=t[1]+1&&a((i.first.cols+1)*e.itemSize[1],i.first.rows*e.itemSize[0]);else if(i.last-l<=t+1){var d=(i.first+1)*e.itemSize;s?a(d,0):a(0,d)}}else q(t,n)},B=function(){var t=function(e,t){return Math.floor(e/(t||e))},r=u,n=0;if(L.current){var o=L.current,l=o.scrollTop,i=o.scrollLeft;if(c)n={rows:(r={rows:t(l,e.itemSize[0]),cols:t(i,e.itemSize[1])}).rows+y.rows,cols:r.cols+y.cols};else n=(r=t(s?i:l,e.itemSize))+y}return{first:u,last:h,viewport:{first:r,last:n}}},K=function(){var t=G(),r=L.current?L.current.offsetWidth-t.left:0,n=L.current?L.current.offsetHeight-t.top:0,o=function(e,t){return Math.ceil(e/(t||e))},l=function(e){return Math.ceil(e/2)},i=c?{rows:o(n,e.itemSize[0]),cols:o(r,e.itemSize[1])}:o(s?r:n,e.itemSize);return{numItemsInViewport:i,numToleratedItems:z||(c?[l(i.rows),l(i.cols)]:l(i))}},Y=function(t){e.autoSize&&!t&&Promise.resolve().then((function(){if(P.current){P.current.style.minHeight=P.current.style.minWidth="auto";var e=P.current,t=e.offsetWidth,r=e.offsetHeight;(c||s)&&(L.current.style.width=(t<W.current?t:W.current)+"px"),(c||i)&&(L.current.style.height=(r<A.current?r:A.current)+"px"),P.current.style.minHeight=P.current.style.minWidth=""}}))},$=function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:0,r=arguments.length>1?arguments[1]:void 0;return e.items?Math.min(r?(e.columns||e.items[0]).length:e.items.length,t):0},G=function(){if(P.current){var e=getComputedStyle(P.current),t=parseInt(e.paddingLeft,10)+Math.max(parseInt(e.left,10),0),r=parseInt(e.paddingRight,10)+Math.max(parseInt(e.right,10),0),n=parseInt(e.paddingTop,10)+Math.max(parseInt(e.top,10),0),o=parseInt(e.paddingBottom,10)+Math.max(parseInt(e.bottom,10),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}},Q=function(){if(L.current){var t=L.current.parentElement,r=e.scrollWidth||"".concat(L.current.offsetWidth||t.offsetWidth,"px"),n=e.scrollHeight||"".concat(L.current.offsetHeight||t.offsetHeight,"px"),o=function(e,t){return L.current.style[e]=t};c||s?(o("height",n),o("width",r)):o("height",n)}},Z=function(){var t=e.items;if(D.current&&t){var r=G(),n=function(e,t,r){var n=arguments.length>3&&void 0!==arguments[3]?arguments[3]:0;return D.current.style[e]=(t||[]).length*r+n+"px"};c?(n("height",t,e.itemSize[0],r.y),n("width",e.columns||t[1],e.itemSize[1],r.x)):s?n("width",e.columns||t,e.itemSize,r.x):n("height",t,e.itemSize,r.y)}},ee=function(t){if(P.current){var r=t?t.first:u,n=function(e,t){return e*t},o=function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:0,t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:0;H.current&&(H.current.style.top="-".concat(t,"px")),P.current.style.transform="translate3d(".concat(e,"px, ").concat(t,"px, 0)")};if(c)o(n(r.cols,e.itemSize[1]),n(r.rows,e.itemSize[0]));else{var l=n(r,e.itemSize);s?o(l,0):o(0,l)}}},te=function(t){var r=t.target,n=G(),o=function(e,t){return e?e>t?e-t:e:0},l=function(e,t){return Math.floor(e/(t||e))},i=function(e,t,r,n,o,l){return e<=o?o:l?r-n-o:t+o-1},a=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)},f=function(e,t,r,n,o,l){var i=t+n+2*o;return e>=o&&(i+=o+1),$(i,l)},m=o(r.scrollTop,n.top),d=o(r.scrollLeft,n.left),p=c?{rows:0,cols:0}:0,g=h,v=!1,w=U.current;if(c){var S=U.current.top<=m,b=U.current.left<=d,O={rows:l(m,e.itemSize[0]),cols:l(d,e.itemSize[1])},T={rows:i(O.rows,u.rows,h.rows,y.rows,z[0],S),cols:i(O.cols,u.cols,h.cols,y.cols,z[1],b)};p={rows:a(O.rows,T.rows,u.rows,0,0,z[0],S),cols:a(O.cols,T.cols,u.cols,0,0,z[1],b)},g={rows:f(O.rows,p.rows,0,y.rows,z[0]),cols:f(O.cols,p.cols,0,y.cols,z[1],!0)},v=p.rows!==u.rows||g.rows!==h.rows||p.cols!==u.cols||g.cols!==h.cols,w={top:m,left:d}}else{var I=s?d:m,j=U.current<=I,E=l(I,e.itemSize);g=f(E,p=a(E,i(E,u,h,y,z,j),u,0,0,z,j),0,y,z),v=p!==u||g!==h,w=I}return{first:p,last:g,isRangeChanged:v,scrollPos:w}},re=function(t){var r=te(t),n=r.first,o=r.last,l=r.scrollPos;if(r.isRangeChanged){var i={first:n,last:o};ee(i),f(n),v(o),U.current=l,e.onScrollIndexChange&&e.onScrollIndexChange(i),e.lazy&&e.onLazyLoad&&e.onLazyLoad(i)}},ne=function(){N.current&&clearTimeout(N.current),N.current=setTimeout((function(){if(L.current){var t=[r.DomHandler.getWidth(L.current),r.DomHandler.getHeight(L.current)],n=t[0],o=t[1],l=n!==W.current,a=o!==A.current;(c?l||a:s?l:!!i&&a)&&(O(e.numToleratedItems),W.current=n,A.current=o)}}),e.resizeDelay)},oe=function(t){var r=(e.items||[]).length,n=c?u.rows+t:u+t;return{index:n,count:r,first:0===n,last:n===r-1,even:n%2==0,odd:n%2!=0,props:e}},le=function(t,r){var n=x.length;return p({index:t,count:n,first:0===t,last:t===n-1,even:t%2==0,odd:t%2!=0,props:e},r)},ie=function(){var t=e.items;return t&&!I?c?t.slice(u.rows,h.rows).map((function(t){return e.columns?t:t.slice(u.cols,h.cols)})):s&&e.columns?t:t.slice(u,h):[]},se=function(){var t,r,n,o,l;e.disabled||(Q(),t=K(),r=t.numItemsInViewport,n=t.numToleratedItems,o=function(e,t,r,n){return $(e+t+(e<r?2:3)*r,n)},l=c?{rows:o(u.rows,r.rows,n[0]),cols:o(u.cols,r.cols,n[1],!0)}:o(u,r,n),S(r),O(n),v(l),e.showLoader&&R(c?Array.from({length:r.rows}).map((function(){return Array.from({length:r.cols})})):Array.from({length:r})),e.lazy&&e.onLazyLoad&&e.onLazyLoad({first:u,last:l}),Z())};t.useMountEffect((function(){e.disabled||(se(),V(),_(),W.current=r.DomHandler.getWidth(L.current),A.current=r.DomHandler.getHeight(L.current))})),t.useUpdateEffect((function(){se()}),[e.itemSize,e.scrollHeight]),t.useUpdateEffect((function(){e.numToleratedItems!==z&&O(e.numToleratedItems)}),[e.numToleratedItems]),t.useUpdateEffect((function(){e.numToleratedItems===z&&se()}),[z]),t.useUpdateEffect((function(){C&&C.length===(e.items||[]).length||se();var t=I;e.lazy&&k!==e.loading&&e.loading!==I&&(j(e.loading),t=e.loading),Y(t)})),t.useUpdateEffect((function(){U.current=c?{top:0,left:0}:0}),[e.orientation]),o.useImperativeHandle(n,(function(){return{props:e,getElementRef:J,scrollTo:X,scrollToIndex:q,scrollInView:F,getRenderedRange:B}}));var ce=function(t){var n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},l=le(t,n),i=r.ObjectUtils.getJSXElement(e.loadingTemplate,l);return o.createElement(o.Fragment,{key:t},i)},ae=function(t,n){var l=oe(n),i=r.ObjectUtils.getJSXElement(e.itemTemplate,t,l);return o.createElement(o.Fragment,{key:l.index},i)};if(e.disabled){var ue=r.ObjectUtils.getJSXElement(e.contentTemplate,{items:e.items,rows:e.items,columns:e.columns});return o.createElement(o.Fragment,null,e.children,ue)}var fe=r.ObjectUtils.findDiffKeys(e,g.defaultProps),me=r.classNames("p-virtualscroller",{"p-both-scroll":c,"p-horizontal-scroll":s},e.className),de=function(){if(!e.loaderDisabled&&e.showLoader&&I){var t=r.classNames("p-virtualscroller-loader",{"p-component-overlay":!e.loadingTemplate}),n=o.createElement("i",{className:"p-virtualscroller-loading-icon pi pi-spinner pi-spin"});return e.loadingTemplate?n=x.map((function(e,t){return ce(t,c&&{numCols:y.cols})})):e.loaderIconTemplate&&(n=r.ObjectUtils.getJSXElement(e.loaderIconTemplate,{className:"p-virtualscroller-loading-icon",element:n,props:e})),o.createElement("div",{className:t},n)}return null}(),pe=function(){var t=ie().map(ae),n=r.classNames("p-virtualscroller-content",{"p-virtualscroller-loading":I}),l=o.createElement("div",{ref:P,className:n},t);if(e.contentTemplate){var a={className:n,contentRef:function(e){return P.current=r.ObjectUtils.getRefElement(e)},spacerRef:function(e){return D.current=r.ObjectUtils.getRefElement(e)},stickyRef:function(e){return H.current=r.ObjectUtils.getRefElement(e)},items:ie(),getItemOptions:function(e){return oe(e)},children:t,element:l,props:e,loading:I,getLoaderOptions:function(e,t){return le(e,t)},loadingTemplate:e.loadingTemplate,itemSize:e.itemSize,rows:I?e.loaderDisabled?x:[]:ie(),columns:e.columns&&c||s?I&&e.loaderDisabled?c?x[0]:x:e.columns.slice(c?u.cols:u,c?h.cols:h):e.columns,vertical:i,horizontal:s,both:c};return r.ObjectUtils.getJSXElement(e.contentTemplate,a)}return l}(),ge=e.showSpacer?o.createElement("div",{ref:D,className:"p-virtualscroller-spacer"}):null;return o.createElement("div",l({ref:L,className:me,tabIndex:0,style:e.style},fe,{onScroll:function(t){e.onScroll&&e.onScroll(t),e.delay?(M.current&&clearTimeout(M.current),!I&&e.showLoader&&te(t).isRangeChanged&&j(!0),M.current=setTimeout((function(){re(t),!I||!e.showLoader||e.lazy&&void 0!==e.loading||j(!1)}),e.delay)):re(t)}}),pe,ge,de)})));g.displayName="VirtualScroller",g.defaultProps={__TYPE:"VirtualScroller",id:null,style:null,className:null,items:null,itemSize:0,scrollHeight:null,scrollWidth:null,orientation:"vertical",numToleratedItems:null,delay:0,resizeDelay:10,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},exports.VirtualScroller=g;