Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support lazy expand #1105

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions src/Body/BodyRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ function BodyRow<RecordType extends { children?: readonly RecordType[] }>(
cellComponent={cellComponent}
colSpan={flattenColumns.length}
isEmpty={false}
data={record}
>
{expandContent}
</ExpandedRow>
Expand Down
15 changes: 13 additions & 2 deletions src/Body/ExpandedRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import TableContext from '../context/TableContext';
import devRenderTimes from '../hooks/useRenderTimes';
import type { CustomizeComponent } from '../interface';
import useResolve from '../hooks/useResolve';

export interface ExpandedRowProps {
prefixCls: string;
Expand All @@ -14,6 +15,7 @@
children: React.ReactNode;
colSpan: number;
isEmpty: boolean;
data?: any;
}

function ExpandedRow(props: ExpandedRowProps) {
Expand All @@ -30,8 +32,11 @@
expanded,
colSpan,
isEmpty,
data,
} = props;

const { data: LazyData, resolve } = useResolve(data.load);

Check failure on line 38 in src/Body/ExpandedRow.tsx

View workflow job for this annotation

GitHub Actions / coverage

tests/Cell.spec.tsx > Table.Cell > onHeaderCell

TypeError: Cannot read properties of undefined (reading 'load') ❯ ExpandedRow src/Body/ExpandedRow.tsx:38:55 ❯ renderWithHooks node_modules/react-dom/cjs/react-dom.development.js:14803:18 ❯ mountIndeterminateComponent node_modules/react-dom/cjs/react-dom.development.js:17482:13 ❯ beginWork node_modules/react-dom/cjs/react-dom.development.js:18596:16 ❯ HTMLUnknownElement.callCallback node_modules/react-dom/cjs/react-dom.development.js:188:14 ❯ HTMLUnknownElement.callTheUserObjectsOperation node_modules/jsdom/lib/jsdom/living/generated/EventListener.js:26:30 ❯ innerInvokeEventListeners node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:350:25 ❯ invokeEventListeners node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:286:3 ❯ HTMLUnknownElementImpl._dispatch node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:233:9 ❯ HTMLUnknownElementImpl.dispatchEvent node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:104:17

Check failure on line 38 in src/Body/ExpandedRow.tsx

View workflow job for this annotation

GitHub Actions / coverage

tests/Colgroup.spec.jsx > Table.ColGroup > internal props should render

TypeError: Cannot read properties of undefined (reading 'load') ❯ ExpandedRow src/Body/ExpandedRow.tsx:38:55 ❯ renderWithHooks node_modules/react-dom/cjs/react-dom.development.js:14803:18 ❯ mountIndeterminateComponent node_modules/react-dom/cjs/react-dom.development.js:17482:13 ❯ beginWork node_modules/react-dom/cjs/react-dom.development.js:18596:16 ❯ HTMLUnknownElement.callCallback node_modules/react-dom/cjs/react-dom.development.js:188:14 ❯ HTMLUnknownElement.callTheUserObjectsOperation node_modules/jsdom/lib/jsdom/living/generated/EventListener.js:26:30 ❯ innerInvokeEventListeners node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:350:25 ❯ invokeEventListeners node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:286:3 ❯ HTMLUnknownElementImpl._dispatch node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:233:9 ❯ HTMLUnknownElementImpl.dispatchEvent node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:104:17

Check failure on line 38 in src/Body/ExpandedRow.tsx

View workflow job for this annotation

GitHub Actions / coverage

tests/Colgroup.spec.jsx > Table.ColGroup > correct key

TypeError: Cannot read properties of undefined (reading 'load') ❯ ExpandedRow src/Body/ExpandedRow.tsx:38:55 ❯ renderWithHooks node_modules/react-dom/cjs/react-dom.development.js:14803:18 ❯ mountIndeterminateComponent node_modules/react-dom/cjs/react-dom.development.js:17482:13 ❯ beginWork node_modules/react-dom/cjs/react-dom.development.js:18596:16 ❯ HTMLUnknownElement.callCallback node_modules/react-dom/cjs/react-dom.development.js:188:14 ❯ HTMLUnknownElement.callTheUserObjectsOperation node_modules/jsdom/lib/jsdom/living/generated/EventListener.js:26:30 ❯ innerInvokeEventListeners node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:350:25 ❯ invokeEventListeners node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:286:3 ❯ HTMLUnknownElementImpl._dispatch node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:233:9 ❯ HTMLUnknownElementImpl.dispatchEvent node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:104:17

Check failure on line 38 in src/Body/ExpandedRow.tsx

View workflow job for this annotation

GitHub Actions / coverage

tests/Deprecated.spec.jsx > Table.Deprecated > warning > renders table body to the wrapper

TypeError: Cannot read properties of undefined (reading 'load') ❯ ExpandedRow src/Body/ExpandedRow.tsx:38:55 ❯ renderWithHooks node_modules/react-dom/cjs/react-dom.development.js:14803:18 ❯ mountIndeterminateComponent node_modules/react-dom/cjs/react-dom.development.js:17482:13 ❯ beginWork node_modules/react-dom/cjs/react-dom.development.js:18596:16 ❯ HTMLUnknownElement.callCallback node_modules/react-dom/cjs/react-dom.development.js:188:14 ❯ HTMLUnknownElement.callTheUserObjectsOperation node_modules/jsdom/lib/jsdom/living/generated/EventListener.js:26:30 ❯ innerInvokeEventListeners node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:350:25 ❯ invokeEventListeners node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:286:3 ❯ HTMLUnknownElementImpl._dispatch node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:233:9 ❯ HTMLUnknownElementImpl.dispatchEvent node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:104:17

Check failure on line 38 in src/Body/ExpandedRow.tsx

View workflow job for this annotation

GitHub Actions / coverage

tests/Deprecated.spec.jsx > Table.Deprecated > warning > warning for 'onRowClick'

TypeError: Cannot read properties of undefined (reading 'load') ❯ ExpandedRow src/Body/ExpandedRow.tsx:38:55 ❯ renderWithHooks node_modules/react-dom/cjs/react-dom.development.js:14803:18 ❯ mountIndeterminateComponent node_modules/react-dom/cjs/react-dom.development.js:17482:13 ❯ beginWork node_modules/react-dom/cjs/react-dom.development.js:18596:16 ❯ HTMLUnknownElement.callCallback node_modules/react-dom/cjs/react-dom.development.js:188:14 ❯ HTMLUnknownElement.callTheUserObjectsOperation node_modules/jsdom/lib/jsdom/living/generated/EventListener.js:26:30 ❯ innerInvokeEventListeners node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:350:25 ❯ invokeEventListeners node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:286:3 ❯ HTMLUnknownElementImpl._dispatch node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:233:9 ❯ HTMLUnknownElementImpl.dispatchEvent node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:104:17

Check failure on line 38 in src/Body/ExpandedRow.tsx

View workflow job for this annotation

GitHub Actions / coverage

tests/Deprecated.spec.jsx > Table.Deprecated > warning > warning for 'onRowDoubleClick'

TypeError: Cannot read properties of undefined (reading 'load') ❯ ExpandedRow src/Body/ExpandedRow.tsx:38:55 ❯ renderWithHooks node_modules/react-dom/cjs/react-dom.development.js:14803:18 ❯ mountIndeterminateComponent node_modules/react-dom/cjs/react-dom.development.js:17482:13 ❯ beginWork node_modules/react-dom/cjs/react-dom.development.js:18596:16 ❯ HTMLUnknownElement.callCallback node_modules/react-dom/cjs/react-dom.development.js:188:14 ❯ HTMLUnknownElement.callTheUserObjectsOperation node_modules/jsdom/lib/jsdom/living/generated/EventListener.js:26:30 ❯ innerInvokeEventListeners node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:350:25 ❯ invokeEventListeners node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:286:3 ❯ HTMLUnknownElementImpl._dispatch node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:233:9 ❯ HTMLUnknownElementImpl.dispatchEvent node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:104:17

Check failure on line 38 in src/Body/ExpandedRow.tsx

View workflow job for this annotation

GitHub Actions / coverage

tests/Deprecated.spec.jsx > Table.Deprecated > warning > warning for 'onRowMouseEnter'

TypeError: Cannot read properties of undefined (reading 'load') ❯ ExpandedRow src/Body/ExpandedRow.tsx:38:55 ❯ renderWithHooks node_modules/react-dom/cjs/react-dom.development.js:14803:18 ❯ mountIndeterminateComponent node_modules/react-dom/cjs/react-dom.development.js:17482:13 ❯ beginWork node_modules/react-dom/cjs/react-dom.development.js:18596:16 ❯ HTMLUnknownElement.callCallback node_modules/react-dom/cjs/react-dom.development.js:188:14 ❯ HTMLUnknownElement.callTheUserObjectsOperation node_modules/jsdom/lib/jsdom/living/generated/EventListener.js:26:30 ❯ innerInvokeEventListeners node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:350:25 ❯ invokeEventListeners node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:286:3 ❯ HTMLUnknownElementImpl._dispatch node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:233:9 ❯ HTMLUnknownElementImpl.dispatchEvent node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:104:17

Check failure on line 38 in src/Body/ExpandedRow.tsx

View workflow job for this annotation

GitHub Actions / coverage

tests/Deprecated.spec.jsx > Table.Deprecated > warning > warning for 'onRowMouseLeave'

TypeError: Cannot read properties of undefined (reading 'load') ❯ ExpandedRow src/Body/ExpandedRow.tsx:38:55 ❯ renderWithHooks node_modules/react-dom/cjs/react-dom.development.js:14803:18 ❯ mountIndeterminateComponent node_modules/react-dom/cjs/react-dom.development.js:17482:13 ❯ beginWork node_modules/react-dom/cjs/react-dom.development.js:18596:16 ❯ HTMLUnknownElement.callCallback node_modules/react-dom/cjs/react-dom.development.js:188:14 ❯ HTMLUnknownElement.callTheUserObjectsOperation node_modules/jsdom/lib/jsdom/living/generated/EventListener.js:26:30 ❯ innerInvokeEventListeners node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:350:25 ❯ invokeEventListeners node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:286:3 ❯ HTMLUnknownElementImpl._dispatch node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:233:9 ❯ HTMLUnknownElementImpl.dispatchEvent node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:104:17

Check failure on line 38 in src/Body/ExpandedRow.tsx

View workflow job for this annotation

GitHub Actions / coverage

tests/FixedColumn.spec.tsx > Table.FixedColumn > renders correctly > scrollX - without data

TypeError: Cannot read properties of undefined (reading 'load') ❯ ExpandedRow src/Body/ExpandedRow.tsx:38:55 ❯ renderWithHooks node_modules/react-dom/cjs/react-dom.development.js:14803:18 ❯ mountIndeterminateComponent node_modules/react-dom/cjs/react-dom.development.js:17482:13 ❯ beginWork node_modules/react-dom/cjs/react-dom.development.js:18596:16 ❯ HTMLUnknownElement.callCallback node_modules/react-dom/cjs/react-dom.development.js:188:14 ❯ HTMLUnknownElement.callTheUserObjectsOperation node_modules/jsdom/lib/jsdom/living/generated/EventListener.js:26:30 ❯ innerInvokeEventListeners node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:350:25 ❯ invokeEventListeners node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:286:3 ❯ HTMLUnknownElementImpl._dispatch node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:233:9 ❯ HTMLUnknownElementImpl.dispatchEvent node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:104:17

Check failure on line 38 in src/Body/ExpandedRow.tsx

View workflow job for this annotation

GitHub Actions / coverage

tests/FixedColumn.spec.tsx > Table.FixedColumn > renders correctly > scrollXY - without data

TypeError: Cannot read properties of undefined (reading 'load') ❯ ExpandedRow src/Body/ExpandedRow.tsx:38:55 ❯ renderWithHooks node_modules/react-dom/cjs/react-dom.development.js:14803:18 ❯ mountIndeterminateComponent node_modules/react-dom/cjs/react-dom.development.js:17482:13 ❯ beginWork node_modules/react-dom/cjs/react-dom.development.js:18596:16 ❯ HTMLUnknownElement.callCallback node_modules/react-dom/cjs/react-dom.development.js:188:14 ❯ HTMLUnknownElement.callTheUserObjectsOperation node_modules/jsdom/lib/jsdom/living/generated/EventListener.js:26:30 ❯ innerInvokeEventListeners node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:350:25 ❯ invokeEventListeners node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:286:3 ❯ HTMLUnknownElementImpl._dispatch node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:233:9 ❯ HTMLUnknownElementImpl.dispatchEvent node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:104:17

const { scrollbarSize, fixHeader, fixColumn, componentWidth, horizonScroll } = useContext(
TableContext,
['scrollbarSize', 'fixHeader', 'fixColumn', 'componentWidth', 'horizonScroll'],
Expand Down Expand Up @@ -60,11 +65,17 @@
<Component
className={className}
style={{
display: expanded ? null : 'none',
display: data.hasChildren
? expanded && resolve
? null
: 'none'
: expanded
? null
: 'none',
}}
>
<Cell component={cellComponent} prefixCls={prefixCls} colSpan={colSpan}>
{contentNode}
{data.hasChildren ? LazyData : contentNode}
</Cell>
</Component>
);
Expand Down
1 change: 1 addition & 0 deletions src/Table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ export interface TableProps<RecordType = any>
expandable?: ExpandableConfig<RecordType>;
indentSize?: number;
rowClassName?: string | RowClassName<RecordType>;
load?: (data: any) => void;

// Additional Part
footer?: PanelRender<RecordType>;
Expand Down
21 changes: 21 additions & 0 deletions src/hooks/useResolve.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { useState, useEffect, useCallback } from 'react';

function useResolve(loadFunction) {
const [data, setData] = useState(null);
const [resolve, setResolve] = useState(false);

const stableLoadFunction = useCallback(loadFunction, []);

useEffect(() => {
if (stableLoadFunction) {
stableLoadFunction().then(result => {
setData(result);
setResolve(true);
});
}
}, [stableLoadFunction]);

return { data, resolve };
}

export default useResolve;
Loading