diff --git a/CHANGELOG.md b/CHANGELOG.md index 5ddc78c5d9..f3a5b4a6c1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -85,6 +85,7 @@ - Only visible headers cells are now rendered. [#1837](https://github.com/adazzle/react-data-grid/pull/1837) - ⚠️ the `rowKey` prop is now required for row selection. - ⚠️ `column.cellClass` does not affect header cells anymore. + - ⚠️ `onScroll` will directly pass the UIEvent rather than the scrollLeft and scrollRight only. ## `master` to `alpha` - **Added:** diff --git a/src/DataGrid.tsx b/src/DataGrid.tsx index c21d23a863..85038cfc73 100644 --- a/src/DataGrid.tsx +++ b/src/DataGrid.tsx @@ -37,7 +37,6 @@ import { RowExpandToggleEvent, RowRendererProps, RowsUpdateEvent, - ScrollPosition, SelectRowEvent } from './common/types'; import { CellNavigationMode, SortDirection } from './common/enums'; @@ -119,7 +118,7 @@ export interface DataGridProps { /** Function called whenever a row is clicked */ onRowClick?: (rowIdx: number, row: R, column: CalculatedColumn) => void; /** Called when the grid is scrolled */ - onScroll?: (scrollPosition: ScrollPosition) => void; + onScroll?: (event: React.UIEvent) => void; /** Called when a column is resized */ onColumnResize?: (idx: number, width: number) => void; onRowExpandToggle?: (event: RowExpandToggleEvent) => void; @@ -325,7 +324,7 @@ function DataGrid({ const { scrollTop, scrollLeft } = event.currentTarget; setScrollTop(scrollTop); setScrollLeft(scrollLeft); - onScroll?.({ scrollTop, scrollLeft }); + onScroll?.(event); } const handleColumnResize = useCallback((column: CalculatedColumn, width: number) => { diff --git a/src/common/types.ts b/src/common/types.ts index 7e4d2ce624..d5f7c26102 100644 --- a/src/common/types.ts +++ b/src/common/types.ts @@ -135,11 +135,6 @@ export interface FilterRendererProps void; } -export interface ScrollPosition { - scrollLeft: number; - scrollTop: number; -} - export interface RowGroupMetaData { isGroup: boolean; treeDepth: number; diff --git a/stories/demos/AllFeatures.tsx b/stories/demos/AllFeatures.tsx index dd650c6a8b..d099650fca 100644 --- a/stories/demos/AllFeatures.tsx +++ b/stories/demos/AllFeatures.tsx @@ -56,9 +56,27 @@ function createRows(numberOfRows: number): Row[] { return rows; } +function isAtBottom(event: React.UIEvent): boolean { + const target = event.target as HTMLDivElement; + return target.clientHeight + target.scrollTop === target.scrollHeight; +} + +function loadMoreRows(newRowsCount: number, length: number): Promise { + return new Promise(resolve => { + const newRows: Row[] = []; + + for (let i = 0; i < newRowsCount; i++) { + newRows[i] = createFakeRowObjectData(i + length); + } + + setTimeout(() => resolve(newRows), 1000); + }); +} + export default function AllFeatures() { const [rows, setRows] = useState(() => createRows(2000)); const [selectedRows, setSelectedRows] = useState(() => new Set()); + const [isLoading, setIsLoading] = useState(false); const gridRef = useRef(null); const columns = useMemo((): Column[] => [ @@ -190,26 +208,41 @@ export default function AllFeatures() { } }, []); + async function handleScroll(event: React.UIEvent) { + if (!isAtBottom(event)) return; + + setIsLoading(true); + + const newRows = await loadMoreRows(50, rows.length); + + setRows([...rows, ...newRows]); + setIsLoading(false); + } + return ( <> {({ height, width }) => ( - + <> + + {isLoading &&
Loading more rows...
} + )}
diff --git a/stories/index.less b/stories/index.less index 0627961d7e..2518039965 100644 --- a/stories/index.less +++ b/stories/index.less @@ -11,3 +11,13 @@ body, html { height: 100vh; padding: 8px; } + +.load-more-rows-tag { + line-height: 35px; + width: 180px; + position: relative; + top: -70px; + padding: 8px 16px; + background: rgba(0,0,0,0.6); + color: white; +}