github.com/minio/console@v1.4.1/web-app/src/screens/Console/Common/VirtualizedList/VirtualizedList.tsx (about) 1 // This file is part of MinIO Console Server 2 // Copyright (c) 2022 MinIO, Inc. 3 // 4 // This program is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Affero General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // This program is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Affero General Public License for more details. 13 // 14 // You should have received a copy of the GNU Affero General Public License 15 // along with this program. If not, see <http://www.gnu.org/licenses/>. 16 17 import React, { Fragment, ReactElement } from "react"; 18 import { FixedSizeList as List } from "react-window"; 19 import InfiniteLoader from "react-window-infinite-loader"; 20 import { AutoSizer } from "react-virtualized"; 21 22 interface IVirtualizedList { 23 rowRenderFunction: (index: number) => ReactElement | null; 24 totalItems: number; 25 defaultHeight?: number; 26 } 27 28 let itemStatusMap: any = {}; 29 const LOADING = 1; 30 const LOADED = 2; 31 32 const VirtualizedList = ({ 33 rowRenderFunction, 34 totalItems, 35 defaultHeight, 36 }: IVirtualizedList) => { 37 const isItemLoaded = (index: any) => !!itemStatusMap[index]; 38 39 const loadMoreItems = (startIndex: number, stopIndex: number) => { 40 for (let index = startIndex; index <= stopIndex; index++) { 41 itemStatusMap[index] = LOADING; 42 } 43 44 for (let index = startIndex; index <= stopIndex; index++) { 45 itemStatusMap[index] = LOADED; 46 } 47 }; 48 49 const RenderItemLine = ({ index, style }: any) => { 50 return <div style={style}>{rowRenderFunction(index)}</div>; 51 }; 52 53 return ( 54 <Fragment> 55 <InfiniteLoader 56 isItemLoaded={isItemLoaded} 57 loadMoreItems={loadMoreItems} 58 itemCount={totalItems} 59 > 60 {({ onItemsRendered, ref }) => ( 61 // @ts-ignore 62 <AutoSizer> 63 {({ width, height }) => { 64 return ( 65 <List 66 itemSize={defaultHeight || 220} 67 height={height} 68 itemCount={totalItems} 69 width={width} 70 ref={ref} 71 onItemsRendered={onItemsRendered} 72 > 73 {RenderItemLine} 74 </List> 75 ); 76 }} 77 </AutoSizer> 78 )} 79 </InfiniteLoader> 80 </Fragment> 81 ); 82 }; 83 84 export default VirtualizedList;