From a4c75b4882a92e04c8afdd58174d04e1b17d7131 Mon Sep 17 00:00:00 2001 From: Jaco Date: Mon, 16 Jan 2023 13:31:25 +0200 Subject: [PATCH] Optimize table split rendering (#8825) * Optimize table split rendering * Rename creation function * Adjust naming of body* variables * One more body extraction rename --- packages/react-components/src/Table/index.tsx | 64 ++++++++++--------- 1 file changed, 35 insertions(+), 29 deletions(-) diff --git a/packages/react-components/src/Table/index.tsx b/packages/react-components/src/Table/index.tsx index e3651b3622..d4a0d4e0dc 100644 --- a/packages/react-components/src/Table/index.tsx +++ b/packages/react-components/src/Table/index.tsx @@ -27,31 +27,41 @@ interface Props { noBodyTag?: boolean; } -function extractBodyChildren (children: React.ReactNode): [boolean, React.ReactNode | React.ReactNode[]] { +function extractBody (children: React.ReactNode, isSplit?: boolean): { body: React.ReactNode | React.ReactNode[], bodySplit: [React.ReactNode[], React.ReactNode[]] | null, isEmpty: boolean } { if (!Array.isArray(children)) { - return [!children, children]; + return { + body: children, + bodySplit: null, + isEmpty: !children + }; } - const kids = children.filter((child) => !!child); - const isEmpty = kids.length === 0; + const body = children.filter((child) => !!child); + const isEmpty = body.length === 0; + let bodySplit: [React.ReactNode[], React.ReactNode[]] | null = null; - return [isEmpty, isEmpty ? null : kids]; + if (!isEmpty && isSplit) { + const half = Math.ceil(body.length / 2); + + bodySplit = [ + body.slice(0, half), + body.slice(half) + ]; + } + + return { + body: isEmpty || bodySplit + ? null + : body, + bodySplit, + isEmpty + }; } function TableBase ({ children, className = '', empty, emptySpinner, filter, footer, header, headerChildren, isFixed, isInline, isSplit, legend, noBodyTag }: Props): React.ReactElement { - const [isEmpty, bodyChildren] = extractBodyChildren(children); - - const splitBody = useMemo( - (): [React.ReactNode[], React.ReactNode[]] | null => { - if (!isSplit || isEmpty || !Array.isArray(bodyChildren) || bodyChildren.length === 0) { - return null; - } - - const half = Math.ceil(bodyChildren.length / 2); - - return [bodyChildren.slice(0, half), bodyChildren.slice(half)]; - }, - [bodyChildren, isEmpty, isSplit] + const { body, bodySplit, isEmpty } = useMemo( + () => extractBody(children, isSplit), + [children, isSplit] ); const headerNode = ( @@ -64,12 +74,12 @@ function TableBase ({ children, className = '', empty, emptySpinner, filter, foo ); - const tableClassName = `${(isFixed && !isEmpty) ? 'isFixed' : 'isNotFixed'} ${isInline ? 'isInline' : ''} ${splitBody ? 'noMargin' : ''}`; + const tableClassName = `${(isFixed && !isEmpty) ? 'isFixed' : 'isNotFixed'} ${isInline ? 'isInline' : ''} ${bodySplit ? 'noMargin' : ''}`; return ( - + {legend} - {splitBody + {bodySplit ? ( <> @@ -78,16 +88,12 @@ function TableBase ({ children, className = '', empty, emptySpinner, filter, foo
- - {splitBody[0]} - + {bodySplit[0]}
- - {splitBody[1]} - + {bodySplit[1]}
@@ -101,7 +107,7 @@ function TableBase ({ children, className = '', empty, emptySpinner, filter, foo emptySpinner={emptySpinner} noBodyTag={noBodyTag} > - {bodyChildren} + {body}