Skip to content

feat: support lazy expand #1105

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

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 Cell from '../Cell';
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 @@ export interface ExpandedRowProps {
children: React.ReactNode;
colSpan: number;
isEmpty: boolean;
data?: any;
}

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

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

const { scrollbarSize, fixHeader, fixColumn, componentWidth, horizonScroll } = useContext(
TableContext,
['scrollbarSize', 'fixHeader', 'fixColumn', 'componentWidth', 'horizonScroll'],
Expand Down Expand Up @@ -60,11 +65,17 @@ function ExpandedRow(props: ExpandedRowProps) {
<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;