|
1 | 1 | import { useInfiniteControl } from "@mendix/widget-plugin-grid/components/InfiniteBody"; |
2 | 2 | import classNames from "classnames"; |
3 | | -import { Fragment, ReactElement, ReactNode, useCallback } from "react"; |
4 | | -import { LoadingTypeEnum } from "../../typings/DatagridProps"; |
5 | | -import { usePaginationService } from "../model/hooks/injection-hooks"; |
| 3 | +import { observer } from "mobx-react-lite"; |
| 4 | +import { Fragment, PropsWithChildren, ReactElement, ReactNode, RefObject, UIEventHandler, useCallback } from "react"; |
| 5 | +import { |
| 6 | + useDatagridConfig, |
| 7 | + useItemCount, |
| 8 | + useLoaderViewModel, |
| 9 | + usePaginationService, |
| 10 | + useVisibleColumnsCount |
| 11 | +} from "../model/hooks/injection-hooks"; |
6 | 12 | import { RowSkeletonLoader } from "./loader/RowSkeletonLoader"; |
7 | 13 | import { SpinnerLoader } from "./loader/SpinnerLoader"; |
8 | 14 |
|
9 | | -interface Props { |
10 | | - className?: string; |
11 | | - children?: ReactNode; |
12 | | - loadingType: LoadingTypeEnum; |
13 | | - isFirstLoad: boolean; |
14 | | - isFetchingNextBatch?: boolean; |
15 | | - columnsHidable: boolean; |
16 | | - columnsSize: number; |
17 | | - rowsSize: number; |
18 | | -} |
19 | | - |
20 | | -export function GridBody(props: Props): ReactElement { |
| 15 | +export function GridBody(props: PropsWithChildren): ReactElement { |
21 | 16 | const { children } = props; |
22 | | - |
23 | | - const paging = usePaginationService(); |
24 | | - const pageSize = paging.pageSize; |
25 | | - const setPage = useCallback((cb: (n: number) => number) => paging.setPage(cb), [paging]); |
26 | | - |
27 | | - const isInfinite = paging.pagination === "virtualScrolling"; |
28 | | - const [trackScrolling, bodySize, containerRef] = useInfiniteControl({ |
29 | | - hasMoreItems: paging.hasMoreItems, |
30 | | - isInfinite, |
31 | | - setPage |
32 | | - }); |
33 | | - |
34 | | - const content = (): ReactElement => { |
35 | | - if (props.isFirstLoad) { |
36 | | - return <Loader {...props} rowsSize={props.rowsSize > 0 ? props.rowsSize : pageSize} />; |
37 | | - } |
38 | | - return ( |
39 | | - <Fragment> |
40 | | - {children} |
41 | | - {props.isFetchingNextBatch && <Loader {...props} rowsSize={pageSize} useBorderTop={false} />} |
42 | | - </Fragment> |
43 | | - ); |
44 | | - }; |
| 17 | + const { bodySize, containerRef, isInfinite, handleScroll } = useBodyScroll(); |
45 | 18 |
|
46 | 19 | return ( |
47 | 20 | <div |
48 | | - className={classNames( |
49 | | - "widget-datagrid-grid-body table-content", |
50 | | - { "infinite-loading": isInfinite }, |
51 | | - props.className |
52 | | - )} |
| 21 | + className={classNames("widget-datagrid-grid-body table-content", { "infinite-loading": isInfinite })} |
53 | 22 | style={isInfinite && bodySize > 0 ? { maxHeight: `${bodySize}px` } : {}} |
54 | 23 | role="rowgroup" |
55 | 24 | ref={containerRef} |
56 | | - onScroll={isInfinite ? trackScrolling : undefined} |
| 25 | + onScroll={handleScroll} |
57 | 26 | > |
58 | | - {content()} |
| 27 | + <ContentGuard>{children}</ContentGuard> |
59 | 28 | </div> |
60 | 29 | ); |
61 | 30 | } |
62 | 31 |
|
63 | | -interface LoaderProps { |
64 | | - loadingType: LoadingTypeEnum; |
65 | | - columnsHidable: boolean; |
66 | | - columnsSize: number; |
67 | | - rowsSize: number; |
68 | | - useBorderTop?: boolean; |
69 | | -} |
| 32 | +const ContentGuard = observer(function ContentGuard(props: PropsWithChildren): ReactNode { |
| 33 | + const loaderVM = useLoaderViewModel(); |
| 34 | + const { pageSize } = usePaginationService(); |
| 35 | + const config = useDatagridConfig(); |
| 36 | + const columnsCount = useVisibleColumnsCount().get(); |
| 37 | + const itemCount = useItemCount().get(); |
70 | 38 |
|
71 | | -function Loader(props: LoaderProps): ReactElement { |
72 | | - if (props.loadingType === "spinner") { |
| 39 | + if (loaderVM.isFirstLoad && config.loadingType === "spinner") { |
| 40 | + return <Spinner />; |
| 41 | + } |
| 42 | + |
| 43 | + if (loaderVM.isFirstLoad) { |
73 | 44 | return ( |
74 | | - <div className="widget-datagrid-loader-container"> |
75 | | - <SpinnerLoader withMargins size="large" /> |
76 | | - </div> |
| 45 | + <RowSkeletonLoader |
| 46 | + columnsHidable={config.columnsHidable} |
| 47 | + columnsSize={columnsCount} |
| 48 | + pageSize={itemCount > 0 ? itemCount : pageSize} |
| 49 | + useBorderTop |
| 50 | + /> |
77 | 51 | ); |
78 | 52 | } |
79 | 53 |
|
80 | 54 | return ( |
81 | | - <RowSkeletonLoader |
82 | | - columnsHidable={props.columnsHidable} |
83 | | - columnsSize={props.columnsSize} |
84 | | - pageSize={props.rowsSize} |
85 | | - useBorderTop={props.useBorderTop} |
86 | | - /> |
| 55 | + <Fragment> |
| 56 | + {props.children} |
| 57 | + {(() => { |
| 58 | + if (loaderVM.isFetchingNextBatch && config.loadingType === "spinner") { |
| 59 | + return <Spinner />; |
| 60 | + } |
| 61 | + |
| 62 | + if (loaderVM.isFetchingNextBatch) { |
| 63 | + return ( |
| 64 | + <RowSkeletonLoader |
| 65 | + columnsHidable={config.columnsHidable} |
| 66 | + columnsSize={columnsCount} |
| 67 | + pageSize={pageSize} |
| 68 | + useBorderTop={false} |
| 69 | + /> |
| 70 | + ); |
| 71 | + } |
| 72 | + |
| 73 | + return null; |
| 74 | + })()} |
| 75 | + </Fragment> |
87 | 76 | ); |
| 77 | +}); |
| 78 | + |
| 79 | +function useBodyScroll(): { |
| 80 | + handleScroll: UIEventHandler<HTMLDivElement> | undefined; |
| 81 | + bodySize: number; |
| 82 | + containerRef: RefObject<HTMLDivElement | null>; |
| 83 | + isInfinite: boolean; |
| 84 | +} { |
| 85 | + const paging = usePaginationService(); |
| 86 | + const setPage = useCallback((cb: (n: number) => number) => paging.setPage(cb), [paging]); |
| 87 | + |
| 88 | + const isInfinite = paging.pagination === "virtualScrolling"; |
| 89 | + const [trackScrolling, bodySize, containerRef] = useInfiniteControl({ |
| 90 | + hasMoreItems: paging.hasMoreItems, |
| 91 | + isInfinite, |
| 92 | + setPage |
| 93 | + }); |
| 94 | + |
| 95 | + return { |
| 96 | + handleScroll: isInfinite ? trackScrolling : undefined, |
| 97 | + bodySize, |
| 98 | + containerRef, |
| 99 | + isInfinite |
| 100 | + }; |
88 | 101 | } |
| 102 | + |
| 103 | +const Spinner = (): ReactNode => ( |
| 104 | + <div className="widget-datagrid-loader-container"> |
| 105 | + <SpinnerLoader withMargins size="large" /> |
| 106 | + </div> |
| 107 | +); |
0 commit comments