Skip to content

Commit 5524853

Browse files
committed
feat: expandedRowClassName support receive a string
1 parent 8efa10c commit 5524853

File tree

3 files changed

+28
-4
lines changed

3 files changed

+28
-4
lines changed

src/Body/BodyRow.tsx

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,15 @@ function BodyRow<RecordType extends { children?: readonly RecordType[] }>(
126126

127127
// 若没有 expandedRowRender 参数, 将使用 baseRowNode 渲染 Children
128128
// 此时如果 level > 1 则说明是 expandedRow, 一样需要附加 computedExpandedRowClassName
129-
const computedExpandedRowClassName =
130-
expandedRowClassName && expandedRowClassName(record, index, indent);
129+
const computedExpandedRowClassName = React.useMemo<string>(() => {
130+
if (typeof expandedRowClassName === 'string') {
131+
return expandedRowClassName;
132+
}
133+
if (typeof expandedRowClassName === 'function') {
134+
return expandedRowClassName(record, index, indent);
135+
}
136+
return '';
137+
}, [expandedRowClassName, record, index, indent]);
131138

132139
// ======================== Base tr row ========================
133140
const baseRowNode = (
@@ -139,7 +146,9 @@ function BodyRow<RecordType extends { children?: readonly RecordType[] }>(
139146
`${prefixCls}-row`,
140147
`${prefixCls}-row-level-${indent}`,
141148
rowProps?.className,
142-
indent >= 1 ? computedExpandedRowClassName : '',
149+
{
150+
[computedExpandedRowClassName]: indent >= 1,
151+
},
143152
)}
144153
style={{
145154
...style,

src/context/TableContext.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export interface TableContextProps<RecordType = any> {
3737

3838
// Body
3939
rowClassName: string | RowClassName<RecordType>;
40-
expandedRowClassName: RowClassName<RecordType>;
40+
expandedRowClassName: string | RowClassName<RecordType>;
4141
onRow?: GetComponentProps<RecordType>;
4242
emptyNode?: React.ReactNode;
4343

tests/ExpandRow.spec.jsx

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,21 @@ 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 when it's string", () => {
419+
const expandedRowClassName = 'expand-row-test-str-class-name';
420+
const wrapper = mount(
421+
createTable({
422+
expandable: {
423+
expandedRowRender,
424+
expandedRowKeys: [0],
425+
expandedRowClassName,
426+
},
427+
}),
428+
);
429+
430+
expect(wrapper.find('tbody tr').at(1).hasClass(expandedRowClassName)).toBeTruthy();
431+
});
432+
418433
it('renders expend row class correctly using children without expandedRowRender', () => {
419434
const expandedRowClassName = vi.fn().mockReturnValue('expand-row-test-class-name');
420435

0 commit comments

Comments
 (0)