Skip to content

Commit c39b2d0

Browse files
authored
fix: Correct the size of resize observer (#404)
1 parent 05c6790 commit c39b2d0

File tree

3 files changed

+45
-28
lines changed

3 files changed

+45
-28
lines changed

src/Table.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,7 @@ function Table<RecordType extends DefaultRecordType>(props: TableProps<RecordTyp
313313
};
314314

315315
// ====================== Scroll ======================
316+
const fullTableRef = React.useRef<HTMLDivElement>();
316317
const scrollHeaderRef = React.useRef<HTMLDivElement>();
317318
const scrollBodyRef = React.useRef<HTMLDivElement>();
318319
const [pingedLeft, setPingedLeft] = React.useState(false);
@@ -388,7 +389,7 @@ function Table<RecordType extends DefaultRecordType>(props: TableProps<RecordTyp
388389

389390
const onFullTableResize = ({ width }) => {
390391
triggerOnScroll();
391-
setComponentWidth(width);
392+
setComponentWidth(fullTableRef.current ? fullTableRef.current.offsetWidth : width);
392393
};
393394

394395
// Sync scroll bar when init or `fixColumn` changed
@@ -586,6 +587,7 @@ function Table<RecordType extends DefaultRecordType>(props: TableProps<RecordTyp
586587
})}
587588
style={style}
588589
id={id}
590+
ref={fullTableRef}
589591
{...ariaProps}
590592
>
591593
{title && <Panel className={`${prefixCls}-title`}>{title(mergedData)}</Panel>}

tests/ExpandRow.spec.js

Lines changed: 41 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import React from 'react';
22
import { mount } from 'enzyme';
33
import { act } from 'react-dom/test-utils';
44
import { resetWarned } from 'rc-util/lib/warning';
5+
import { spyElementPrototype } from 'rc-util/lib/test/domHook';
56
import Table from '../src';
67

78
describe('Table.Expand', () => {
@@ -87,33 +88,47 @@ describe('Table.Expand', () => {
8788
expect(wrapper.render()).toMatchSnapshot();
8889
});
8990

90-
it('renders fixed column correctly', () => {
91-
const columns = [
92-
{ title: 'Name', dataIndex: 'name', key: 'name', fixed: true },
93-
{ title: 'Age', dataIndex: 'age', key: 'age' },
94-
{ title: 'Gender', dataIndex: 'gender', key: 'gender', fixed: 'right' },
95-
];
96-
const data = [
97-
{ key: 0, name: 'Lucy', age: 27, gender: 'F' },
98-
{ key: 1, name: 'Jack', age: 28, gender: 'M' },
99-
];
100-
const wrapper = mount(
101-
createTable({
102-
columns,
103-
data,
104-
scroll: { x: 903 },
105-
expandable: { expandedRowRender, defaultExpandAllRows: true },
106-
}),
107-
);
108-
act(() => {
109-
wrapper
110-
.find('ResizeObserver')
111-
.first()
112-
.props()
113-
.onResize({ width: 1128 });
91+
describe('renders fixed column correctly', () => {
92+
let domSpy;
93+
94+
beforeAll(() => {
95+
domSpy = spyElementPrototype(HTMLDivElement, 'offsetWidth', {
96+
get: () => 1128,
97+
});
98+
});
99+
100+
afterAll(() => {
101+
domSpy.mockRestore();
102+
});
103+
104+
it('work', () => {
105+
const columns = [
106+
{ title: 'Name', dataIndex: 'name', key: 'name', fixed: true },
107+
{ title: 'Age', dataIndex: 'age', key: 'age' },
108+
{ title: 'Gender', dataIndex: 'gender', key: 'gender', fixed: 'right' },
109+
];
110+
const data = [
111+
{ key: 0, name: 'Lucy', age: 27, gender: 'F' },
112+
{ key: 1, name: 'Jack', age: 28, gender: 'M' },
113+
];
114+
const wrapper = mount(
115+
createTable({
116+
columns,
117+
data,
118+
scroll: { x: 903 },
119+
expandable: { expandedRowRender, defaultExpandAllRows: true },
120+
}),
121+
);
122+
act(() => {
123+
wrapper
124+
.find('ResizeObserver')
125+
.first()
126+
.props()
127+
.onResize({ width: 1128 });
128+
});
129+
wrapper.update();
130+
expect(wrapper.render()).toMatchSnapshot();
114131
});
115-
wrapper.update();
116-
expect(wrapper.render()).toMatchSnapshot();
117132
});
118133

119134
it('renders expand icon to the specify column', () => {

tests/__snapshots__/ExpandRow.spec.js.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ exports[`Table.Expand childrenColumnName 1`] = `
101101
</div>
102102
`;
103103

104-
exports[`Table.Expand renders fixed column correctly 1`] = `
104+
exports[`Table.Expand renders fixed column correctly work 1`] = `
105105
<div
106106
class="rc-table rc-table-fixed-column rc-table-has-fix-left rc-table-has-fix-right"
107107
>

0 commit comments

Comments
 (0)