Skip to content

Commit

Permalink
feat: expandedRowClassName support receive a string
Browse files Browse the repository at this point in the history
  • Loading branch information
li-jia-nan committed Sep 28, 2024
1 parent 8efa10c commit 5524853
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
15 changes: 12 additions & 3 deletions src/Body/BodyRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,15 @@ function BodyRow<RecordType extends { children?: readonly RecordType[] }>(

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

// ======================== Base tr row ========================
const baseRowNode = (
Expand All @@ -139,7 +146,9 @@ function BodyRow<RecordType extends { children?: readonly RecordType[] }>(
`${prefixCls}-row`,
`${prefixCls}-row-level-${indent}`,
rowProps?.className,
indent >= 1 ? computedExpandedRowClassName : '',
{
[computedExpandedRowClassName]: indent >= 1,
},
)}
style={{
...style,
Expand Down
2 changes: 1 addition & 1 deletion src/context/TableContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export interface TableContextProps<RecordType = any> {

// Body
rowClassName: string | RowClassName<RecordType>;
expandedRowClassName: RowClassName<RecordType>;
expandedRowClassName: string | RowClassName<RecordType>;
onRow?: GetComponentProps<RecordType>;
emptyNode?: React.ReactNode;

Expand Down
15 changes: 15 additions & 0 deletions tests/ExpandRow.spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,21 @@ describe('Table.Expand', () => {
expect(wrapper.find('tbody tr').at(1).hasClass('expand-row-test-class-name')).toBeTruthy();
});

it("renders expend row class correctly when it's string", () => {
const expandedRowClassName = 'expand-row-test-str-class-name';
const wrapper = mount(
createTable({
expandable: {
expandedRowRender,
expandedRowKeys: [0],
expandedRowClassName,
},
}),
);

expect(wrapper.find('tbody tr').at(1).hasClass(expandedRowClassName)).toBeTruthy();
});

it('renders expend row class correctly using children without expandedRowRender', () => {
const expandedRowClassName = vi.fn().mockReturnValue('expand-row-test-class-name');

Expand Down

0 comments on commit 5524853

Please sign in to comment.