Skip to content

Add scroll reach bottom api for lazy loading usage #1824

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions examples/demos/example13-all-features.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,17 @@ export default class extends React.Component {
minHeight={height}
selectedRows={this.state.selectedRows}
onSelectedRowsChange={this.onSelectedRowsChange}
onScroll={(scrollOption) => {
if (scrollOption.reachedBottom) {
this.setState(({ rows }) => {
const newRows = [];
for (let i = 0; i < 50; i++) {
newRows[i] = { ...this.createFakeRowObjectData(i), id: i + rows.length };
}
return { rows: [...rows, ...newRows] };
});
}
}}
/>
)}
</AutoSizer>
Expand Down
10 changes: 8 additions & 2 deletions packages/react-data-grid/src/Canvas.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react';

import { EventTypes } from './common/enums';
import { CalculatedColumn, CellMetaData, ColumnMetrics, InteractionMasksMetaData, Position, ScrollPosition } from './common/types';
import { CalculatedColumn, CellMetaData, ColumnMetrics, InteractionMasksMetaData, Position, ScrollOption } from './common/types';
import EventBus from './EventBus';
import InteractionMasks from './masks/InteractionMasks';
import { DataGridProps } from './DataGrid';
Expand Down Expand Up @@ -38,7 +38,7 @@ export interface CanvasProps<R, K extends keyof R> extends SharedDataGridProps<R
height: number;
eventBus: EventBus;
interactionMasksMetaData: InteractionMasksMetaData<R>;
onScroll(position: ScrollPosition): void;
onScroll(position: ScrollOption): void;
onCanvasKeydown?(e: React.KeyboardEvent<HTMLDivElement>): void;
onCanvasKeyup?(e: React.KeyboardEvent<HTMLDivElement>): void;
onRowSelectionChange(rowIdx: number, row: R, checked: boolean, isShiftClick: boolean): void;
Expand Down Expand Up @@ -110,6 +110,12 @@ export default function Canvas<R, K extends keyof R>({
}
}, [rowHeight, scrollToRowIndex]);

useEffect(() => {
if (scrollTop + clientHeight - rowHeight * rowsCount === 8) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

onScroll({ scrollLeft, scrollTop, reachedBottom: true });
}
}, [scrollTop, rowHeight, rowsCount, rowOverscanEndIdx, clientHeight, onScroll, scrollLeft]);

function handleScroll(e: React.UIEvent<HTMLDivElement>) {
const { scrollLeft, scrollTop } = e.currentTarget;
setScrollLeft(scrollLeft);
Expand Down
6 changes: 3 additions & 3 deletions packages/react-data-grid/src/DataGrid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import {
SubRowDetails,
SubRowOptions,
IRowRendererProps,
ScrollPosition
ScrollOption
} from './common/types';

export interface DataGridProps<R, K extends keyof R> {
Expand Down Expand Up @@ -116,7 +116,7 @@ export interface DataGridProps<R, K extends keyof R> {
/** The direction to sort the sortColumn*/
sortDirection?: DEFINE_SORT;
/** Called when the grid is scrolled */
onScroll?(scrollPosition: ScrollPosition): void;
onScroll?(scrollOption: ScrollOption): void;
/** Component used to render a draggable header cell */
draggableHeaderCell?: React.ComponentType<{ column: CalculatedColumn<R>; onHeaderDrop(): void }>;
getValidFilterValues?(columnKey: keyof R): unknown;
Expand Down Expand Up @@ -256,7 +256,7 @@ function DataGrid<R, K extends keyof R>({
}
}

function handleScroll(scrollPosition: ScrollPosition) {
function handleScroll(scrollPosition: ScrollOption) {
if (headerRef.current && scrollLeft.current !== scrollPosition.scrollLeft) {
scrollLeft.current = scrollPosition.scrollLeft;
headerRef.current.setScrollLeft(scrollPosition.scrollLeft);
Expand Down
4 changes: 4 additions & 0 deletions packages/react-data-grid/src/common/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,10 @@ export interface ScrollPosition {
scrollTop: number;
}

export interface ScrollOption extends ScrollPosition {
reachedBottom?: boolean;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We might want to revisit the API name, but I believe this should be a reasonable API.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or should I add a stand alone API like onReachedBottom to be explicitly?

}

export interface InteractionMasksMetaData<TRow> {
onCheckCellIsEditable?(e: CheckCellIsEditableEvent<TRow>): boolean;
onCellCopyPaste?(e: CellCopyPasteEvent<TRow>): void;
Expand Down
1 change: 1 addition & 0 deletions packages/react-data-grid/style/rdg-core.less
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,5 @@
.rdg-grid {
// min-height is here to show the horizontal scrollbar when there are no rows
min-height: 1px;
margin-bottom: 8px;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add small space between the bottom row and the bottom border, as the editable small square will increase the height size a little bit, so it's better to leave the margin in advance to avoid some randomly bug that a cell cannot be edited.

image

}