UNPKG

react-window-infinite-loader

Version:

Infinite loader utils inspired by `react-virtualized` but for use with `react-window`.

3 lines (2 loc) 1.21 kB
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const u=require("react");function g({isRowLoaded:r,minimumBatchSize:a,rowCount:o,startIndex:i,stopIndex:c}){const n=[];let d=-1,t=-1;for(let e=i;e<=c;e++)r(e)?t>=0&&(n.push({startIndex:d,stopIndex:t}),d=t=-1):(t=e,d<0&&(d=e));if(t>=0){const e=Math.min(Math.max(t,d+a-1),o-1);for(let s=t+1;s<=e&&!r(s);s++)t=s;n.push({startIndex:d,stopIndex:t})}if(n.length){const e=n[0];for(;e.stopIndex-e.startIndex+1<a&&e.startIndex>0;){const s=e.startIndex-1;if(!r(s))e.startIndex=s;else break}}return n}function b({isRowLoaded:r,loadMoreRows:a,minimumBatchSize:o=10,rowCount:i,threshold:c=15}){const n=u.useMemo(()=>new Set,[r,a]),d=u.useCallback(e=>r(e)?!0:n.has(e),[r,n]);return u.useCallback(({startIndex:e,stopIndex:s})=>{const f=g({isRowLoaded:d,minimumBatchSize:o,rowCount:i,startIndex:Math.max(0,e-c),stopIndex:Math.min(i-1,s+c)});for(let I=0;I<f.length;I+=2){const{startIndex:l,stopIndex:p}=f[I];for(let x=l;x<=p;x++)n.add(x);a(l,p)}},[d,a,o,n,i,c])}function S({children:r,...a}){const o=b(a);return r({onRowsRendered:o})}exports.InfiniteLoader=S;exports.useInfiniteLoader=b; //# sourceMappingURL=react-window-infinite-loader.cjs.map