Skip to content

Commit 9aef85d

Browse files
committed
test: update test case
1 parent 6a3c10a commit 9aef85d

File tree

6 files changed

+109
-108
lines changed

6 files changed

+109
-108
lines changed

docs/examples/expandedSticky.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ const Demo = () => {
3636
title: '手机号',
3737
dataIndex: 'a',
3838
width: 100,
39-
fixed: 'left',
39+
// fixed: 'left',
4040
onCell: (_, index) => {
4141
const { rowSpan = 1 } = rowSpanList[index];
4242
const props: React.TdHTMLAttributes<HTMLTableCellElement> = {};

src/Body/BodyRow.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export interface BodyRowProps<RecordType> {
2424
rowKey: React.Key;
2525

2626
// Expanded Row
27-
expandedRowInfo: {
27+
expandedRowInfo?: {
2828
colSpan: number;
2929
sticky: number;
3030
};
@@ -214,9 +214,9 @@ function BodyRow<RecordType extends { children?: readonly RecordType[] }>(
214214
prefixCls={prefixCls}
215215
component={RowComponent}
216216
cellComponent={cellComponent}
217-
colSpan={expandedRowInfo.colSpan}
217+
colSpan={expandedRowInfo ? expandedRowInfo.colSpan : flattenColumns.length}
218218
isEmpty={false}
219-
stickyOffset={expandedRowInfo.sticky}
219+
stickyOffset={expandedRowInfo?.sticky}
220220
>
221221
{expandContent}
222222
</ExpandedRow>

src/Body/index.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@ function Body<RecordType>(props: BodyProps<RecordType>) {
3434
emptyNode,
3535
classNames,
3636
styles,
37-
expandedRowOffset,
38-
fixedInfoList,
37+
expandedRowOffset = 0,
3938
colWidths,
4039
} = useContext(TableContext, [
4140
'prefixCls',

src/hooks/useColumns/index.tsx

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ function useColumns<RecordType>(
104104
expandIcon,
105105
rowExpandable,
106106
expandIconColumnIndex,
107+
expandedRowOffset = 0,
107108
direction,
108109
expandRowByClick,
109110
columnWidth,
@@ -128,6 +129,7 @@ function useColumns<RecordType>(
128129
clientWidth: number;
129130
fixed?: FixedType;
130131
scrollWidth?: number;
132+
expandedRowOffset?: number;
131133
},
132134
transformColumns: (columns: ColumnsType<RecordType>) => ColumnsType<RecordType>,
133135
): [
@@ -220,7 +222,16 @@ function useColumns<RecordType>(
220222
},
221223
};
222224

223-
return cloneColumns.map(col => (col === EXPAND_COLUMN ? expandColumn : col));
225+
return cloneColumns.map((col, index) => {
226+
const column = col === EXPAND_COLUMN ? expandColumn : col;
227+
if (index < expandedRowOffset) {
228+
return {
229+
...column,
230+
fixed: column.fixed || 'start',
231+
};
232+
}
233+
return column;
234+
});
224235
}
225236

226237
if (process.env.NODE_ENV !== 'production' && baseColumns.includes(EXPAND_COLUMN)) {
@@ -229,7 +240,7 @@ function useColumns<RecordType>(
229240

230241
return baseColumns.filter(col => col !== EXPAND_COLUMN);
231242
// eslint-disable-next-line react-hooks/exhaustive-deps
232-
}, [expandable, baseColumns, getRowKey, expandedKeys, expandIcon, direction]);
243+
}, [expandable, baseColumns, getRowKey, expandedKeys, expandIcon, direction, expandedRowOffset]);
233244

234245
// ========================= Transform ========================
235246
const mergedColumns = React.useMemo(() => {

tests/Expanded.spec.tsx

Lines changed: 0 additions & 100 deletions
This file was deleted.

tests/ExpandedOffset.spec.tsx

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
import React from 'react';
2+
import { spyElementPrototypes } from 'rc-util/lib/test/domHook';
3+
import { render, act } from '@testing-library/react';
4+
import { _rs } from '@rc-component/resize-observer';
5+
import Table, { type ColumnsType } from '../src';
6+
7+
async function triggerResize(ele: HTMLElement) {
8+
await act(async () => {
9+
_rs([{ target: ele }] as any);
10+
await Promise.resolve();
11+
});
12+
}
13+
14+
describe('Table.ExpandedOffset', () => {
15+
let domSpy: ReturnType<typeof spyElementPrototypes>;
16+
17+
beforeEach(() => {
18+
vi.useFakeTimers();
19+
});
20+
21+
beforeAll(() => {
22+
domSpy = spyElementPrototypes(HTMLElement, {
23+
offsetParent: {
24+
get: () => ({}),
25+
},
26+
offsetWidth: {
27+
get: () => 50,
28+
},
29+
});
30+
});
31+
32+
afterAll(() => {
33+
domSpy.mockRestore();
34+
});
35+
36+
afterEach(() => {
37+
vi.clearAllTimers();
38+
vi.useRealTimers();
39+
});
40+
41+
it('expanded + sticky', async () => {
42+
const columns: ColumnsType = [
43+
{
44+
title: 'a',
45+
// `fixed` will auto patch to fill the space
46+
// fixed: 'left',
47+
},
48+
Table.EXPAND_COLUMN,
49+
{ title: 'b' },
50+
{ title: 'c' },
51+
];
52+
53+
const data = [{ key: 'a' }];
54+
const { container } = render(
55+
<Table<Record<string, any>>
56+
columns={columns}
57+
data={data}
58+
sticky
59+
scroll={{ x: 1200 }}
60+
expandable={{
61+
expandedRowOffset: 1,
62+
defaultExpandAllRows: true,
63+
expandedRowRender: record => <div>{record.key}</div>,
64+
}}
65+
/>,
66+
);
67+
68+
await triggerResize(container.querySelector<HTMLElement>('.rc-table'));
69+
70+
act(() => {
71+
const coll = container.querySelector('.rc-table-resize-collection');
72+
if (coll) {
73+
triggerResize(coll as HTMLElement);
74+
}
75+
});
76+
77+
await act(async () => {
78+
vi.runAllTimers();
79+
await Promise.resolve();
80+
});
81+
82+
expect(container.querySelector('.rc-table-expanded-row .rc-table-cell')).toHaveAttribute(
83+
'colspan',
84+
'3',
85+
);
86+
expect(container.querySelector('.rc-table-expanded-row .rc-table-cell div')).toHaveStyle({
87+
position: 'sticky',
88+
left: '50px',
89+
});
90+
});
91+
});

0 commit comments

Comments
 (0)