1
1
import { observer } from "mobx-react" ;
2
2
import { onSnapshot } from "mobx-state-tree" ;
3
- import React , { useCallback , useEffect , useRef , useState } from "react" ;
3
+ import React , { useCallback , useEffect , useMemo , useRef , useState } from "react" ;
4
4
import ReactDataGrid from "react-data-grid" ;
5
5
import { TableContentModelType } from "../../../models/tools/table/table-content" ;
6
6
import { exportTableContentAsJson } from "../../../models/tools/table/table-export" ;
@@ -39,6 +39,7 @@ const TableToolComponent: React.FC<IToolTileProps> = observer(({
39
39
// Gather data from the model
40
40
const modelRef = useCurrent ( model ) ;
41
41
const getContent = useCallback ( ( ) => modelRef . current . content as TableContentModelType , [ modelRef ] ) ;
42
+ const content = useMemo ( ( ) => getContent ( ) , [ getContent ] ) ;
42
43
const metadata = getContent ( ) . metadata ;
43
44
44
45
// Basic operations based on the model
@@ -47,8 +48,7 @@ const TableToolComponent: React.FC<IToolTileProps> = observer(({
47
48
} = useModelDataSet ( model ) ;
48
49
49
50
// Set up user specified columns and function to measure a column
50
- // TODO The user specified columns should be moved out of react and into MST
51
- const { userColumnWidths, measureColumnWidth } = useMeasureColumnWidth ( ) ;
51
+ const { measureColumnWidth, resizeColumn, resizeColumnWidth } = useMeasureColumnWidth ( { content } ) ;
52
52
53
53
// Functions for determining the height of rows, including the header
54
54
// These require knowledge of the column widths
@@ -85,7 +85,7 @@ const TableToolComponent: React.FC<IToolTileProps> = observer(({
85
85
86
86
// The size of the title bar
87
87
const { titleCellWidth, getTitleHeight } =
88
- useTitleSize ( { readOnly, columns, measureColumnWidth, dataSet : dataSet . current } ) ;
88
+ useTitleSize ( { readOnly, columns, measureColumnWidth, dataSet : dataSet . current , rowChanges } ) ;
89
89
90
90
// A function to update the height of the tile based on the content size
91
91
const heightRef = useCurrent ( height ) ;
@@ -108,7 +108,7 @@ const TableToolComponent: React.FC<IToolTileProps> = observer(({
108
108
109
109
// A function to call when a column needs to change width
110
110
const { onColumnResize } = useColumnResize ( {
111
- columns, userColumnWidths , requestRowHeight, triggerRowChange
111
+ columns, content , requestRowHeight, resizeColumn , resizeColumnWidth , triggerRowChange
112
112
} ) ;
113
113
// Finishes setting up the controlsColumn with changeHandlers (which weren't defined when controlColumn was created)
114
114
useControlsColumn ( { controlsColumn, readOnly : ! ! readOnly , onAddColumn, onRemoveRows } ) ;
@@ -169,8 +169,8 @@ const TableToolComponent: React.FC<IToolTileProps> = observer(({
169
169
} ) ;
170
170
} , [ rows , rowHeight , headerHeight , getTitleHeight , getContent , modelRef , readOnly ] ) ;
171
171
const exportContentAsTileJson = useCallback ( ( ) => {
172
- return exportTableContentAsJson ( getContent ( ) . metadata , dataSet . current ) ;
173
- } , [ dataSet , getContent ] ) ;
172
+ return exportTableContentAsJson ( content . metadata , dataSet . current , content . columnWidth ) ;
173
+ } , [ dataSet , content ] ) ;
174
174
useToolApi ( { content : getContent ( ) , getTitle, getContentHeight, exportContentAsTileJson,
175
175
onRegisterToolApi, onUnregisterToolApi } ) ;
176
176
@@ -191,6 +191,13 @@ const TableToolComponent: React.FC<IToolTileProps> = observer(({
191
191
return ( ) => disposer ( ) ;
192
192
} ) ;
193
193
194
+ useEffect ( ( ) => {
195
+ const disposer = onSnapshot ( content . columnWidths , ( ) => {
196
+ triggerRowChange ( ) ;
197
+ } ) ;
198
+ return ( ) => disposer ( ) ;
199
+ } ) ;
200
+
194
201
const toolbarProps = useToolbarToolApi ( { id : model . id , enabled : ! readOnly , onRegisterToolApi, onUnregisterToolApi } ) ;
195
202
return (
196
203
< div className = "table-tool" >
0 commit comments