Skip to content

Commit f5e5ff4

Browse files
committed
chore: reorganize code
1 parent 0dbe5f9 commit f5e5ff4

File tree

2 files changed

+33
-25
lines changed

2 files changed

+33
-25
lines changed

packages/pluggableWidgets/datagrid-web/src/model/hooks/useInfiniteControl.tsx

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
import { RefObject, UIEvent, useCallback, useLayoutEffect } from "react";
1+
import { RefObject, UIEvent, useCallback, useEffect, useLayoutEffect } from "react";
22
import { useOnScreen } from "@mendix/widget-plugin-hooks/useOnScreen";
33
import { useGridSizeStore } from "@mendix/datagrid-web/src/model/hooks/injection-hooks";
4-
5-
const offsetBottom = 30;
4+
import { VIRTUAL_SCROLLING_OFFSET } from "../stores/GridSize.store";
65

76
export function useInfiniteControl(): [trackBodyScrolling: ((e: any) => void) | undefined] {
87
const gridSizeStore = useGridSizeStore();
@@ -33,7 +32,7 @@ export function useInfiniteControl(): [trackBodyScrolling: ((e: any) => void) |
3332
* causing mismatch by 1 pixel point, thus, add magic number 2 as buffer.
3433
*/
3534
const bottom =
36-
Math.floor(target.scrollHeight - offsetBottom - target.scrollTop) <=
35+
Math.floor(target.scrollHeight - VIRTUAL_SCROLLING_OFFSET - target.scrollTop) <=
3736
Math.floor(target.clientHeight) + 2;
3837
if (bottom) {
3938
gridSizeStore.bumpPage();
@@ -42,18 +41,9 @@ export function useInfiniteControl(): [trackBodyScrolling: ((e: any) => void) |
4241
[gridSizeStore]
4342
);
4443

45-
const gridBody = gridSizeStore.gridBodyRef.current;
46-
const { hasVirtualScrolling, gridBodyHeight, hasMoreItems } = gridSizeStore;
47-
48-
const lockGridBodyHeight = useCallback((): void => {
49-
if (isVisible && hasVirtualScrolling && hasMoreItems && gridBodyHeight === undefined && gridBody) {
50-
gridSizeStore.setGridBodyHeight(gridBody.clientHeight - offsetBottom);
51-
}
52-
}, [isVisible, hasVirtualScrolling, hasMoreItems, gridBodyHeight, gridBody, gridSizeStore]);
53-
54-
useLayoutEffect(() => {
55-
setTimeout(() => lockGridBodyHeight(), 100);
56-
}, [lockGridBodyHeight]);
44+
useEffect(() => {
45+
setTimeout(() => isVisible && gridSizeStore.lockGridBodyHeight(), 100);
46+
}, [isVisible, gridSizeStore]);
5747

5848
useLayoutEffect(() => {
5949
const observeTarget = gridSizeStore.gridContainerRef.current;

packages/pluggableWidgets/datagrid-web/src/model/stores/GridSize.store.ts

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import { action, computed, makeAutoObservable, observable } from "mobx";
22
import { createRef } from "react";
33
import { PaginationController } from "../services/PaginationController";
44

5+
export const VIRTUAL_SCROLLING_OFFSET = 30;
6+
57
export class GridSizeStore {
68
gridContainerRef = createRef<HTMLDivElement>();
79
gridBodyRef = createRef<HTMLDivElement>();
@@ -29,7 +31,7 @@ export class GridSizeStore {
2931
setGridWidth: action,
3032

3133
gridBodyHeight: observable,
32-
setGridBodyHeight: action,
34+
lockGridBodyHeight: action,
3335

3436
columnSizes: observable,
3537
updateColumnSizes: action,
@@ -38,10 +40,6 @@ export class GridSizeStore {
3840
});
3941
}
4042

41-
get hasMoreItems(): boolean {
42-
return this.paging.hasMoreItems;
43-
}
44-
4543
get hasVirtualScrolling(): boolean {
4644
return this.paging.pagination === "virtualScrolling";
4745
}
@@ -64,11 +62,31 @@ export class GridSizeStore {
6462
this.gridWidth = size;
6563
}
6664

67-
setGridBodyHeight(size: number): void {
68-
this.gridBodyHeight = size;
69-
}
70-
7165
updateColumnSizes(sizes: number[]): void {
7266
this.columnSizes = sizes;
7367
}
68+
69+
lockGridBodyHeight(): void {
70+
if (!this.hasVirtualScrolling || !this.paging.hasMoreItems) {
71+
return;
72+
}
73+
const gridBody = this.gridBodyRef.current;
74+
if (!gridBody || this.gridBodyHeight !== undefined) {
75+
return;
76+
}
77+
78+
this.gridBodyHeight = gridBody.clientHeight - VIRTUAL_SCROLLING_OFFSET;
79+
}
7480
}
81+
82+
/**
83+
* const lockGridBodyHeight = useCallback((): void => {
84+
* if (isVisible && hasVirtualScrolling && hasMoreItems && gridBodyHeight === undefined && gridBody) {
85+
* gridSizeStore.setGridBodyHeight(gridBody.clientHeight - offsetBottom);
86+
* }
87+
* }, [isVisible, hasVirtualScrolling, hasMoreItems, gridBodyHeight, gridBody, gridSizeStore]);
88+
*
89+
* useLayoutEffect(() => {
90+
* setTimeout(() => lockGridBodyHeight(), 100);
91+
* }, [lockGridBodyHeight]);
92+
*/

0 commit comments

Comments
 (0)