UNPKG

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) 15.4 kB
import*as e from"react";import{usePrevious as t,useResizeListener as r,useEventListener as n,useMountEffect as o,useUpdateEffect as l}from"primereact/hooks";import{ObjectUtils as i,DomHandler as s,classNames as c}from"primereact/utils";function u(){return u=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},u.apply(this,arguments)}function a(e){return a="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},a(e)}function f(e,t){if("object"!==a(e)||null===e)return e;var r=e[Symbol.toPrimitive];if(void 0!==r){var n=r.call(e,t||"default");if("object"!==a(n))return n;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===t?String:Number)(e)}function m(e){var t=f(e,"string");return"symbol"===a(t)?t:String(t)}function p(e,t,r){return(t=m(t))in e?Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[t]=r,e}function d(e){if(Array.isArray(e))return e}function g(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 h(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 v(e,t){if(e){if("string"==typeof e)return h(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)?h(e,t):void 0}}function y(){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 w(e,t){return d(e)||g(e,t)||v(e,t)||y()}var S={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 i.getMergedProps(e,S.defaultProps)},getOtherProps:function(e){return i.getDiffProps(e,S.defaultProps)}};function b(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 z(e){for(var t=1;t<arguments.length;t++){var r=null!=arguments[t]?arguments[t]:{};t%2?b(Object(r),!0).forEach((function(t){p(e,t,r[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(r)):b(Object(r)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(r,t))}))}return e}var O=e.memo(e.forwardRef((function(a,f){var m=S.getProps(a),d=t(a)||{},g="vertical"===m.orientation,h="horizontal"===m.orientation,v="both"===m.orientation,y=w(e.useState(v?{rows:0,cols:0}:0),2),b=y[0],O=y[1],T=w(e.useState(v?{rows:0,cols:0}:0),2),I=T[0],x=T[1],R=w(e.useState(0),2),P=R[0],E=R[1],j=w(e.useState(v?{rows:0,cols:0}:0),2),L=j[0],M=j[1],H=w(e.useState(m.numToleratedItems),2),W=H[0],D=H[1],F=w(e.useState(m.loading||!1),2),N=F[0],A=F[1],C=w(e.useState([]),2),V=C[0],k=C[1],J=e.useRef(null),X=e.useRef(null),_=e.useRef(null),B=e.useRef(null),U=e.useRef(v?{top:0,left:0}:0),Y=e.useRef(null),$=e.useRef(null),q=e.useRef({}),G=e.useRef({}),K=e.useRef(null),Q=e.useRef(null),Z=e.useRef(null),ee=e.useRef(null),te=e.useRef(!1),re=e.useRef(null),ne=w(r({listener:function(e){return be()},when:!m.disabled}),1)[0],oe=w(n({target:"window",type:"orientationchange",listener:function(e){return be()},when:!m.disabled}),1)[0],le=function(){return J},ie=function(e){return Math.floor((e+4*W)/(m.step||1))},se=function(e){return!m.step||P!==ie(e)},ce=function(e){U.current=v?{top:0,left:0}:0,J.current&&J.current.scrollTo(e)},ue=function(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"auto",r=me().numToleratedItems,n=ge(),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},i=function(){return ce({left:arguments.length>0&&void 0!==arguments[0]?arguments[0]:0,top:arguments.length>1&&void 0!==arguments[1]?arguments[1]:0,behavior:t})},s=v?{rows:0,cols:0}:0,c=!1;v?(i(l((s={rows:o(e[0],r[0]),cols:o(e[1],r[1])}).cols,m.itemSize[1],n.left),l(s.rows,m.itemSize[0],n.top)),c=b.rows!==s.rows||b.cols!==s.cols):(s=o(e,r),h?i(l(s,m.itemSize,n.left),0):i(0,l(s,m.itemSize,n.top)),c=b!==s),te.current=c,O(s)},ae=function(e,t){var r=arguments.length>2&&void 0!==arguments[2]?arguments[2]:"auto";if(t){var n=fe(),o=n.first,l=n.viewport,i=function(){return ce({left:arguments.length>0&&void 0!==arguments[0]?arguments[0]:0,top:arguments.length>1&&void 0!==arguments[1]?arguments[1]:0,behavior:r})},s="to-end"===t;if("to-start"===t){if(v)l.first.rows-o.rows>e[0]?i(l.first.cols*m.itemSize[1],(l.first.rows-1)*m.itemSize[0]):l.first.cols-o.cols>e[1]&&i((l.first.cols-1)*m.itemSize[1],l.first.rows*m.itemSize[0]);else if(l.first-o>e){var c=(l.first-1)*m.itemSize;h?i(c,0):i(0,c)}}else if(s)if(v)l.last.rows-o.rows<=e[0]+1?i(l.first.cols*m.itemSize[1],(l.first.rows+1)*m.itemSize[0]):l.last.cols-o.cols<=e[1]+1&&i((l.first.cols+1)*m.itemSize[1],l.first.rows*m.itemSize[0]);else if(l.last-o<=e+1){var u=(l.first+1)*m.itemSize;h?i(u,0):i(0,u)}}else ue(e,r)},fe=function(){var e=function(e,t){return Math.floor(e/(t||e))},t=b,r=0;if(J.current){var n=J.current,o=n.scrollTop,l=n.scrollLeft;if(v)r={rows:(t={rows:e(o,m.itemSize[0]),cols:e(l,m.itemSize[1])}).rows+L.rows,cols:t.cols+L.cols};else r=(t=e(h?l:o,m.itemSize))+L}return{first:b,last:I,viewport:{first:t,last:r}}},me=function(){var e=ge(),t=J.current?J.current.offsetWidth-e.left:0,r=J.current?J.current.offsetHeight-e.top:0,n=function(e,t){return Math.ceil(e/(t||e))},o=function(e){return Math.ceil(e/2)},l=v?{rows:n(r,m.itemSize[0]),cols:n(t,m.itemSize[1])}:n(h?t:r,m.itemSize);return{numItemsInViewport:l,numToleratedItems:W||(v?[o(l.rows),o(l.cols)]:o(l))}},pe=function(e){m.autoSize&&!e&&Promise.resolve().then((function(){if(X.current){X.current.style.minHeight=X.current.style.minWidth="auto",X.current.style.position="relative",J.current.style.contain="none";var e=[s.getWidth(J.current),s.getHeight(J.current)],t=e[0],r=e[1];(v||h)&&(J.current.style.width=(t<K.current?t:m.scrollWidth||K.current)+"px"),(v||g)&&(J.current.style.height=(r<Q.current?r:m.scrollHeight||Q.current)+"px"),X.current.style.minHeight=X.current.style.minWidth="",X.current.style.position="",J.current.style.contain=""}}))},de=function(){return m.items?Math.min((arguments.length>1?arguments[1]:void 0)?(m.columns||m.items[0]).length:m.items.length,arguments.length>0&&void 0!==arguments[0]?arguments[0]:0):0},ge=function(){if(X.current){var e=getComputedStyle(X.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}},he=function(){if(J.current){var e=J.current.parentElement,t=m.scrollWidth||"".concat(J.current.offsetWidth||e.offsetWidth,"px"),r=m.scrollHeight||"".concat(J.current.offsetHeight||e.offsetHeight,"px"),n=function(e,t){return J.current.style[e]=t};v||h?(n("height",r),n("width",t)):n("height",r)}},ve=function(){var e=m.items;if(e){var t=ge(),r=function(e,t,r){var n=arguments.length>3&&void 0!==arguments[3]?arguments[3]:0;return G.current=z(z({},G.current),p({},"".concat(e),(t||[]).length*r+n+"px"))};v?(r("height",e,m.itemSize[0],t.y),r("width",m.columns||e[1],m.itemSize[1],t.x)):h?r("width",m.columns||e,m.itemSize,t.x):r("height",e,m.itemSize,t.y)}},ye=function(e){if(X.current&&!m.appendOnly){var t=e?e.first:b,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;B.current&&(B.current.style.top="-".concat(t,"px")),q.current=z(z({},q.current),{transform:"translate3d(".concat(e,"px, ").concat(t,"px, 0)")})};if(v)n(r(t.cols,m.itemSize[1]),r(t.rows,m.itemSize[0]));else{var o=r(t,m.itemSize);h?n(o,0):n(0,o)}}},we=function(e){var t=e.target,r=ge(),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},i=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)},s=function(e,t,r,n,o,l){var i=t+n+2*o;return e>=o&&(i+=o+1),de(i,l)},c=n(t.scrollTop,r.top),u=n(t.scrollLeft,r.left),a=v?{rows:0,cols:0}:0,f=I,p=!1,d=U.current;if(v){var g=U.current.top<=c,y=U.current.left<=u;if(!m.appendOnly||m.appendOnly&&(g||y)){var w={rows:o(c,m.itemSize[0]),cols:o(u,m.itemSize[1])},S={rows:l(w.rows,b.rows,I.rows,L.rows,W[0],g),cols:l(w.cols,b.cols,I.cols,L.cols,W[1],y)};a={rows:i(w.rows,S.rows,b.rows,0,0,W[0],g),cols:i(w.cols,S.cols,b.cols,0,0,W[1],y)},f={rows:s(w.rows,a.rows,0,L.rows,W[0]),cols:s(w.cols,a.cols,0,L.cols,W[1],!0)},p=a.rows!==b.rows||f.rows!==I.rows||a.cols!==b.cols||f.cols!==I.cols||te.current,d={top:c,left:u}}}else{var z=h?u:c,O=U.current<=z;if(!m.appendOnly||m.appendOnly&&O){var T=o(z,m.itemSize);f=s(T,a=i(T,l(T,b,I,L,W,O),b,0,0,W,O),0,L,W),p=a!==b||f!==I||te.current,d=z}}return{first:a,last:f,isRangeChanged:p,scrollPos:d}},Se=function(e){var t=we(e),r=t.first,n=t.last,o=t.scrollPos;if(t.isRangeChanged){var l={first:r,last:n};if(ye(l),O(r),x(n),U.current=o,m.onScrollIndexChange&&m.onScrollIndexChange(l),m.lazy&&se(r)){var i={first:m.step?Math.min(ie(r)*m.step,m.items.length-m.step):r,last:Math.min(m.step?(ie(r)+1)*m.step:n,m.items.length)};(!re.current||re.current.first!==i.first||re.current.last!==i.last)&&m.onLazyLoad&&m.onLazyLoad(i),re.current=i}}},be=function(){$.current&&clearTimeout($.current),$.current=setTimeout((function(){if(J.current){var e=[s.getWidth(J.current),s.getHeight(J.current)],t=e[0],r=e[1],n=t!==K.current,o=r!==Q.current;(v?n||o:h?n:!!g&&o)&&(D(m.numToleratedItems),K.current=t,Q.current=r,Z.current=s.getWidth(X.current),ee.current=s.getHeight(X.current))}}),m.resizeDelay)},ze=function(e){var t=(m.items||[]).length,r=v?b.rows+e:b+e;return{index:r,count:t,first:0===r,last:r===t-1,even:r%2==0,odd:r%2!=0,props:m}},Oe=function(e,t){var r=V.length;return z({index:e,count:r,first:0===e,last:e===r-1,even:e%2==0,odd:e%2!=0,props:m},t)},Te=function(){var e=m.items;return e&&!N?v?e.slice(m.appendOnly?0:b.rows,I.rows).map((function(e){return m.columns?e:e.slice(m.appendOnly?0:b.cols,I.cols)})):h&&m.columns?e:e.slice(m.appendOnly?0:b,I):[]},Ie=function(){var e,t,r,n,o;m.disabled||(he(),e=me(),t=e.numItemsInViewport,r=e.numToleratedItems,n=function(e,t,r){return de(e+t+(e<r?2:3)*r,arguments.length>3&&void 0!==arguments[3]&&arguments[3])},o=v?{rows:n(b.rows,t.rows,r[0]),cols:n(b.cols,t.cols,r[1],!0)}:n(b,t,r),M(t),D(r),x(o),m.showLoader&&k(v?Array.from({length:t.rows}).map((function(){return Array.from({length:t.cols})})):Array.from({length:t})),m.lazy&&Promise.resolve().then((function(){re.current={first:m.step?v?{rows:0,cols:b.cols}:0:b,last:Math.min(m.step?m.step:o,m.items.length)},m.onLazyLoad&&m.onLazyLoad(re.current)})),ve())};o((function(){J.current&&s.isVisible(J.current)&&(X.current=X.current||X.current||s.findSingle(J.current,".p-virtualscroller-content"),Ie(),ne(),oe(),K.current=s.getWidth(J.current),Q.current=s.getHeight(J.current),Z.current=s.getWidth(X.current),ee.current=s.getHeight(X.current))})),l((function(){Ie()}),[m.itemSize,m.scrollHeight,m.scrollWidth]),l((function(){m.numToleratedItems!==W&&D(m.numToleratedItems)}),[m.numToleratedItems]),l((function(){m.numToleratedItems===W&&Ie()}),[W]),l((function(){d.items&&d.items.length===(m.items||[]).length||Ie();var e=N;m.lazy&&d.loading!==m.loading&&m.loading!==N&&(A(m.loading),e=m.loading),pe(e)})),l((function(){U.current=v?{top:0,left:0}:0}),[m.orientation]),e.useImperativeHandle(f,(function(){return{props:m,getElementRef:le,scrollTo:ce,scrollToIndex:ue,scrollInView:ae,getRenderedRange:fe}}));var xe=function(t){var r=Oe(t,arguments.length>1&&void 0!==arguments[1]?arguments[1]:{}),n=i.getJSXElement(m.loadingTemplate,r);return e.createElement(e.Fragment,{key:t},n)},Re=function(t,r){var n=ze(r),o=i.getJSXElement(m.itemTemplate,t,n);return e.createElement(e.Fragment,{key:n.index},o)};if(m.disabled){var Pe=i.getJSXElement(m.contentTemplate,{items:m.items,rows:m.items,columns:m.columns});return e.createElement(e.Fragment,null,m.children,Pe)}var Ee=S.getOtherProps(m),je=c("p-virtualscroller",{"p-virtualscroller-inline":m.inline,"p-virtualscroller-both p-both-scroll":v,"p-virtualscroller-horizontal p-horizontal-scroll":h},m.className),Le=function(){if(!m.loaderDisabled&&m.showLoader&&N){var t=c("p-virtualscroller-loader",{"p-component-overlay":!m.loadingTemplate}),r=e.createElement("i",{className:"p-virtualscroller-loading-icon pi pi-spinner pi-spin"});return m.loadingTemplate?r=V.map((function(e,t){return xe(t,v&&{numCols:L.cols})})):m.loaderIconTemplate&&(r=i.getJSXElement(m.loaderIconTemplate,{className:"p-virtualscroller-loading-icon",element:r,props:m})),e.createElement("div",{className:t},r)}return null}(),Me=function(){var t=Te().map(Re),r=c("p-virtualscroller-content",{"p-virtualscroller-loading":N}),n=e.createElement("div",{ref:X,style:q.current,className:r},t);if(m.contentTemplate){var o={style:q.current,className:r,spacerStyle:G.current,contentRef:function(e){return X.current=i.getRefElement(e)},spacerRef:function(e){return _.current=i.getRefElement(e)},stickyRef:function(e){return B.current=i.getRefElement(e)},items:Te(),getItemOptions:function(e){return ze(e)},children:t,element:n,props:m,loading:N,getLoaderOptions:function(e,t){return Oe(e,t)},loadingTemplate:m.loadingTemplate,itemSize:m.itemSize,rows:N?m.loaderDisabled?V:[]:Te(),columns:m.columns&&v||h?N&&m.loaderDisabled?v?V[0]:V:m.columns.slice(v?b.cols:b,v?I.cols:I):m.columns,vertical:g,horizontal:h,both:v};return i.getJSXElement(m.contentTemplate,o)}return n}(),He=m.showSpacer?e.createElement("div",{ref:_,style:G.current,className:"p-virtualscroller-spacer"}):null;return e.createElement("div",u({ref:J,className:je,tabIndex:m.tabIndex,style:m.style},Ee,{onScroll:function(e){m.onScroll&&m.onScroll(e),m.delay?(Y.current&&clearTimeout(Y.current),se(b)&&(!N&&m.showLoader&&(we(e).isRangeChanged||m.step&&se(b))&&A(!0),Y.current=setTimeout((function(){Se(e),!N||!m.showLoader||m.lazy&&void 0!==m.loading||(A(!1),E(ie(b)))}),m.delay))):Se(e)}}),Me,He,Le)})));O.displayName="VirtualScroller";export{O as VirtualScroller};