From 2447ba1d6a912c806ce4bc3392667496bbcea49c Mon Sep 17 00:00:00 2001 From: Stephen Smith Date: Mon, 29 Apr 2024 09:35:02 -0400 Subject: [PATCH] 21642: Change conditionaltabs to render tabs as hidden to avoid re-renders Arvados-DCO-1.1-Signed-off-by: Stephen Smith --- .../conditional-tabs/conditional-tabs.tsx | 15 ++++++++++---- .../views/process-panel/process-io-card.tsx | 20 +++++++++++-------- 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/services/workbench2/src/components/conditional-tabs/conditional-tabs.tsx b/services/workbench2/src/components/conditional-tabs/conditional-tabs.tsx index ff8d517c4e..499e84df8a 100644 --- a/services/workbench2/src/components/conditional-tabs/conditional-tabs.tsx +++ b/services/workbench2/src/components/conditional-tabs/conditional-tabs.tsx @@ -2,14 +2,19 @@ // // SPDX-License-Identifier: AGPL-3.0 -import React, { ReactNode, useEffect, useState } from "react"; +import React, { CSSProperties, ReactElement, useEffect, useState } from "react"; import { Tabs, Tab } from "@material-ui/core"; import { TabsProps } from "@material-ui/core/Tabs"; +interface ComponentWithHidden { + styles: CSSProperties; + hidden: boolean; +}; + export type TabData = { show: boolean; label: string; - content: ReactNode; + content: ReactElement; }; type ConditionalTabsProps = { @@ -19,7 +24,6 @@ type ConditionalTabsProps = { export const ConditionalTabs = (props: Omit & ConditionalTabsProps) => { const [tabState, setTabState] = useState(0); const visibleTabs = props.tabs.filter(tab => tab.show); - const activeTab = visibleTabs[tabState]; const visibleTabNames = visibleTabs.map(tab => tab.label).join(); const handleTabChange = (event: React.MouseEvent, value: number) => { @@ -39,6 +43,9 @@ export const ConditionalTabs = (props: Omit & C onChange={handleTabChange} > {visibleTabs.map(tab => )} - {activeTab && activeTab.content} + + {visibleTabs.map((tab, i) => ( + React.cloneElement(tab.content, {hidden: i !== tabState}) + ))} ; }; diff --git a/services/workbench2/src/views/process-panel/process-io-card.tsx b/services/workbench2/src/views/process-panel/process-io-card.tsx index 76d4c52d04..6d60b8cf22 100644 --- a/services/workbench2/src/views/process-panel/process-io-card.tsx +++ b/services/workbench2/src/views/process-panel/process-io-card.tsx @@ -485,12 +485,13 @@ export type ProcessIOParameter = { interface ProcessIOPreviewDataProps { data: ProcessIOParameter[]; valueLabel: string; + hidden?: boolean; } type ProcessIOPreviewProps = ProcessIOPreviewDataProps & WithStyles; const ProcessIOPreview = memo( - withStyles(styles)(({ data, valueLabel, classes }: ProcessIOPreviewProps) => { + withStyles(styles)(({ data, valueLabel, hidden, classes }: ProcessIOPreviewProps) => { const showLabel = data.some((param: ProcessIOParameter) => param.label); const hasMoreValues = (index: number) => ( @@ -546,7 +547,7 @@ const ProcessIOPreview = memo( ; }; - return
+ return