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) 4.64 kB
import*as e from"react";import{localeOption as r}from"primereact/api";import{useMountEffect as t,useUpdateEffect as n,useUnmountEffect as l}from"primereact/hooks";import{ObjectUtils as o,classNames as a}from"primereact/utils";function u(){return u=Object.assign?Object.assign.bind():function(e){for(var r=1;r<arguments.length;r++){var t=arguments[r];for(var n in t)Object.prototype.hasOwnProperty.call(t,n)&&(e[n]=t[n])}return e},u.apply(this,arguments)}function c(e,r){(null==r||r>e.length)&&(r=e.length);for(var t=0,n=new Array(r);t<r;t++)n[t]=e[t];return n}function i(e){if(Array.isArray(e))return c(e)}function s(e){if("undefined"!=typeof Symbol&&null!=e[Symbol.iterator]||null!=e["@@iterator"])return Array.from(e)}function f(e,r){if(e){if("string"==typeof e)return c(e,r);var t=Object.prototype.toString.call(e).slice(8,-1);return"Object"===t&&e.constructor&&(t=e.constructor.name),"Map"===t||"Set"===t?Array.from(e):"Arguments"===t||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t)?c(e,r):void 0}}function m(){throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}function d(e){return i(e)||s(e)||f(e)||m()}function p(e){if(Array.isArray(e))return e}function y(e,r){var t=null==e?null:"undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(null!=t){var n,l,o,a,u=[],c=!0,i=!1;try{if(o=(t=t.call(e)).next,0===r){if(Object(t)!==t)return;c=!1}else for(;!(c=(n=o.call(t)).done)&&(u.push(n.value),u.length!==r);c=!0);}catch(e){i=!0,l=e}finally{try{if(!c&&null!=t.return&&(a=t.return(),Object(a)!==a))return}finally{if(i)throw l}}return u}}function v(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var h={defaultProps:{__TYPE:"DataScroller",id:null,value:null,rows:0,inline:!1,scrollHeight:null,loader:!1,buffer:.9,style:null,className:null,onLazyLoad:null,emptyMessage:null,itemTemplate:null,header:null,footer:null,lazy:!1,children:void 0},getProps:function(e){return o.getMergedProps(e,h.defaultProps)},getOtherProps:function(e){return o.getDiffProps(e,h.defaultProps)}},g=e.memo(e.forwardRef((function(c,i){var s,m,g=h.getProps(c),b=e.useState([]),w=(m=2,p(s=b)||y(s,m)||f(s,m)||v()),E=w[0],S=w[1],H=e.useRef(null),O=e.useRef(null),P=e.useRef(g.value),L=e.useRef([]),j=e.useRef(0),A=e.useRef(null),N=function(){if(g.lazy)g.onLazyLoad&&g.onLazyLoad(z()),j.current+=g.rows;else if(P.current){for(var e=j.current;e<j.current+g.rows&&!(e>=P.current.length);e++)L.current.push(P.current[e]);0!==P.current.length&&(j.current+=g.rows),S(d(L.current))}},T=function(){j.current=0,L.current=[],S(d(L.current)),N()},z=function(){return{first:j.current,rows:g.rows}},R=function(){A.current&&(g.inline&&O.current?O.current.removeEventListener("scroll",A.current):g.loader||window.removeEventListener("scroll",A.current)),A.current=null};t((function(){N(),g.loader||(g.inline?(A.current=function(){O.current.scrollTop>=O.current.scrollHeight*g.buffer-O.current.clientHeight&&N()},O.current.addEventListener("scroll",A.current)):(A.current=function(){var e=document.body,r=document.documentElement,t=window.pageYOffset||document.documentElement.scrollTop,n=r.clientHeight;t>=Math.max(e.scrollHeight,e.offsetHeight,n,r.scrollHeight,r.offsetHeight)*g.buffer-n&&N()},window.addEventListener("scroll",A.current)))})),n((function(){g.value&&(P.current=g.value,g.lazy||(j.current=0),L.current=[],g.lazy?(L.current=P.current,S(d(L.current))):N())}),[g.value]),n((function(){g.loader&&R()}),[g.loader]),l((function(){A.current&&R()})),e.useImperativeHandle(i,(function(){return{props:g,load:N,reset:T,getElement:function(){return H.current},getContent:function(){return O.current}}}));var I=function(r,t){var n=g.itemTemplate?g.itemTemplate(r):r;return e.createElement("li",{key:t+"_datascrollitem"},n)},M=function(){var t=o.getJSXElement(g.emptyMessage,g)||r("emptyMessage");return e.createElement("li",null,t)},x=h.getOtherProps(g),D=a("p-datascroller p-component",g.className,{"p-datascroller-inline":g.inline}),_=g.header?e.createElement("div",{className:"p-datascroller-header"},g.header):null,k=g.footer?e.createElement("div",{className:"p-datascroller-footer"},g.footer):null,C=function(){var r=o.isNotEmpty(E)?E.map(I):M();return e.createElement("div",{ref:O,className:"p-datascroller-content",style:{maxHeight:g.scrollHeight}},e.createElement("ul",{className:"p-datascroller-list"},r))}();return e.createElement("div",u({id:g.id,ref:H,className:D},x),_,C,k)})));g.displayName="DataScroller";export{g as DataScroller};