@@ -3,12 +3,15 @@ import React, { useContext, useEffect } from 'react';
33import { useSelector } from 'react-redux' ;
44import { Tbody , Tr , Td , Div } from '../styled-components' ;
55import { Body as Renderers } from './Renderer' ;
6- import { getStyles , getRenderer } from '../utils' ;
7- import { MODIFY_DATA , SET_BODY_INNER_WIDTH } from '../actions' ;
6+ import { getStyles , getRenderer , getInitialVisibleColumns } from '../utils' ;
7+ import { MODIFY_DATA , SET_BODY_INNER_WIDTH , SET_TABLE_WIDTH , SET_VISIBLE_COLUMN_IDS , SET_COLUMN_WIDTHS } from '../actions' ;
88import { withScrollSpy } from '../hoc' ;
99import ConfigContext from '../context' ;
1010import { createSelector } from 'reselect' ;
1111
12+ const addElementResizeEventListener = require ( 'element-resize-event' ) ;
13+ const removeElementResizeEventListener = require ( 'element-resize-event' ) . unbind ;
14+
1215const renderCol = ( rowIndex , primaryKey , schema , styles , column , index ) => {
1316 const { textAlign, name, type } = column ;
1417 const ColRenderer = getRenderer ( column , Renderers ) ;
@@ -70,16 +73,27 @@ const Body = React.forwardRef(({ top: startTop = 0 }, ref) => {
7073
7174 const updateTableDimensions = ( ) => {
7275 action ( SET_BODY_INNER_WIDTH ) ( {
73- clientWidth : ref . current ? ref . current . clientWidth : minWidth ,
74- } ) ;
76+ clientWidth : ref . current ? ref . current . parentElement . clientWidth : minWidth ,
77+ } )
7578 } ;
7679
7780 useEffect ( ( ) => {
78- window . addEventListener ( 'resize' , updateTableDimensions ) ;
79- return ( ) => window . removeEventListener ( 'resize' , updateTableDimensions ) ;
80- } , [ updateTableDimensions ] ) ;
81+ action ( SET_VISIBLE_COLUMN_IDS ) ( { ids : getInitialVisibleColumns ( columns ) } ) ;
82+ action ( SET_TABLE_WIDTH ) ( { width : minWidth , widthAdjustment : 1 } ) ;
83+ action ( SET_COLUMN_WIDTHS ) ( columns . reduce ( ( acc , column ) => {
84+ if ( column . visible !== false ) {
85+ acc . push ( column . width ) ;
86+ }
87+ return acc ;
88+ } , [ ] ) ) ;
89+ } , [ ] ) ;
90+
91+ useEffect ( ( ) => {
92+ addElementResizeEventListener ( ref . current , updateTableDimensions ) ;
93+ return ( ) => removeElementResizeEventListener ( ref . current )
94+ } , [ ] ) ;
8195
82- useEffect ( ( ) => updateTableDimensions ( ) , [ ref . current ] ) ;
96+ useEffect ( ( ) => updateTableDimensions ( ) , [ ref . current . clientWidth ] ) ;
8397
8498 const totalHeight = rowHeight * itemCount ;
8599 const visibleHeight = minHeight || totalHeight ;
0 commit comments