From 0371c6a7f96da0689c9a0d6af18f9e1a8ba0bd3d Mon Sep 17 00:00:00 2001 From: Guido Modarelli Date: Wed, 11 Dec 2024 11:42:29 -0300 Subject: [PATCH] refactor(wz-text-with-tooltip): convert class to functional component with hooks for cleaner state management and rendering logic --- .../wz-text-with-tooltip-if-truncated.tsx | 91 ++++++++----------- 1 file changed, 39 insertions(+), 52 deletions(-) diff --git a/plugins/main/public/components/common/wz-text-with-tooltip-if-truncated.tsx b/plugins/main/public/components/common/wz-text-with-tooltip-if-truncated.tsx index 59d0cafb29..5ff4ab948d 100644 --- a/plugins/main/public/components/common/wz-text-with-tooltip-if-truncated.tsx +++ b/plugins/main/public/components/common/wz-text-with-tooltip-if-truncated.tsx @@ -11,7 +11,7 @@ * Find more information about this on the LICENSE file. */ -import React, { Component } from 'react'; +import React, { useEffect, useRef, useState } from 'react'; import { EuiToolTip } from '@elastic/eui'; import './wz-text-with-tooltip-if-truncated.scss'; @@ -21,23 +21,12 @@ interface WzTextWithTooltipIfTruncatedProps extends React.PropsWithChildren { tooltipProps?: object; } -export default class WzTextWithTooltipIfTruncated extends Component { - static defaultProps = { - contentStyle: {}, - }; - state: { - withTooltip: boolean; - }; - timer?: ReturnType; - contentReference: React.RefObject; - - constructor(props: WzTextWithTooltipIfTruncatedProps) { - super(props); - this.contentReference = React.createRef(); - this.state = { - withTooltip: false, - }; - } +const WzTextWithTooltipIfTruncated = ( + props: WzTextWithTooltipIfTruncatedProps, +) => { + const { contentStyle = {}, tooltip, tooltipProps, children } = props; + const [withTooltip, setWithTooltip] = useState(false); + const contentReference = useRef(null); /** * The function `createClone` creates a clone of an HTML element with specific @@ -48,7 +37,7 @@ export default class WzTextWithTooltipIfTruncated extends Component { // HTML element clone of reference const clone = reference.cloneNode(true) as HTMLElement; clone.style.display = 'inline'; @@ -56,49 +45,47 @@ export default class WzTextWithTooltipIfTruncated extends Component { - const reference = this.contentReference.current as HTMLElement; - const clone = this.createClone(reference); + useEffect(() => { + const timer = setTimeout(() => { + const reference = contentReference.current as HTMLElement; + const clone = createClone(reference); document.body.appendChild(clone); - this.setState({ - withTooltip: reference.offsetWidth < clone.offsetWidth, - }); + setWithTooltip(reference.offsetWidth < clone.offsetWidth); clone.remove(); }); - } - componentWillUnmount() { - if (this.timer) { - clearTimeout(this.timer); - } - } + return () => { + if (timer) { + clearTimeout(timer); + } + }; + }, [children]); - renderContent() { + const renderContent = () => { return ( - {this.props.children || this.props.tooltip} + {children || tooltip} ); - } + }; - render() { - return this.state.withTooltip ? ( - - {this.renderContent()} - - ) : ( - this.renderContent() - ); - } -} + return withTooltip ? ( + + {renderContent()} + + ) : ( + renderContent() + ); +}; + +export default React.memo(WzTextWithTooltipIfTruncated);