11import classNames from 'classnames' ;
22import React , { PureComponent } from 'react' ;
33import PropTypes from 'prop-types' ;
4+ import elementResizeDetectorMaker from 'element-resize-detector' ;
45import styles from './index.styl' ;
56import TableTemplate from './TableTemplate' ;
67
@@ -38,7 +39,7 @@ class Table extends PureComponent {
3839 useFixedHeader : false
3940 } ;
4041
41- customEvent = null ;
42+ resizer = elementResizeDetectorMaker ( ) ;
4243
4344 mainTable = null ;
4445
@@ -66,41 +67,32 @@ class Table extends PureComponent {
6667 } ) ;
6768 }
6869 } ,
69- getTableHeight : ( ) => {
70+ getTableSize : ( ) => {
7071 const { maxHeight } = this . props ;
7172 const tableTopBorder = this . tableWrapper . style [ 'border-top-width' ] || window . getComputedStyle ( this . tableWrapper , null ) [ 'border-top-width' ] ;
7273 const tableBottomBorder = this . tableWrapper . style [ 'border-bottom-width' ] || window . getComputedStyle ( this . tableWrapper , null ) [ 'border-bottom-width' ] ;
7374 const headerHeight = this . title ? this . title . getBoundingClientRect ( ) . height : 0 ;
7475 const footerHeight = this . foot ? this . foot . getBoundingClientRect ( ) . height : 0 ;
7576 const tableHeight = maxHeight - headerHeight - footerHeight - parseInt ( tableTopBorder , 10 ) - parseInt ( tableBottomBorder , 10 ) ;
76- this . setState ( { tableHeight } ) ;
77- } ,
78- getTableWidth : ( ) => {
7977 const tableWidth = this . tableWrapper . getBoundingClientRect ( ) . width ;
80- if ( tableWidth !== this . state . tableWidth ) {
81- this . setState ( { tableWidth } ) ;
82- }
78+ this . setState ( {
79+ tableHeight,
80+ tableWidth
81+ } ) ;
8382 }
8483 } ;
8584
8685 componentDidMount ( ) {
87- const { getTableHeight, getTableWidth } = this . actions ;
88- window . addEventListener ( 'resize' , getTableHeight ) ;
89- if ( document . createEvent ) {
90- // IE version
91- this . customEvent = document . createEvent ( 'Event' ) ;
92- this . customEvent . initEvent ( 'checkWidth' , true , true ) ;
93- } else {
94- this . customEvent = new Event ( 'checkWidth' ) ;
95- }
96- window . addEventListener ( 'checkWidth' , getTableWidth ) ;
97- getTableHeight ( ) ;
86+ const { getTableSize } = this . actions ;
87+ this . resizer . listenTo ( this . tableWrapper , getTableSize ) ;
88+ window . addEventListener ( 'resize' , getTableSize ) ;
89+ getTableSize ( ) ;
9890 }
9991
10092 componentWillUnmount ( ) {
101- const { getTableHeight , getTableWidth } = this . actions ;
102- window . removeEventListener ( 'resize' , getTableHeight ) ;
103- window . removeEventListener ( 'checkWidth ' , getTableWidth ) ;
93+ const { getTableSize } = this . actions ;
94+ this . resizer . removeListener ( this . tableWrapper , getTableSize ) ;
95+ window . removeEventListener ( 'resize ' , getTableSize ) ;
10496 }
10597
10698 componentDidUpdate ( prevProps , prevState ) {
@@ -112,12 +104,8 @@ class Table extends PureComponent {
112104 if ( prevProps . data !== this . props . data ||
113105 prevProps . maxHeight !== this . props . maxHeight ||
114106 prevProps . expandedRowKeys !== this . props . expandedRowKeys ) {
115- const { getTableHeight } = this . actions ;
116- getTableHeight ( ) ;
117- // Issue: Page has no vertical scrollbar at begin, but appears the scrollbar after expanding A table,
118- // and B table width also displays horizontal scrollbar.
119- // Solution: Add this action to check other tables size in the same page.
120- window . dispatchEvent ( this . customEvent ) ;
107+ const { getTableSize } = this . actions ;
108+ getTableSize ( ) ;
121109 }
122110 }
123111
@@ -247,6 +235,7 @@ class Table extends PureComponent {
247235
248236 render ( ) {
249237 const {
238+ data,
250239 className,
251240 loading,
252241 bordered,
@@ -279,7 +268,7 @@ class Table extends PureComponent {
279268 { [ styles . tableBordered ] : bordered } ,
280269 { [ styles . tableExtendColumnWidth ] : ! averageColumnsWidth } ,
281270 { [ styles . tableFixedHeader ] : useFixedHeader } ,
282- { [ styles . tableNoData ] : ! props . data || props . data . length === 0 } ,
271+ { [ styles . tableNoData ] : ! data || data . length === 0 } ,
283272 { [ styles . tableHover ] : hoverable } ,
284273 { [ styles . tableSortable ] : sortable }
285274 ) }
0 commit comments