Skip to content

Commit 5452752

Browse files
author
Tina C Lin (RD-TW)
committed
[Third party] User element-resize-detector to listen table size whether was changed or not.
1 parent 2cd8cdb commit 5452752

File tree

2 files changed

+19
-29
lines changed

2 files changed

+19
-29
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
"@trendmicro/react-anchor": "^0.5.5",
5050
"trendmicro-ui": "^0.4.0",
5151
"classnames": "^2.2.5",
52+
"element-resize-detector": "^1.1.12",
5253
"lodash.get": "^4.4.2",
5354
"prop-types": "^15.5.8"
5455
},

src/Table.jsx

Lines changed: 18 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import classNames from 'classnames';
22
import React, { PureComponent } from 'react';
33
import PropTypes from 'prop-types';
4+
import elementResizeDetectorMaker from 'element-resize-detector';
45
import styles from './index.styl';
56
import 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

Comments
 (0)