Skip to content

Commit

Permalink
chore(scroll): always handle onScroll (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
amen-souissi committed May 17, 2021
1 parent a574300 commit df18b2d
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 12 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@decathlon/react-table",
"version": "1.15.2",
"version": "1.15.3",
"description": "React components for efficiently rendering large tabular data",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
4 changes: 2 additions & 2 deletions src/components/scroller.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,9 @@ class Scroller extends React.Component<IScrollerProps> {
const verticalScrollBar = hasVerticalScrollBar ? SCROLLBAR_SIZE : 0;
const horizontalScrollBar = hasHorizontalScrollBar ? SCROLLBAR_SIZE : 0;
// We calculate the maximum value of the scroll
this.scrollTopMax = virtualHeight - height + horizontalScrollBar;
this.scrollTopMax = virtualHeight - height - 5 + horizontalScrollBar;
// We calculate the maximum value of the scroll left
this.scrollLeftMax = virtualWidth - width + verticalScrollBar;
this.scrollLeftMax = virtualWidth - width - 5 + verticalScrollBar;
};

public getScrollDirections = (): ScrollDirection[] => {
Expand Down
25 changes: 18 additions & 7 deletions src/components/virtualizer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -354,28 +354,39 @@ class Virtualizer extends React.Component<IVirtualizerProps, IState> {
const newRowsState = hasVerticallyScrolled ? this.getVisibleRowsState(scrollTop) : null;
const newColumnsState = hasHorizontalyScrolled ? this.getVisibleColumnsState(scrollLeft) : null;

if (newRowsState || newColumnsState) {
// @ts-ignore
this.setState({ ...newRowsState, ...newColumnsState }, () => {
const handleOnScroll = (): void => {
if (onScroll || onHorizontallyScroll || onVerticallyScroll) {
const { visibleColumnIndexes, visibleRowIndexes } = this.state;
const columnsCursor = findFirstNotIncluded(visibleColumnIndexes, this.visibleFixedColumns);
const rowsCursor = findFirstNotIncluded(visibleRowIndexes, this.visibleFixedRows);
onScroll &&

if (onScroll) {
onScroll({
scrollValues,
newColumnsState,
newRowsState,
columnsCursor,
rowsCursor
});
onHorizontallyScroll &&
}
if (onHorizontallyScroll) {
onHorizontallyScroll({
scrollValues,
newColumnsState,
columnsCursor
});
onVerticallyScroll && onVerticallyScroll({ scrollValues, newRowsState, rowsCursor });
});
}
if (onVerticallyScroll) {
onVerticallyScroll({ scrollValues, newRowsState, rowsCursor });
}
}
};

if (newRowsState || newColumnsState) {
// @ts-ignore
this.setState({ ...newRowsState, ...newColumnsState }, handleOnScroll);
} else {
handleOnScroll();
}
};

Expand Down
4 changes: 2 additions & 2 deletions test/components/scroller.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ describe("Scroller component", () => {
const onScroll = jest.fn();
const wrapper = mount(<Scroller {...scrollerProps} onScroll={onScroll} />);
const scrollerInstance: Scroller = wrapper.instance() as Scroller;
const maxScrollTop = scrollerProps.virtualHeight - scrollerProps.height + SCROLLBAR_SIZE;
const maxScrollTop = scrollerProps.virtualHeight - scrollerProps.height - 5 + SCROLLBAR_SIZE;
scrollerInstance.scrollToTop(maxScrollTop);
// @ts-ignore scrollOrigin is private
expect(scrollerInstance.scrollOrigin).toBe("external");
Expand Down Expand Up @@ -156,7 +156,7 @@ describe("Scroller component", () => {
const onScroll = jest.fn();
const wrapper = mount(<Scroller {...scrollerProps} onScroll={onScroll} />);
const scrollerInstance: Scroller = wrapper.instance() as Scroller;
const maxScrollLeft = scrollerProps.virtualWidth - scrollerProps.width + SCROLLBAR_SIZE;
const maxScrollLeft = scrollerProps.virtualWidth - scrollerProps.width - 5 + SCROLLBAR_SIZE;
scrollerInstance.scrollToLeft(maxScrollLeft);
// @ts-ignore scrollOrigin is private
expect(scrollerInstance.scrollOrigin).toBe("external");
Expand Down

0 comments on commit df18b2d

Please sign in to comment.