From 827ada2da62a2c1ea5ae5956bf2c5d9c6af2c92b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?kiner-tang=28=E6=96=87=E8=BE=89=29?= <1127031143@qq.com> Date: Tue, 13 Jun 2023 14:38:18 +0800 Subject: [PATCH] refactor(Alert): refactor closeIcon (#42962) * refactor: refactor closeIcon * feat: update test case * feat: optimize code * feat: remove demo about closeText * feat: remove demo about closeText * feat: optimize code * feat: optimize code * feat: optimize code * feat: optimize code * feat: optimize code * feat: optimize code * feat: optimize code * feat: optimize code --- .../__snapshots__/demo-extend.test.ts.snap | 29 -------------- .../__tests__/__snapshots__/demo.test.ts.snap | 29 -------------- components/alert/__tests__/index.test.tsx | 27 +++++++++++++ components/alert/demo/closable.tsx | 6 +-- components/alert/demo/close-text.md | 7 ---- components/alert/demo/close-text.tsx | 6 --- components/alert/index.en-US.md | 5 +-- components/alert/index.tsx | 40 +++++++++++++------ components/alert/index.zh-CN.md | 5 +-- 9 files changed, 60 insertions(+), 94 deletions(-) delete mode 100644 components/alert/demo/close-text.md delete mode 100644 components/alert/demo/close-text.tsx diff --git a/components/alert/__tests__/__snapshots__/demo-extend.test.ts.snap b/components/alert/__tests__/__snapshots__/demo-extend.test.ts.snap index 3d84b37e58..b172abd57f 100644 --- a/components/alert/__tests__/__snapshots__/demo-extend.test.ts.snap +++ b/components/alert/__tests__/__snapshots__/demo-extend.test.ts.snap @@ -576,35 +576,6 @@ exports[`renders components/alert/demo/closable.tsx extend context correctly 1`] `; -exports[`renders components/alert/demo/close-text.tsx extend context correctly 1`] = ` - -`; - exports[`renders components/alert/demo/custom-icon.tsx extend context correctly 1`] = `
`; -exports[`renders components/alert/demo/close-text.tsx correctly 1`] = ` - -`; - exports[`renders components/alert/demo/custom-icon.tsx correctly 1`] = `
{ const { container } = render(); expect(!!container.querySelector('.ant-alert-message')).toBe(false); }); + + it('close button should be hidden when closeIcon setting to null or false', () => { + const { container, rerender } = render(); + expect(container.querySelector('.ant-alert-close-icon')).toBeFalsy(); + rerender(); + expect(container.querySelector('.ant-alert-close-icon')).toBeFalsy(); + rerender(); + expect(container.querySelector('.ant-alert-close-icon')).toBeTruthy(); + rerender(); + expect(container.querySelector('.ant-alert-close-icon')).toBeFalsy(); + }); + + it('should warning when using closeText', () => { + resetWarned(); + const warnSpy = jest.spyOn(console, 'error').mockImplementation(() => {}); + + const { container } = render(); + + expect(warnSpy).toHaveBeenCalledWith( + `Warning: [antd: Alert] \`closeText\` is deprecated. Please use \`closeIcon\` instead.`, + ); + + expect(container.querySelector('.ant-alert-close-icon')?.textContent).toBe('close'); + + warnSpy.mockRestore(); + }); }); diff --git a/components/alert/demo/closable.tsx b/components/alert/demo/closable.tsx index 2d04f4ebaf..2edfc24ec2 100644 --- a/components/alert/demo/closable.tsx +++ b/components/alert/demo/closable.tsx @@ -1,5 +1,5 @@ -import React from 'react'; import { Alert, Space } from 'antd'; +import React from 'react'; const onClose = (e: React.MouseEvent) => { console.log(e, 'I was closed.'); @@ -10,14 +10,14 @@ const App: React.FC = () => ( diff --git a/components/alert/demo/close-text.md b/components/alert/demo/close-text.md deleted file mode 100644 index 969bff8f4c..0000000000 --- a/components/alert/demo/close-text.md +++ /dev/null @@ -1,7 +0,0 @@ -## zh-CN - -可以自定义关闭,自定义的文字会替换原先的关闭 `Icon`。 - -## en-US - -Replace the default icon with customized text. diff --git a/components/alert/demo/close-text.tsx b/components/alert/demo/close-text.tsx deleted file mode 100644 index f5517f7247..0000000000 --- a/components/alert/demo/close-text.tsx +++ /dev/null @@ -1,6 +0,0 @@ -import React from 'react'; -import { Alert } from 'antd'; - -const App: React.FC = () => ; - -export default App; diff --git a/components/alert/index.en-US.md b/components/alert/index.en-US.md index 5b66c657a9..498be9f955 100644 --- a/components/alert/index.en-US.md +++ b/components/alert/index.en-US.md @@ -25,7 +25,6 @@ Alert component for feedback. Closable Description Icon -Customized Close Text Banner Loop Banner Smoothly Unmount @@ -40,9 +39,7 @@ Alert component for feedback. | action | The action of Alert | ReactNode | - | 4.9.0 | | afterClose | Called when close animation is finished | () => void | - | | | banner | Whether to show as banner | boolean | false | | -| closable | Whether Alert can be closed | boolean | - | | -| closeText | Close text to show | ReactNode | - | | -| closeIcon | Custom close icon | ReactNode | `` | 4.18.0 | +| closeIcon | Custom close icon, >=5.7.0: close button will be hidden when setting to `null` or `false` | boolean \| ReactNode | `` | | | description | Additional content of Alert | ReactNode | - | | | icon | Custom icon, effective when `showIcon` is true | ReactNode | - | | | message | Content of Alert | ReactNode | - | | diff --git a/components/alert/index.tsx b/components/alert/index.tsx index 1af120f207..60b7c0fcd7 100644 --- a/components/alert/index.tsx +++ b/components/alert/index.tsx @@ -9,6 +9,7 @@ import pickAttrs from 'rc-util/lib/pickAttrs'; import type { ReactElement } from 'react'; import * as React from 'react'; import { replaceElement } from '../_util/reactNode'; +import warning from '../_util/warning'; import { ConfigContext } from '../config-provider'; import ErrorBoundary from './ErrorBoundary'; @@ -20,7 +21,10 @@ export interface AlertProps { type?: 'success' | 'info' | 'warning' | 'error'; /** Whether Alert can be closed */ closable?: boolean; - /** Close text to show */ + /** + * @deprecated please use `closeIcon` instead. + * Close text to show + */ closeText?: React.ReactNode; /** Content of Alert */ message?: React.ReactNode; @@ -41,7 +45,7 @@ export interface AlertProps { banner?: boolean; icon?: React.ReactNode; /** Custom closeIcon */ - closeIcon?: React.ReactNode; + closeIcon?: boolean | React.ReactNode; action?: React.ReactNode; onMouseEnter?: React.MouseEventHandler; onMouseLeave?: React.MouseEventHandler; @@ -78,16 +82,17 @@ const IconNode: React.FC = (props) => { interface CloseIconProps { isClosable: boolean; prefixCls: AlertProps['prefixCls']; - closeText: AlertProps['closeText']; closeIcon: AlertProps['closeIcon']; handleClose: AlertProps['onClose']; } const CloseIcon: React.FC = (props) => { - const { isClosable, closeText, prefixCls, closeIcon, handleClose } = props; + const { isClosable, prefixCls, closeIcon, handleClose } = props; + const mergedCloseIcon = + closeIcon === true || closeIcon === undefined ? : closeIcon; return isClosable ? ( ) : null; }; @@ -111,12 +116,14 @@ const Alert: CompoundedComponent = ({ showIcon, closable, closeText, - closeIcon = , + closeIcon, action, ...props }) => { const [closed, setClosed] = React.useState(false); - + if (process.env.NODE_ENV !== 'production') { + warning(!closeText, 'Alert', '`closeText` is deprecated. Please use `closeIcon` instead.'); + } const ref = React.useRef(null); const { getPrefixCls, direction } = React.useContext(ConfigContext); const prefixCls = getPrefixCls('alert', customizePrefixCls); @@ -136,8 +143,18 @@ const Alert: CompoundedComponent = ({ return banner ? 'warning' : 'info'; }; - // closeable when closeText is assigned - const isClosable = closeText ? true : closable; + // closeable when closeText or closeIcon is assigned + const isClosable = React.useMemo(() => { + if (closeText) { + return true; + } + if (typeof closable === 'boolean') { + return closable; + } + // should be true when closeIcon is 0 or '' + return closeIcon !== false && closeIcon !== null && closeIcon !== undefined; + }, [closeText, closeIcon, closable]); + const type = getType(); // banner mode defaults to Icon @@ -199,10 +216,9 @@ const Alert: CompoundedComponent = ({
{action ?
{action}
: null}
diff --git a/components/alert/index.zh-CN.md b/components/alert/index.zh-CN.md index 8dd34e0f44..bb5a878720 100644 --- a/components/alert/index.zh-CN.md +++ b/components/alert/index.zh-CN.md @@ -26,7 +26,6 @@ group: 可关闭的警告提示 含有辅助性文字介绍 图标 -自定义关闭 顶部公告 轮播的公告 平滑地卸载 @@ -41,9 +40,7 @@ group: | action | 自定义操作项 | ReactNode | - | 4.9.0 | | afterClose | 关闭动画结束后触发的回调函数 | () => void | - | | | banner | 是否用作顶部公告 | boolean | false | | -| closable | 默认不显示关闭按钮 | boolean | - | | -| closeText | 自定义关闭按钮 | ReactNode | - | | -| closeIcon | 自定义关闭 Icon | ReactNode | `` | 4.18.0 | +| closeIcon | 自定义关闭 Icon,>=5.7.0: 设置为 `null` 或 `false` 时隐藏关闭按钮 | boolean \| ReactNode | `` | | | description | 警告提示的辅助性文字介绍 | ReactNode | - | | | icon | 自定义图标,`showIcon` 为 true 时有效 | ReactNode | - | | | message | 警告提示内容 | ReactNode | - | |