11import { useClickActionHelper } from "@mendix/widget-plugin-grid/helpers/ClickActionHelper" ;
22import { useFocusTargetController } from "@mendix/widget-plugin-grid/keyboard-navigation/useFocusTargetController" ;
33import { useSelectionHelper } from "@mendix/widget-plugin-grid/selection" ;
4- import { useConst } from "@mendix/widget-plugin-mobx-kit/react/useConst" ;
54import { generateUUID } from "@mendix/widget-plugin-platform/framework/generate-uuid" ;
5+ import { ContainerProvider } from "brandi-react" ;
66import { observer } from "mobx-react-lite" ;
77import { ReactElement , ReactNode , useCallback , useMemo } from "react" ;
88import { DatagridContainerProps } from "../typings/DatagridProps" ;
99import { Cell } from "./components/Cell" ;
1010import { Widget } from "./components/Widget" ;
1111import { WidgetHeaderContext } from "./components/WidgetHeaderContext" ;
12- import { ProgressStore } from "./features/data-export/ProgressStore" ;
12+ import { useDatagridDepsContainer } from "./Datagrid.depsContainer" ;
13+ import {
14+ useColumnsStore ,
15+ useExportProgressService ,
16+ useLoaderViewModel ,
17+ useMainGate ,
18+ usePaginationService
19+ } from "./deps-hooks" ;
1320import { useDataExport } from "./features/data-export/useDataExport" ;
1421import { useCellEventsController } from "./features/row-interaction/CellEventsController" ;
1522import { useCheckboxEventsController } from "./features/row-interaction/CheckboxEventsController" ;
16- import { DatagridContext } from "./helpers/root-context" ;
1723import { useSelectActionHelper } from "./helpers/SelectActionHelper" ;
18- import { IColumnGroupStore } from "./helpers/state/ColumnGroupStore" ;
19- import { RootGridStore } from "./helpers/state/RootGridStore" ;
20- import { useRootStore } from "./helpers/state/useRootStore" ;
2124import { useDataGridJSActions } from "./helpers/useDataGridJSActions" ;
2225
23- interface Props extends DatagridContainerProps {
24- columnsStore : IColumnGroupStore ;
25- rootStore : RootGridStore ;
26- progressStore : ProgressStore ;
27- }
28-
29- const Container = observer ( ( props : Props ) : ReactElement => {
30- const { columnsStore, rootStore } = props ;
31- const { paginationCtrl } = rootStore ;
26+ const DatagridRoot = observer ( ( props : DatagridContainerProps ) : ReactElement => {
27+ const gate = useMainGate ( ) ;
28+ const columnsStore = useColumnsStore ( ) ;
29+ const paginationService = usePaginationService ( ) ;
30+ const exportProgress = useExportProgressService ( ) ;
31+ const loaderVM = useLoaderViewModel ( ) ;
32+ const items = gate . props . datasource . items ?? [ ] ;
3233
33- const items = props . datasource . items ?? [ ] ;
34-
35- const [ exportProgress , abortExport ] = useDataExport ( props , props . columnsStore , props . progressStore ) ;
34+ const [ , abortExport ] = useDataExport ( props , columnsStore , exportProgress ) ;
3635
3736 const selectionHelper = useSelectionHelper (
3837 props . itemSelection ,
@@ -48,7 +47,7 @@ const Container = observer((props: Props): ReactElement => {
4847 onClick : props . onClick
4948 } ) ;
5049
51- useDataGridJSActions ( rootStore , selectActionHelper ) ;
50+ useDataGridJSActions ( selectActionHelper ) ;
5251
5352 const visibleColumnsCount = selectActionHelper . showCheckboxColumn
5453 ? columnsStore . visibleColumns . length + 1
@@ -64,97 +63,79 @@ const Container = observer((props: Props): ReactElement => {
6463
6564 const checkboxEventsController = useCheckboxEventsController ( selectActionHelper , focusController ) ;
6665
67- const ctx = useConst ( ( ) => {
68- rootStore . basicData . setSelectionHelper ( selectionHelper ) ;
69- return {
70- basicData : rootStore . basicData ,
71- selectionHelper,
72- selectActionHelper,
73- cellEventsController,
74- checkboxEventsController,
75- focusController,
76- selectionCountStore : rootStore . selectionCountStore
77- } ;
78- } ) ;
79-
8066 return (
81- < DatagridContext . Provider value = { ctx } >
82- < Widget
83- className = { props . class }
84- CellComponent = { Cell }
85- columnsDraggable = { props . columnsDraggable }
86- columnsFilterable = { props . columnsFilterable }
87- columnsHidable = { props . columnsHidable }
88- columnsResizable = { props . columnsResizable }
89- columnsSortable = { props . columnsSortable }
90- data = { items }
91- emptyPlaceholderRenderer = { useCallback (
92- ( renderWrapper : ( children : ReactNode ) => ReactElement ) =>
93- props . showEmptyPlaceholder === "custom" ? renderWrapper ( props . emptyPlaceholder ) : < div /> ,
94- [ props . emptyPlaceholder , props . showEmptyPlaceholder ]
95- ) }
96- filterRenderer = { useCallback (
97- ( renderWrapper , columnIndex ) => {
98- const columnFilter = columnsStore . columnFilters [ columnIndex ] ;
99- return renderWrapper ( columnFilter . renderFilterWidgets ( ) ) ;
100- } ,
101- [ columnsStore . columnFilters ]
102- ) }
103- headerTitle = { props . filterSectionTitle ?. value }
104- headerContent = {
105- props . filtersPlaceholder && (
106- < WidgetHeaderContext selectionHelper = { selectionHelper } rootStore = { rootStore } >
107- { props . filtersPlaceholder }
108- </ WidgetHeaderContext >
109- )
110- }
111- hasMoreItems = { props . datasource . hasMoreItems ?? false }
112- headerWrapperRenderer = { useCallback ( ( _columnIndex : number , header : ReactElement ) => header , [ ] ) }
113- id = { useMemo ( ( ) => `DataGrid${ generateUUID ( ) } ` , [ ] ) }
114- numberOfItems = { props . datasource . totalCount }
115- onExportCancel = { abortExport }
116- page = { paginationCtrl . currentPage }
117- pageSize = { props . pageSize }
118- paginationType = { props . pagination }
119- loadMoreButtonCaption = { props . loadMoreButtonCaption ?. value }
120- selectionCountPosition = { props . selectionCountPosition }
121- paging = { paginationCtrl . showPagination }
122- pagingPosition = { props . pagingPosition }
123- showPagingButtons = { props . showPagingButtons }
124- rowClass = { useCallback ( ( value : any ) => props . rowClass ?. get ( value ) ?. value ?? "" , [ props . rowClass ] ) }
125- setPage = { paginationCtrl . setPage }
126- styles = { props . style }
127- exporting = { exportProgress . exporting }
128- processedRows = { exportProgress . loaded }
129- visibleColumns = { columnsStore . visibleColumns }
130- availableColumns = { columnsStore . availableColumns }
131- setIsResizing = { ( status : boolean ) => columnsStore . setIsResizing ( status ) }
132- columnsSwap = { ( moved , [ target , placement ] ) => columnsStore . swapColumns ( moved , [ target , placement ] ) }
133- selectActionHelper = { selectActionHelper }
134- cellEventsController = { cellEventsController }
135- checkboxEventsController = { checkboxEventsController }
136- focusController = { focusController }
137- isFirstLoad = { rootStore . loaderCtrl . isFirstLoad }
138- isFetchingNextBatch = { rootStore . loaderCtrl . isFetchingNextBatch }
139- showRefreshIndicator = { rootStore . loaderCtrl . showRefreshIndicator }
140- loadingType = { props . loadingType }
141- columnsLoading = { ! columnsStore . loaded }
142- />
143- </ DatagridContext . Provider >
67+ < Widget
68+ className = { props . class }
69+ CellComponent = { Cell }
70+ columnsDraggable = { props . columnsDraggable }
71+ columnsFilterable = { props . columnsFilterable }
72+ columnsHidable = { props . columnsHidable }
73+ columnsResizable = { props . columnsResizable }
74+ columnsSortable = { props . columnsSortable }
75+ data = { items }
76+ emptyPlaceholderRenderer = { useCallback (
77+ ( renderWrapper : ( children : ReactNode ) => ReactElement ) =>
78+ props . showEmptyPlaceholder === "custom" ? renderWrapper ( props . emptyPlaceholder ) : < div /> ,
79+ [ props . emptyPlaceholder , props . showEmptyPlaceholder ]
80+ ) }
81+ filterRenderer = { useCallback (
82+ ( renderWrapper , columnIndex ) => {
83+ const columnFilter = columnsStore . columnFilters [ columnIndex ] ;
84+ return renderWrapper ( columnFilter . renderFilterWidgets ( ) ) ;
85+ } ,
86+ [ columnsStore . columnFilters ]
87+ ) }
88+ headerTitle = { props . filterSectionTitle ?. value }
89+ headerContent = {
90+ props . filtersPlaceholder && (
91+ < WidgetHeaderContext selectionHelper = { selectionHelper } >
92+ { props . filtersPlaceholder }
93+ </ WidgetHeaderContext >
94+ )
95+ }
96+ hasMoreItems = { props . datasource . hasMoreItems ?? false }
97+ headerWrapperRenderer = { useCallback ( ( _columnIndex : number , header : ReactElement ) => header , [ ] ) }
98+ id = { useMemo ( ( ) => `DataGrid${ generateUUID ( ) } ` , [ ] ) }
99+ numberOfItems = { props . datasource . totalCount }
100+ onExportCancel = { abortExport }
101+ page = { paginationService . currentPage }
102+ pageSize = { props . pageSize }
103+ paginationType = { props . pagination }
104+ loadMoreButtonCaption = { props . loadMoreButtonCaption ?. value }
105+ selectionCountPosition = { props . selectionCountPosition }
106+ paging = { paginationService . showPagination }
107+ pagingPosition = { props . pagingPosition }
108+ showPagingButtons = { props . showPagingButtons }
109+ rowClass = { useCallback ( ( value : any ) => props . rowClass ?. get ( value ) ?. value ?? "" , [ props . rowClass ] ) }
110+ setPage = { paginationService . setPage }
111+ styles = { props . style }
112+ exporting = { exportProgress . exporting }
113+ processedRows = { exportProgress . loaded }
114+ visibleColumns = { columnsStore . visibleColumns }
115+ availableColumns = { columnsStore . availableColumns }
116+ setIsResizing = { ( status : boolean ) => columnsStore . setIsResizing ( status ) }
117+ columnsSwap = { ( moved , [ target , placement ] ) => columnsStore . swapColumns ( moved , [ target , placement ] ) }
118+ selectActionHelper = { selectActionHelper }
119+ cellEventsController = { cellEventsController }
120+ checkboxEventsController = { checkboxEventsController }
121+ focusController = { focusController }
122+ isFirstLoad = { loaderVM . isFirstLoad }
123+ isFetchingNextBatch = { loaderVM . isFetchingNextBatch }
124+ showRefreshIndicator = { loaderVM . showRefreshIndicator }
125+ loadingType = { props . loadingType }
126+ columnsLoading = { ! columnsStore . loaded }
127+ />
144128 ) ;
145129} ) ;
146130
147- Container . displayName = "DatagridComponent" ;
131+ DatagridRoot . displayName = "DatagridComponent" ;
148132
149133export default function Datagrid ( props : DatagridContainerProps ) : ReactElement | null {
150- const rootStore = useRootStore ( props ) ;
134+ const container = useDatagridDepsContainer ( props ) ;
151135
152136 return (
153- < Container
154- { ...props }
155- rootStore = { rootStore }
156- columnsStore = { rootStore . columnsStore }
157- progressStore = { rootStore . exportProgressCtrl }
158- />
137+ < ContainerProvider container = { container } >
138+ < DatagridRoot { ...props } />
139+ </ ContainerProvider >
159140 ) ;
160141}
0 commit comments