Skip to content

Commit a75024f

Browse files
committed
fix(Table): get correct width after screen resize, close #2399
1 parent 522a19f commit a75024f

File tree

3 files changed

+48
-31
lines changed

3 files changed

+48
-31
lines changed

src/table/base/row.jsx

+6-26
Original file line numberDiff line numberDiff line change
@@ -106,24 +106,13 @@ export default class Row extends React.Component {
106106
const { lockType } = this.context;
107107
return columns.map((child, index) => {
108108
/* eslint-disable no-unused-vars, prefer-const */
109-
const {
110-
dataIndex,
111-
align,
112-
alignHeader,
113-
width,
114-
colSpan,
115-
style,
116-
cellStyle,
117-
__colIndex,
118-
...others
119-
} = child;
109+
const { dataIndex, align, alignHeader, width, colSpan, style, cellStyle, __colIndex, ...others } = child;
120110
const colIndex = '__colIndex' in child ? __colIndex : index;
121111
// colSpan should show in body td by the way of <Table.Column colSpan={2} />
122112
// tbody's cell merge should only by the way of <Table cellProps={} />
123113

124114
const value = fetchDataByPath(record, dataIndex);
125-
const attrs =
126-
getCellProps(rowIndex, colIndex, dataIndex, record) || {};
115+
const attrs = getCellProps(rowIndex, colIndex, dataIndex, record) || {};
127116

128117
if (this.context.notRenderCellIndex) {
129118
const matchCellIndex = this.context.notRenderCellIndex
@@ -134,25 +123,16 @@ export default class Row extends React.Component {
134123
return null;
135124
}
136125
}
137-
if (
138-
(attrs.colSpan && attrs.colSpan > 1) ||
139-
(attrs.rowSpan && attrs.rowSpan > 1)
140-
) {
141-
this._getNotRenderCellIndex(
142-
colIndex,
143-
rowIndex,
144-
attrs.colSpan || 1,
145-
attrs.rowSpan || 1
146-
);
126+
if ((attrs.colSpan && attrs.colSpan > 1) || (attrs.rowSpan && attrs.rowSpan > 1)) {
127+
this._getNotRenderCellIndex(colIndex, rowIndex, attrs.colSpan || 1, attrs.rowSpan || 1);
147128
}
148129

149130
const cellClass = attrs.className;
150131
const className = classnames({
151132
first: lockType !== 'right' && colIndex === 0,
152133
last:
153134
lockType !== 'left' &&
154-
(colIndex === columns.length - 1 ||
155-
colIndex + attrs.colSpan === columns.length), // 考虑合并单元格的情况
135+
(colIndex === columns.length - 1 || colIndex + attrs.colSpan === columns.length), // 考虑合并单元格的情况
156136
[child.className]: child.className,
157137
[cellClass]: cellClass,
158138
});
@@ -218,7 +198,7 @@ export default class Row extends React.Component {
218198
pure,
219199
locale,
220200
expandedIndexSimulate,
221-
tableOuterWidth,
201+
tableEl,
222202
rtl,
223203
wrapper,
224204
...others

src/table/expanded/row.jsx

+35-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React from 'react';
22
import PropTypes from 'prop-types';
3-
import { log } from '../../util';
3+
import { log, dom, events } from '../../util';
44
import Row from '../lock/row';
55

66
export default class ExpandedRow extends React.Component {
@@ -21,6 +21,34 @@ export default class ExpandedRow extends React.Component {
2121
lockType: PropTypes.oneOf(['left', 'right']),
2222
};
2323

24+
componentDidMount() {
25+
events.on(window, 'resize', this.setExpandedWidth);
26+
}
27+
28+
componentDidUpdate() {
29+
this.setExpandedWidth();
30+
}
31+
32+
componentWillUnmount() {
33+
events.off(window, 'resize', this.setExpandedWidth);
34+
}
35+
36+
getExpandedRow = (parentKey, ref) => {
37+
if (!this.expandedRowRef) {
38+
this.expandedRowRef = {};
39+
}
40+
this.expandedRowRef[parentKey] = ref;
41+
};
42+
43+
setExpandedWidth = () => {
44+
const { tableEl } = this.props;
45+
const totalWidth = +(tableEl && tableEl.clientWidth) - 2 || '100%';
46+
47+
Object.keys(this.expandedRowRef || {}).forEach(key => {
48+
dom.setStyle(this.expandedRowRef[key], { width: totalWidth });
49+
});
50+
};
51+
2452
renderExpandedRow(record, rowIndex) {
2553
const {
2654
expandedRowRender,
@@ -30,7 +58,6 @@ export default class ExpandedRow extends React.Component {
3058
expandedIndexSimulate,
3159
expandedRowWidthEquals2Table,
3260
} = this.context;
33-
const { tableOuterWidth } = this.props;
3461
const expandedIndex = expandedIndexSimulate ? (rowIndex - 1) / 2 : rowIndex;
3562

3663
const { columns, cellRef } = this.props;
@@ -76,7 +103,6 @@ export default class ExpandedRow extends React.Component {
76103

77104
const expandedRowStyle = {
78105
position: 'sticky',
79-
width: (tableOuterWidth || 0) - 1,
80106
left: 0,
81107
};
82108
// 暴露给用户的index
@@ -85,14 +111,19 @@ export default class ExpandedRow extends React.Component {
85111
content = (
86112
<div
87113
className={`${prefix}table-cell-wrapper`}
114+
ref={this.getExpandedRow.bind(this, record[primaryKey])}
88115
style={expandedRowWidthEquals2Table && expandedRowStyle}
89116
>
90117
{content}
91118
</div>
92119
);
93120
} else {
94121
content = expandedRowWidthEquals2Table ? (
95-
<div className={`${prefix}table-expanded-area`} style={expandedRowStyle}>
122+
<div
123+
className={`${prefix}table-expanded-area`}
124+
ref={this.getExpandedRow.bind(this, record[primaryKey])}
125+
style={expandedRowStyle}
126+
>
96127
{content}
97128
</div>
98129
) : (

src/table/new-lock.jsx

+7-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { findDOMNode } from 'react-dom';
33
import PropTypes from 'prop-types';
44
import classnames from 'classnames';
55
import shallowElementEquals from 'shallow-element-equals';
6-
import { log, obj, dom } from '../util';
6+
import { log, obj, dom, events } from '../util';
77
import LockRow from './lock/row';
88
import LockBody from './lock/body';
99
import LockHeader from './lock/header';
@@ -61,6 +61,8 @@ export default function stickyLock(BaseComponent) {
6161
this.updateOffsetArr();
6262
this.onLockBodyScroll(isEmpty ? { currentTarget: this.headerNode } : { currentTarget: this.bodyNode });
6363
this.forceUpdate();
64+
65+
events.on(window, 'resize', this.updateOffsetArr);
6466
}
6567

6668
shouldComponentUpdate(nextProps, nextState, nextContext) {
@@ -76,6 +78,10 @@ export default function stickyLock(BaseComponent) {
7678
this.updateOffsetArr();
7779
}
7880

81+
componentWillUnmount() {
82+
events.off(window, 'resize', this.updateOffsetArr);
83+
}
84+
7985
updateOffsetArr = () => {
8086
const { lockLeftChildren, lockRightChildren, originChildren } = this.splitChildren || {};
8187

0 commit comments

Comments
 (0)