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