From 44a15e609f096e76874bb50ff075c52b007fad78 Mon Sep 17 00:00:00 2001 From: mcwong Date: Sun, 11 Dec 2022 00:43:50 +0800 Subject: [PATCH] fix: tooltip can't disappear when mouseleave on Paragraph (#38509) --- components/typography/Base/index.tsx | 36 ++++++++++++++----- components/typography/__tests__/copy.test.tsx | 16 +++++++++ 2 files changed, 44 insertions(+), 8 deletions(-) diff --git a/components/typography/Base/index.tsx b/components/typography/Base/index.tsx index 7ef36168b59b..7b2febfc74b3 100644 --- a/components/typography/Base/index.tsx +++ b/components/typography/Base/index.tsx @@ -191,15 +191,19 @@ const Base = React.forwardRef((props, ref) => { // ========================== Copyable ========================== const [enableCopy, copyConfig] = useMergedConfig(copyable); const [copied, setCopied] = React.useState(false); - const copyIdRef = React.useRef(); + const [copyTooltipVisible, setCopyTootipVisible] = React.useState(false); + const copyIdsRef = React.useRef([]); + + const copyVisibleTime = 3000; const copyOptions: Pick = {}; if (copyConfig.format) { copyOptions.format = copyConfig.format; } - const cleanCopyId = () => { - window.clearTimeout(copyIdRef.current!); + const cleanCopyIds = () => { + copyIdsRef.current.forEach((id) => window.clearTimeout(id)); + copyIdsRef.current = []; }; const onCopyClick = (e?: React.MouseEvent) => { @@ -211,15 +215,27 @@ const Base = React.forwardRef((props, ref) => { setCopied(true); // Trigger tips update - cleanCopyId(); - copyIdRef.current = window.setTimeout(() => { + cleanCopyIds(); + const copyTimeoutId = window.setTimeout(() => { setCopied(false); - }, 3000); + }, copyVisibleTime); + const copyVisibleTimeoutId = window.setTimeout(() => { + setCopyTootipVisible(false); + }, copyVisibleTime - 50); + copyIdsRef.current.push(copyTimeoutId, copyVisibleTimeoutId); copyConfig.onCopy?.(e); }; - React.useEffect(() => cleanCopyId, []); + const onCopyMouseOver = () => { + setCopyTootipVisible(true); + }; + + const onCopyMouseLeft = () => { + setCopyTootipVisible(false); + }; + + React.useEffect(() => cleanCopyIds, []); // ========================== Ellipsis ========================== const [isLineClampSupport, setIsLineClampSupport] = React.useState(false); @@ -459,12 +475,16 @@ const Base = React.forwardRef((props, ref) => { const systemStr = copied ? textLocale.copied : textLocale.copy; const ariaLabel = typeof copyTitle === 'string' ? copyTitle : systemStr; + const tooltipVisible = copyTooltipVisible && !!copyTitle; + return ( - + {copied ? getNode(iconNodes[1], , true) diff --git a/components/typography/__tests__/copy.test.tsx b/components/typography/__tests__/copy.test.tsx index ed5c6d8ea9e6..c738da01c4c8 100644 --- a/components/typography/__tests__/copy.test.tsx +++ b/components/typography/__tests__/copy.test.tsx @@ -265,5 +265,21 @@ describe('Typography copy', () => { expect(spy.mock.calls[0][0]).toEqual(nextText); jest.useRealTimers(); }); + + it('tooltip should hide when mouse leave', () => { + const { container } = render( + + test copy + , + ); + + expect(container.getElementsByTagName('span')).toHaveLength(1); + const button = container.getElementsByTagName('span')[0]; + + fireEvent.mouseOver(button); + expect(container.querySelector('.ant-tooltip-open')).not.toBeNull(); + fireEvent.mouseLeave(button); + expect(container.querySelector('.ant-tooltip-open')).toBeNull(); + }); }); });