import { useState, useEffect } from 'react'; import { CellMeasurerCache, CellMeasurer } from 'react-virtualized'; import { AutoSizer, VirtualTableBody, WindowScroller } from '@patternfly/react-virtualized-extension'; import { Table, Thead, Tr, Th, Td, Caption, TableGridBreakpoint } from '@patternfly/react-table'; export const WindowScrollerExample = () => { const [scrollableElement, setScrollableElement] = useState(); useEffect(() => { const scrollableElement = document.getElementById('content-scrollable-2') as HTMLElement; setScrollableElement(scrollableElement); }, []); // this StringArray type is just needed because something in our documentation framework crashes when it encounters // a string[][] type type StringArray = string[]; const rows: StringArray[] = []; for (let i = 0; i < 100000; i++) { const cells: string[] = []; const num = Math.floor(Math.random() * Math.floor(2)) + 1; for (let j = 0; j < 5; j++) { const cellValue = i.toString() + ' Arma virumque cano Troiae qui primus ab oris. '.repeat(num); cells.push(cellValue); } rows.push(cells); } const columns = ['Repositories', 'Branches', 'Pull requests', 'Workspaces', 'Last Commit']; const scrollToIndex = -1; // can be used to programmatically set current index const measurementCache = new CellMeasurerCache({ fixedWidth: true, minHeight: 44, keyMapper: (rowIndex) => rowIndex }); const rowRenderer = ({ index: rowIndex, _isScrolling, key, style, parent }) => { const text = rows[rowIndex][0]; return ( {columns.map((col, index) => ( {text} ))} ); }; interface ScrollableContainerStyle { height: number; overflowX: 'auto'; overflowY: 'scroll'; scrollBehavior: 'smooth'; WebkitOverflowScrolling: 'touch'; position: 'relative'; } const scrollableContainerStyle: ScrollableContainerStyle = { height: 500 /* important note: the scrollable container should have some sort of fixed height, or it should be wrapped in container that is smaller than ReactVirtualized__VirtualGrid container and has overflow visible if using the Window Scroller. See WindowScroller.example.css */, overflowX: 'auto', overflowY: 'scroll', scrollBehavior: 'smooth', WebkitOverflowScrolling: 'touch', position: 'relative' }; return (
{columns.map((col, index) => ( ))}
WindowScroller allows scrolling of a parent container or the window instead of tbody. It also can be used to dynamically size the table to the size of the scroll element.
{col}
{({ height, isScrolling, registerChild, onChildScroll, scrollTop }) => ( {({ width }) => (
void}>
)}
)}
); };