Skip to content

Commit

Permalink
enhance: Rm duplicated re-render of expanded row (#1022)
Browse files Browse the repository at this point in the history
* test: test driven

* fix: not expanded for rerender
  • Loading branch information
zombieJ authored Sep 18, 2023
1 parent f6e1871 commit 6e49703
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
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;

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);
});
});

1 comment on commit 6e49703

@vercel
Copy link

@vercel vercel bot commented on 6e49703 Sep 18, 2023

Choose a reason for hiding this comment

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

Successfully deployed to the following URLs:

table – ./

table-git-master-react-component.vercel.app
table-react-component.vercel.app

Please sign in to comment.