Skip to content

Commit b550bab

Browse files
authored
expandedRowRender 不生效 (#1070)
* 🐞 fix(expandedRowClassName): #400 * 🧪 test(expandedRowClassName): 测试用例
1 parent 25da823 commit b550bab

File tree

5 files changed

+99
-2
lines changed

5 files changed

+99
-2
lines changed

docs/demo/expandedRowClassName.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
title: expandedRowClassName
3+
nav:
4+
title: Demo
5+
path: /demo
6+
---
7+
8+
<code src="../examples/expandedRowClassName.tsx"></code>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.tesExpandedRowClassName td {
2+
background-color: red !important;
3+
color: #fff;
4+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import React from 'react';
2+
import Table from 'rc-table';
3+
4+
import styles from './expandedRowClassName.module.less';
5+
6+
const columns = [
7+
{
8+
title: 'Name',
9+
dataIndex: 'name',
10+
key: 'name',
11+
},
12+
{
13+
title: 'Age',
14+
dataIndex: 'age',
15+
key: 'age',
16+
},
17+
{
18+
title: 'Address',
19+
dataIndex: 'address',
20+
key: 'address',
21+
},
22+
];
23+
24+
const data = [
25+
{
26+
name: 'John',
27+
age: '25',
28+
address: '1 A Street',
29+
children: [
30+
{ name: 'C-John', age: '31', address: '1 A Stree2t' },
31+
{
32+
name: 'C-Fred',
33+
age: '532',
34+
address: '2 B Str1eet',
35+
children: [
36+
{ name: 'D-John', age: '31', address: '1 A Stree2t' },
37+
{ name: 'D-Fred', age: '532', address: '2 B Str1eet' },
38+
{ name: 'D-Anne', age: '43217', address: '3 C S3treet' },
39+
],
40+
},
41+
{ name: 'C-Anne', age: '43217', address: '3 C S3treet' },
42+
],
43+
},
44+
{ name: 'Fred', age: '38', address: '2 B Street' },
45+
{ name: 'Anne', age: '47', address: '3 C Street' },
46+
];
47+
48+
const Demo = () => (
49+
<div>
50+
<h2>Table expandedRowClassName</h2>
51+
<Table
52+
rowKey={'name'}
53+
columns={columns}
54+
data={data}
55+
expandable={{
56+
expandedRowClassName: () => styles.tesExpandedRowClassName,
57+
}}
58+
/>
59+
</div>
60+
);
61+
62+
export default Demo;

src/Body/BodyRow.tsx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,11 @@ function BodyRow<RecordType extends { children?: readonly RecordType[] }>(
124124
devRenderTimes(props);
125125
}
126126

127+
// 若没有 expandedRowRender 参数, 将使用 baseRowNode 渲染 Children
128+
// 此时如果 level > 1 则说明是 expandedRow, 一样需要附加 computedExpandedRowClassName
129+
const computedExpandedRowClassName =
130+
expandedRowClassName && expandedRowClassName(record, index, indent);
131+
127132
// ======================== Base tr row ========================
128133
const baseRowNode = (
129134
<RowComponent
@@ -134,6 +139,7 @@ function BodyRow<RecordType extends { children?: readonly RecordType[] }>(
134139
`${prefixCls}-row`,
135140
`${prefixCls}-row-level-${indent}`,
136141
rowProps?.className,
142+
indent >= 1 ? computedExpandedRowClassName : '',
137143
)}
138144
style={{
139145
...style,
@@ -179,8 +185,7 @@ function BodyRow<RecordType extends { children?: readonly RecordType[] }>(
179185
let expandRowNode: React.ReactElement;
180186
if (rowSupportExpand && (expandedRef.current || expanded)) {
181187
const expandContent = expandedRowRender(record, index, indent + 1, expanded);
182-
const computedExpandedRowClassName =
183-
expandedRowClassName && expandedRowClassName(record, index, indent);
188+
184189
expandRowNode = (
185190
<ExpandedRow
186191
expanded={expanded}

tests/ExpandRow.spec.jsx

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,24 @@ describe('Table.Expand', () => {
415415
expect(wrapper.find('tbody tr').at(1).hasClass('expand-row-test-class-name')).toBeTruthy();
416416
});
417417

418+
it('renders expend row class correctly using children without expandedRowRender', () => {
419+
const expandedRowClassName = vi.fn().mockReturnValue('expand-row-test-class-name');
420+
421+
const _data = [{ ...sampleData[0], children: [sampleData[1]] }];
422+
423+
const wrapper = mount(
424+
createTable({
425+
data: _data,
426+
expandable: {
427+
expandedRowKeys: [0],
428+
expandedRowClassName,
429+
},
430+
}),
431+
);
432+
433+
expect(wrapper.find('tbody tr').at(1).hasClass('expand-row-test-class-name')).toBeTruthy();
434+
});
435+
418436
it('renders expend column title', () => {
419437
const wrapper = mount(
420438
createTable({

0 commit comments

Comments
 (0)