From 19f8505d0c52d47e67b63fb989ee60657abf0c45 Mon Sep 17 00:00:00 2001 From: MaHui Date: Wed, 10 May 2023 11:04:45 +0800 Subject: [PATCH] fix: Popover display empty div when title and content is null (#42229) * fix: Popover display empty div when title and content is null (#42217) * fix: Popover display empty div when title and content is null * test: Popover add test * lint: remove useless block statement --------- Co-authored-by: MaHui (cherry picked from commit 4271ff0976099619005509de57d24381f9fa34e5) * lint: fix Popover test lint error * lint: fix Popover test lint error * improvement: Popover overlay * lint: fix Popover test lint error * Update package.json --------- Co-authored-by: MaHui Co-authored-by: afc163 --- components/popover/__tests__/index.test.tsx | 30 ++++++++---------- components/popover/index.tsx | 35 ++++++++++++--------- package.json | 2 +- 3 files changed, 34 insertions(+), 33 deletions(-) diff --git a/components/popover/__tests__/index.test.tsx b/components/popover/__tests__/index.test.tsx index eb0e3c3499..fda37a048a 100644 --- a/components/popover/__tests__/index.test.tsx +++ b/components/popover/__tests__/index.test.tsx @@ -8,38 +8,34 @@ describe('Popover', () => { mountTest(Popover); it('should show overlay when trigger is clicked', () => { - const ref = React.createRef(); - const popover = render( - + show me your code , ); - - expect(ref.current.getPopupDomNode()).toBe(null); - expect(popover.container.querySelector('.ant-popover-inner-content')).toBeFalsy(); + expect(document.querySelector('.ant-popover')).toBe(null); fireEvent.click(popover.container.querySelector('span')!); - expect(popover.container.querySelector('.ant-popover-inner-content')).toBeTruthy(); + const popup = document.querySelector('.ant-popover'); + expect(popup?.querySelector?.('.ant-popover-inner-content')).toBeTruthy(); }); it('shows content for render functions', () => { const renderTitle = () => 'some-title'; const renderContent = () => 'some-content'; - const ref = React.createRef(); const popover = render( - + show me your code , ); fireEvent.click(popover.container.querySelector('span')!); - const popup = ref.current.getPopupDomNode(); + const popup = document.querySelector('.ant-popover'); expect(popup).not.toBe(null); - expect(popup.innerHTML).toContain('some-title'); - expect(popup.innerHTML).toContain('some-content'); - expect(popup.innerHTML).toMatchSnapshot(); + expect(popup?.innerHTML).toContain('some-title'); + expect(popup?.innerHTML).toContain('some-content'); + expect(popup?.innerHTML).toMatchSnapshot(); }); it('handles empty title/content props safely', () => { @@ -50,8 +46,8 @@ describe('Popover', () => { ); fireEvent.click(container.querySelector('span')!); - expect(container.querySelector('.ant-popover-title')?.textContent).toBeFalsy(); - expect(container.querySelector('.ant-popover-inner-content')?.textContent).toBeFalsy(); + const popup = document.querySelector('.ant-popover'); + expect(popup).toBe(null); }); it('should not render popover when the title & content props is empty', () => { @@ -62,8 +58,8 @@ describe('Popover', () => { ); fireEvent.click(container.querySelector('span')!); - expect(container.querySelector('.ant-popover-title')?.textContent).toBeFalsy(); - expect(container.querySelector('.ant-popover-inner-content')?.textContent).toBeFalsy(); + const popup = document.querySelector('.ant-popover'); + expect(popup).toBe(null); }); it('props#overlay do not warn anymore', () => { diff --git a/components/popover/index.tsx b/components/popover/index.tsx index 3f77b75b93..7ce2778929 100644 --- a/components/popover/index.tsx +++ b/components/popover/index.tsx @@ -1,10 +1,10 @@ import * as React from 'react'; -import { ConfigContext } from '../config-provider'; -import type { AbstractTooltipProps } from '../tooltip'; -import Tooltip from '../tooltip'; import type { RenderFunction } from '../_util/getRenderPropValue'; import { getRenderPropValue } from '../_util/getRenderPropValue'; import { getTransitionName } from '../_util/motion'; +import { ConfigContext } from '../config-provider'; +import type { AbstractTooltipProps } from '../tooltip'; +import Tooltip from '../tooltip'; export interface PopoverProps extends AbstractTooltipProps { title?: React.ReactNode | RenderFunction; @@ -19,17 +19,12 @@ interface OverlayPorps { content?: PopoverProps['content']; } -const Overlay: React.FC = ({ title, content, prefixCls }) => { - if (!title && !content) { - return null; - } - return ( - <> - {title &&
{getRenderPropValue(title)}
} -
{getRenderPropValue(content)}
- - ); -}; +const Overlay: React.FC = ({ title, content, prefixCls }) => ( + <> + {title &&
{getRenderPropValue(title)}
} +
{getRenderPropValue(content)}
+ +); const Popover = React.forwardRef((props, ref) => { const { @@ -49,6 +44,16 @@ const Popover = React.forwardRef((props, ref) => { const prefixCls = getPrefixCls('popover', customizePrefixCls); const rootPrefixCls = getPrefixCls(); + const mergedOverlay = React.useMemo(() => { + if (_overlay) { + return _overlay; + } + if (!title && !content) { + return null; + } + return ; + }, [_overlay, title, content, prefixCls]); + return ( ((props, ref) => { {...otherProps} prefixCls={prefixCls} ref={ref} - overlay={_overlay || } + overlay={mergedOverlay} transitionName={getTransitionName(rootPrefixCls, 'zoom-big', otherProps.transitionName)} /> ); diff --git a/package.json b/package.json index ef720c85f5..45e82f0bb3 100644 --- a/package.json +++ b/package.json @@ -223,7 +223,7 @@ "eslint-plugin-markdown": "^3.0.0", "eslint-plugin-react": "^7.31.8", "eslint-plugin-react-hooks": "^4.1.2", - "eslint-plugin-unicorn": "^44.0.0", + "eslint-plugin-unicorn": "^47.0.0", "fast-glob": "^3.2.11", "fetch-jsonp": "^1.1.3", "fs-extra": "^10.0.0",