Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 5 additions & 9 deletions src/Body/BodyRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import * as React from 'react';
import Cell from '../Cell';
import { responseImmutable } from '../context/TableContext';
import devRenderTimes from '../hooks/useRenderTimes';
import useRowInfo from '../hooks/useRowInfo';
import type { ColumnType, CustomizeComponent, GetRowKey } from '../interface';
import ExpandedRow from './ExpandedRow';
import useRowInfo from '../hooks/useRowInfo';

export interface BodyRowProps<RecordType> {
record: RecordType;
Expand Down Expand Up @@ -116,18 +116,14 @@ function BodyRow<RecordType extends { children?: readonly RecordType[] }>(
rowSupportExpand,
} = rowInfo;

const [expandRended, setExpandRended] = React.useState(false);
// Force render expand row if expanded before
const expandedRef = React.useRef(false);
expandedRef.current ||= expanded;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

新语法? 我赶紧百度学习一下.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

||= 不是新语法,就是用的比较少~~


if (process.env.NODE_ENV !== 'production') {
devRenderTimes(props);
}

React.useEffect(() => {
if (expanded) {
setExpandRended(true);
}
}, [expanded]);

// ======================== Base tr row ========================
const baseRowNode = (
<RowComponent
Expand Down Expand Up @@ -181,7 +177,7 @@ function BodyRow<RecordType extends { children?: readonly RecordType[] }>(

// ======================== Expand Row =========================
let expandRowNode: React.ReactElement;
if (rowSupportExpand && (expandRended || expanded)) {
if (rowSupportExpand && (expandedRef.current || expanded)) {
const expandContent = expandedRowRender(record, index, indent + 1, expanded);
const computedExpandedRowClassName =
expandedRowClassName && expandedRowClassName(record, index, indent);
Expand Down
15 changes: 15 additions & 0 deletions tests/ExpandRow.spec.jsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { render } from '@testing-library/react';
import { mount } from 'enzyme';
import { spyElementPrototype } from 'rc-util/lib/test/domHook';
import { resetWarned } from 'rc-util/lib/warning';
Expand Down Expand Up @@ -636,4 +637,18 @@ describe('Table.Expand', () => {
);
errorSpy.mockRestore();
});

it('should only trigger once', () => {
const expandedRowRender = vi.fn(() => <p>extra data</p>);
render(
createTable({
expandable: {
expandedRowRender,
expandedRowKeys: [0],
},
}),
);

expect(expandedRowRender).toHaveBeenCalledTimes(1);
});
});