From bd6b854851c9ba8a19065902832eeee39491b857 Mon Sep 17 00:00:00 2001 From: Martin Muzikar <46345469+mmuzikar@users.noreply.github.com> Date: Thu, 26 Sep 2024 22:54:35 +0200 Subject: [PATCH] fix: add checks for contentRef to avoid errors in console (#10870) --- .../ExpandableSection/ExpandableSection.tsx | 26 ++++++++++++------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/packages/react-core/src/components/ExpandableSection/ExpandableSection.tsx b/packages/react-core/src/components/ExpandableSection/ExpandableSection.tsx index 27c26cce648..e1d338a4a4f 100644 --- a/packages/react-core/src/components/ExpandableSection/ExpandableSection.tsx +++ b/packages/react-core/src/components/ExpandableSection/ExpandableSection.tsx @@ -70,7 +70,7 @@ export interface ExpandableSectionProps extends React.HTMLProps interface ExpandableSectionState { isExpanded: boolean; hasToggle: boolean; - previousWidth: number; + previousWidth?: number; } const setLineClamp = (lines: number, element: HTMLDivElement) => { @@ -87,7 +87,7 @@ class ExpandableSection extends React.Component { - if (this.expandableContentRef.current) { - const { offsetWidth } = this.expandableContentRef.current; - if (this.state.previousWidth !== offsetWidth) { - this.setState({ previousWidth: offsetWidth }); - this.checkToggleVisibility(); - } + if (!this.expandableContentRef.current) { + return; + } + const { offsetWidth } = this.expandableContentRef.current; + if (this.state.previousWidth !== offsetWidth) { + this.setState({ previousWidth: offsetWidth }); + this.checkToggleVisibility(); } }; handleResize = debounce(this.resize, 250); @@ -221,7 +227,7 @@ class ExpandableSection extends React.Component { - this.setState({ isExpanded: isOpen }, () => onToggleProp(event, this.state.isExpanded)); + this.setState({ isExpanded: isOpen }, () => onToggleProp?.(event, this.state.isExpanded)); }; } @@ -239,7 +245,7 @@ class ExpandableSection extends React.Component onToggle(event, !propOrStateIsExpanded)} + onClick={(event) => onToggle?.(event, !propOrStateIsExpanded)} > {variant !== ExpandableSectionVariant.truncate && (