Skip to content

Commit 2d0e9fd

Browse files
authored
fix: Reset order column should sync the column width with fixed header (#408)
* fix: Use order to get table columns width instead of arr * add test case
1 parent dedc2d0 commit 2d0e9fd

File tree

7 files changed

+84
-24
lines changed

7 files changed

+84
-24
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
"prop-types": "^15.5.8",
5858
"raf": "^3.4.1",
5959
"rc-resize-observer": "^0.1.2",
60-
"rc-util": "^4.14.1",
60+
"rc-util": "^4.19.0",
6161
"react-lifecycles-compat": "^3.0.2",
6262
"shallowequal": "^1.1.0"
6363
},

src/Body/BodyRow.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ function BodyRow<RecordType extends { children?: RecordType[] }>(props: BodyRowP
188188
<ResizeObserver
189189
key={key}
190190
onResize={({ width }) => {
191-
onColumnResize(colIndex, width);
191+
onColumnResize(key, width);
192192
}}
193193
>
194194
{cellNode}

src/Header/HeaderRow.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ function HeaderRow<RecordType>({
7676
<ResizeObserver
7777
key={cellIndex}
7878
onResize={({ width }) => {
79-
onColumnResize(cell.colStart, width);
79+
onColumnResize(columnsKey[cell.colStart], width);
8080
}}
8181
>
8282
{cellNode}

src/Table.tsx

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ import BodyContext from './context/BodyContext';
5757
import Body from './Body';
5858
import useColumns from './hooks/useColumns';
5959
import { useFrameState, useTimeoutLock } from './hooks/useFrame';
60-
import { getPathValue, mergeObject, validateValue, newArr } from './utils/valueUtil';
60+
import { getPathValue, mergeObject, validateValue, getColumnsKey } from './utils/valueUtil';
6161
import ResizeContext from './context/ResizeContext';
6262
import useStickyOffsets from './hooks/useStickyOffsets';
6363
import ColGroup from './ColGroup';
@@ -318,7 +318,11 @@ function Table<RecordType extends DefaultRecordType>(props: TableProps<RecordTyp
318318
const scrollBodyRef = React.useRef<HTMLDivElement>();
319319
const [pingedLeft, setPingedLeft] = React.useState(false);
320320
const [pingedRight, setPingedRight] = React.useState(false);
321-
const [colWidths, updateColWidths] = useFrameState<number[]>(newArr(flattenColumns.length));
321+
const [colsWidths, updateColsWidths] = useFrameState(new Map<React.Key, number>());
322+
323+
// Convert map to number width
324+
const colsKeys = getColumnsKey(flattenColumns);
325+
const colWidths = colsKeys.map(columnKey => colsWidths.get(columnKey));
322326
const stickyOffsets = useStickyOffsets(colWidths, flattenColumns.length);
323327

324328
const fixHeader = hasData && scroll && validateValue(scroll.y);
@@ -342,11 +346,11 @@ function Table<RecordType extends DefaultRecordType>(props: TableProps<RecordTyp
342346
};
343347
}
344348

345-
function onColumnResize(colIndex: number, width: number) {
346-
updateColWidths((widths: number[]) => {
347-
const newWidth = widths.slice(0, flattenColumns.length);
348-
newWidth[colIndex] = width;
349-
return newWidth;
349+
function onColumnResize(columnKey: React.Key, width: number) {
350+
updateColsWidths(widths => {
351+
const newWidths = new Map(widths);
352+
newWidths.set(columnKey, width);
353+
return newWidths;
350354
});
351355
}
352356

src/context/ResizeContext.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import { createContext } from 'react';
1+
import * as React from 'react';
22

33
interface ResizeContextProps {
4-
onColumnResize: (colIndex: number, width: number) => void;
4+
onColumnResize: (columnKey: React.Key, width: number) => void;
55
}
66

7-
const ResizeContext = createContext<ResizeContextProps>(null);
7+
const ResizeContext = React.createContext<ResizeContextProps>(null);
88

99
export default ResizeContext;

src/utils/valueUtil.tsx

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -90,14 +90,3 @@ export function mergeObject<ReturnObject extends object>(
9090
export function validateValue<T>(val: T) {
9191
return val !== null && val !== undefined;
9292
}
93-
94-
/**
95-
* Create a packaged array to fast the v8 process speed
96-
*/
97-
export function newArr(len: number): number[] {
98-
const list: number[] = [];
99-
for (let i = 0; i < len; i += 1) {
100-
list[i] = null;
101-
}
102-
return list;
103-
}

tests/FixedHeader.spec.js

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import React from 'react';
2+
import { mount } from 'enzyme';
3+
import { act } from 'react-dom/test-utils';
4+
import Table from '../src';
5+
6+
describe('Table.FixedHeader', () => {
7+
it('switch column', () => {
8+
jest.useFakeTimers();
9+
const col1 = { dataIndex: 'light', width: 100 };
10+
const col2 = { dataIndex: 'bamboo', width: 200 };
11+
const wrapper = mount(
12+
<Table
13+
columns={[col1, col2]}
14+
data={[{ light: 'bamboo', bamboo: 'light', key: 1 }]}
15+
scroll={{ y: 10 }}
16+
/>,
17+
);
18+
19+
wrapper
20+
.find('ResizeObserver')
21+
.at(0)
22+
.props()
23+
.onResize({ width: 100 });
24+
wrapper
25+
.find('ResizeObserver')
26+
.at(1)
27+
.props()
28+
.onResize({ width: 200 });
29+
30+
act(() => {
31+
jest.runAllTimers();
32+
wrapper.update();
33+
});
34+
35+
expect(
36+
wrapper
37+
.find('colgroup col')
38+
.first()
39+
.props().style.width,
40+
).toEqual(100);
41+
expect(
42+
wrapper
43+
.find('colgroup col')
44+
.last()
45+
.props().style.width,
46+
).toEqual(200);
47+
48+
// Update columns
49+
wrapper.setProps({ columns: [col2, col1] });
50+
wrapper.update();
51+
52+
expect(
53+
wrapper
54+
.find('colgroup col')
55+
.first()
56+
.props().style.width,
57+
).toEqual(200);
58+
expect(
59+
wrapper
60+
.find('colgroup col')
61+
.last()
62+
.props().style.width,
63+
).toEqual(100);
64+
65+
jest.useRealTimers();
66+
});
67+
});

0 commit comments

Comments
 (0)