Skip to content

feat: expose scroll event #2011

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

Merged
merged 8 commits into from
Apr 21, 2020
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:**
Expand Down
5 changes: 2 additions & 3 deletions src/DataGrid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ import {
RowExpandToggleEvent,
RowRendererProps,
RowsUpdateEvent,
ScrollPosition,
Copy link
Contributor

Choose a reason for hiding this comment

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

This type isn't used anywhere anymore, let's delete it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Nice catch, I didn't know this detail.

SelectRowEvent
} from './common/types';
import { CellNavigationMode, SortDirection } from './common/enums';
Expand Down Expand Up @@ -119,7 +118,7 @@ export interface DataGridProps<R, K extends keyof R, SR = unknown> {
/** Function called whenever a row is clicked */
onRowClick?: (rowIdx: number, row: R, column: CalculatedColumn<R, SR>) => void;
/** Called when the grid is scrolled */
onScroll?: (scrollPosition: ScrollPosition) => void;
onScroll?: (event: React.UIEvent<HTMLDivElement>) => void;
/** Called when a column is resized */
onColumnResize?: (idx: number, width: number) => void;
onRowExpandToggle?: (event: RowExpandToggleEvent) => void;
Expand Down Expand Up @@ -325,7 +324,7 @@ function DataGrid<R, K extends keyof R, SR>({
const { scrollTop, scrollLeft } = event.currentTarget;
setScrollTop(scrollTop);
setScrollLeft(scrollLeft);
onScroll?.({ scrollTop, scrollLeft });
onScroll?.(event);
}

const handleColumnResize = useCallback((column: CalculatedColumn<R, SR>, width: number) => {
Expand Down
5 changes: 0 additions & 5 deletions src/common/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,6 @@ export interface FilterRendererProps<TRow, TFilterValue = unknown, TSummaryRow =
onChange: (value: TFilterValue) => void;
}

export interface ScrollPosition {
scrollLeft: number;
scrollTop: number;
}

export interface RowGroupMetaData {
isGroup: boolean;
treeDepth: number;
Expand Down
63 changes: 48 additions & 15 deletions stories/demos/AllFeatures.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,27 @@ function createRows(numberOfRows: number): Row[] {
return rows;
}

function isAtBottom(event: React.UIEvent<HTMLDivElement>): boolean {
const target = event.target as HTMLDivElement;
return target.clientHeight + target.scrollTop === target.scrollHeight;
}

function loadMoreRows(newRowsCount: number, length: number): Promise<Row[]> {
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<string>());
const [isLoading, setIsLoading] = useState(false);
const gridRef = useRef<DataGridHandle>(null);

const columns = useMemo((): Column<Row>[] => [
Expand Down Expand Up @@ -190,26 +208,41 @@ export default function AllFeatures() {
}
}, []);

async function handleScroll(event: React.UIEvent<HTMLDivElement>) {
if (!isAtBottom(event)) return;

setIsLoading(true);

const newRows = await loadMoreRows(50, rows.length);

setRows([...rows, ...newRows]);
setIsLoading(false);
}

return (
<>
<Toolbar onAddRow={handleAddRow} numberOfRows={rows.length} />
<AutoSizer>
{({ height, width }) => (
<DataGrid
ref={gridRef}
columns={columns}
rows={rows}
rowKey="id"
onRowsUpdate={handleRowUpdate}
onRowClick={handleRowClick}
rowHeight={30}
width={width}
height={height - 40}
selectedRows={selectedRows}
onSelectedRowsChange={setSelectedRows}
enableCellCopyPaste
enableCellDragAndDrop
/>
<>
<DataGrid
ref={gridRef}
columns={columns}
rows={rows}
rowKey="id"
onRowsUpdate={handleRowUpdate}
onRowClick={handleRowClick}
rowHeight={30}
width={width}
height={height - 40}
selectedRows={selectedRows}
onScroll={handleScroll}
onSelectedRowsChange={setSelectedRows}
enableCellCopyPaste
enableCellDragAndDrop
/>
{isLoading && <div className="load-more-rows-tag" style={{ left: width - 230 }}>Loading more rows...</div>}
</>
)}
</AutoSizer>
</>
Expand Down
10 changes: 10 additions & 0 deletions stories/index.less
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}