diff --git a/src/Body/BodyRow.tsx b/src/Body/BodyRow.tsx index 8cee145bf..faa2fefc5 100644 --- a/src/Body/BodyRow.tsx +++ b/src/Body/BodyRow.tsx @@ -199,6 +199,7 @@ function BodyRow( cellComponent={cellComponent} colSpan={flattenColumns.length} isEmpty={false} + data={record} > {expandContent} diff --git a/src/Body/ExpandedRow.tsx b/src/Body/ExpandedRow.tsx index 22cb4032a..7fa8ba542 100644 --- a/src/Body/ExpandedRow.tsx +++ b/src/Body/ExpandedRow.tsx @@ -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; @@ -14,6 +15,7 @@ export interface ExpandedRowProps { children: React.ReactNode; colSpan: number; isEmpty: boolean; + data?: any; } function ExpandedRow(props: ExpandedRowProps) { @@ -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'], @@ -60,11 +65,17 @@ function ExpandedRow(props: ExpandedRowProps) { - {contentNode} + {data.hasChildren ? LazyData : contentNode} ); diff --git a/src/Table.tsx b/src/Table.tsx index a8c6344f8..a2a76fbec 100644 --- a/src/Table.tsx +++ b/src/Table.tsx @@ -103,6 +103,7 @@ export interface TableProps expandable?: ExpandableConfig; indentSize?: number; rowClassName?: string | RowClassName; + load?: (data: any) => void; // Additional Part footer?: PanelRender; diff --git a/src/hooks/useResolve.ts b/src/hooks/useResolve.ts new file mode 100644 index 000000000..be0adffe6 --- /dev/null +++ b/src/hooks/useResolve.ts @@ -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;