From fd0ff5f225e508049819be11c4a429aa3c1156b5 Mon Sep 17 00:00:00 2001 From: linhf <1959099976@qq.com> Date: Tue, 1 Aug 2023 01:41:55 +0800 Subject: [PATCH 01/14] feat(Tooltip): Add close function --- packages/design/src/tooltip/demo/basic.tsx | 34 +++++++++-- packages/design/src/tooltip/index.md | 5 +- packages/design/src/tooltip/index.tsx | 69 ++++++++++++++++++++-- 3 files changed, 96 insertions(+), 12 deletions(-) diff --git a/packages/design/src/tooltip/demo/basic.tsx b/packages/design/src/tooltip/demo/basic.tsx index 316d1c725..9b98568a9 100644 --- a/packages/design/src/tooltip/demo/basic.tsx +++ b/packages/design/src/tooltip/demo/basic.tsx @@ -1,10 +1,32 @@ -import { Tooltip } from '@oceanbase/design'; +import { Space, Tooltip, Button } from '@oceanbase/design'; +import { CloseCircleTwoTone } from '@oceanbase/icons'; import React from 'react'; -const App: React.FC = () => ( - - Tooltip will show on mouse enter. - -); +const App: React.FC = () => { + const log = (e: React.MouseEvent) => { + console.log(e); + }; + const preventDefault = (e: React.MouseEvent) => { + e.preventDefault(); + console.log('Clicked! But prevent default.'); + }; + + return ( + + + + + + + + } onClose={log}> + + + + + + + ); +} export default App; diff --git a/packages/design/src/tooltip/index.md b/packages/design/src/tooltip/index.md index faf95f866..c3b8f56f1 100644 --- a/packages/design/src/tooltip/index.md +++ b/packages/design/src/tooltip/index.md @@ -12,7 +12,7 @@ nav: ## 代码演示 - + @@ -24,5 +24,8 @@ nav: | :-- | :-- | :-- | :-- | :-- | | type | 类型 | default \| light \| info \| success \| warning \| error | default | - | | mouseFollow | 是否跟随鼠标移动,开启后会去掉箭头,并且 `placement`、`open` 和 `trigger` 等属性也将失效 | boolean | false | - | +| closeIcon | 自定义关闭按钮 | boolean | ReactNode | false | - | +| closeTitle | 自定义关闭标题 | ReactNode | - | - | +| onClose | 关闭时的回调(可通过 e.preventDefault() 来阻止默认行为) | (e) => void | - | - | - 更多 API 详见 antd Tooltip 文档: https://ant.design/components/tooltip-cn diff --git a/packages/design/src/tooltip/index.tsx b/packages/design/src/tooltip/index.tsx index 1f19c3ed9..898a98355 100644 --- a/packages/design/src/tooltip/index.tsx +++ b/packages/design/src/tooltip/index.tsx @@ -1,8 +1,11 @@ -import { Tooltip as AntTooltip } from 'antd'; +import { Tooltip as AntTooltip, Space } from 'antd'; import type { TooltipPropsWithTitle as AntTooltipPropsWithTitle } from 'antd/es/tooltip'; -import React from 'react'; +import React, { useContext, useEffect, useMemo } from 'react'; +import { CloseCircleTwoTone, CloseOutlined } from '@oceanbase/icons'; import { token } from '../static-function'; import MouseTooltip from './MouseTooltip'; +import ConfigProvider from '../config-provider'; +import useToken from 'antd/lib/theme/useToken'; export * from 'antd/es/tooltip'; @@ -11,6 +14,9 @@ export type TooltipType = 'default' | 'light' | 'success' | 'info' | 'warning' | export interface TooltipProps extends AntTooltipPropsWithTitle { type?: TooltipType; mouseFollow?: boolean; + closeIcon?: boolean | React.ReactNode; + closeTitle?: React.ReactNode; + onClose?: (e: React.MouseEvent) => void; } export const getTooltipTypeList = () => [ @@ -52,12 +58,66 @@ const Tooltip: CompoundedComponent = ({ color, overlayInnerStyle, mouseFollow, + closeTitle, + closeIcon = false, + onClose, + title, ...restProps }) => { + const { getPrefixCls } = useContext(ConfigProvider.ConfigContext); + + const { prefixCls: customizePrefixCls } = restProps + const prefixCls = getPrefixCls('tooltip', customizePrefixCls); + + const [open, setOpen] = React.useState(undefined); + const token = useToken() + + useEffect(() => { + // 使用 open 关闭 tooltip 后,将 open 重新设置为 undefined 让 antd tooltip 继续接管展示逻辑 + if (open === false) { + setOpen(undefined) + } + }, [open]) + + const handleCloseClick = (e: React.MouseEvent) => { + e.stopPropagation(); + onClose?.(e); + + if (e.defaultPrevented) { + return; + } + setOpen(false); + }; + + const spaceStyle = { display: 'flex', justifyContent: 'space-between' } + const CloseIconNode = useMemo(() => { + if (closeIcon === false || closeIcon === null) { + return null + } + + const IconNode = closeIcon === true ? : + {closeIcon} + + return closeTitle ? + {closeTitle} + {IconNode} + : IconNode + }, [closeIcon]) + + const titleNode = typeof title === 'function' ? title() : title + const titleWithCloseIcon = [ + closeTitle && CloseIconNode, + !closeTitle ? + {titleNode} + {CloseIconNode} + : titleNode + ] + const typeList = getTooltipTypeList(); const typeItem = typeList.find(item => item.type === type); return mouseFollow ? ( ) : ( Date: Tue, 1 Aug 2023 01:49:53 +0800 Subject: [PATCH 02/14] fix: tooltip doc --- packages/design/src/tooltip/index.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/design/src/tooltip/index.md b/packages/design/src/tooltip/index.md index c3b8f56f1..c871e1b1c 100644 --- a/packages/design/src/tooltip/index.md +++ b/packages/design/src/tooltip/index.md @@ -24,7 +24,7 @@ nav: | :-- | :-- | :-- | :-- | :-- | | type | 类型 | default \| light \| info \| success \| warning \| error | default | - | | mouseFollow | 是否跟随鼠标移动,开启后会去掉箭头,并且 `placement`、`open` 和 `trigger` 等属性也将失效 | boolean | false | - | -| closeIcon | 自定义关闭按钮 | boolean | ReactNode | false | - | +| closeIcon | 自定义关闭按钮 | boolean \| ReactNode | false | - | | closeTitle | 自定义关闭标题 | ReactNode | - | - | | onClose | 关闭时的回调(可通过 e.preventDefault() 来阻止默认行为) | (e) => void | - | - | From 6127017f5c9b32cdedde796d6e675771f3b2a4f3 Mon Sep 17 00:00:00 2001 From: linhf <1959099976@qq.com> Date: Tue, 1 Aug 2023 01:50:59 +0800 Subject: [PATCH 03/14] fix: Accidental deletion --- packages/design/src/tooltip/index.tsx | 3 +++ 1 file changed, 3 insertions(+) diff --git a/packages/design/src/tooltip/index.tsx b/packages/design/src/tooltip/index.tsx index bb85ea5f0..49a1be2fe 100644 --- a/packages/design/src/tooltip/index.tsx +++ b/packages/design/src/tooltip/index.tsx @@ -143,6 +143,9 @@ const Tooltip: CompoundedComponent = ({ ); }; +if (process.env.NODE_ENV !== 'production') { + Tooltip.displayName = AntTooltip.displayName; +} Tooltip.__ANT_TOOLTIP = true; From af5aef3b23eea2dd841a6206b53473f2794724e2 Mon Sep 17 00:00:00 2001 From: linhf <1959099976@qq.com> Date: Tue, 1 Aug 2023 02:07:26 +0800 Subject: [PATCH 04/14] fix(Tooltip): Modify the display logic --- packages/design/src/tooltip/index.tsx | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/packages/design/src/tooltip/index.tsx b/packages/design/src/tooltip/index.tsx index 49a1be2fe..6730868f2 100644 --- a/packages/design/src/tooltip/index.tsx +++ b/packages/design/src/tooltip/index.tsx @@ -70,14 +70,6 @@ const Tooltip: CompoundedComponent = ({ const prefixCls = getPrefixCls('tooltip', customizePrefixCls); const [open, setOpen] = React.useState(undefined); - const token = useToken() - - useEffect(() => { - // 使用 open 关闭 tooltip 后,将 open 重新设置为 undefined 让 antd tooltip 继续接管展示逻辑 - if (open === false) { - setOpen(undefined) - } - }, [open]) const handleCloseClick = (e: React.MouseEvent) => { e.stopPropagation(); @@ -89,9 +81,10 @@ const Tooltip: CompoundedComponent = ({ setOpen(false); }; + const hasCloseIcon = !(closeIcon === false || closeIcon === null) const spaceStyle = { display: 'flex', justifyContent: 'space-between' } const CloseIconNode = useMemo(() => { - if (closeIcon === false || closeIcon === null) { + if (!hasCloseIcon) { return null } @@ -131,6 +124,11 @@ const Tooltip: CompoundedComponent = ({ { + if (v && hasCloseIcon) { + setOpen(v) + } + }} color={color || typeItem?.backgroundColor} overlayInnerStyle={{ color: typeItem?.color, From 6672eb862efaf5679796a3a752272bc6975b1f73 Mon Sep 17 00:00:00 2001 From: linhf <1959099976@qq.com> Date: Sun, 6 Aug 2023 18:34:00 +0800 Subject: [PATCH 05/14] chore: Remove closeTitle & Add style --- packages/design/src/tooltip/index.tsx | 52 +++++++++------------- packages/design/src/tooltip/style/index.ts | 28 ++++++++++++ 2 files changed, 48 insertions(+), 32 deletions(-) create mode 100644 packages/design/src/tooltip/style/index.ts diff --git a/packages/design/src/tooltip/index.tsx b/packages/design/src/tooltip/index.tsx index 6730868f2..3b169dc22 100644 --- a/packages/design/src/tooltip/index.tsx +++ b/packages/design/src/tooltip/index.tsx @@ -1,11 +1,12 @@ import { Tooltip as AntTooltip, Space } from 'antd'; import type { TooltipPropsWithTitle as AntTooltipPropsWithTitle } from 'antd/es/tooltip'; -import React, { useContext, useEffect, useMemo } from 'react'; -import { CloseCircleTwoTone, CloseOutlined } from '@oceanbase/icons'; +import React, { useContext, useMemo } from 'react'; +import { CloseOutlined } from '@oceanbase/icons'; import { token } from '../static-function'; import MouseTooltip from './MouseTooltip'; import ConfigProvider from '../config-provider'; -import useToken from 'antd/lib/theme/useToken'; +import useStyle from './style'; +import classNames from 'classnames'; export * from 'antd/es/tooltip'; @@ -15,7 +16,6 @@ export interface TooltipProps extends AntTooltipPropsWithTitle { type?: TooltipType; mouseFollow?: boolean; closeIcon?: boolean | React.ReactNode; - closeTitle?: React.ReactNode; onClose?: (e: React.MouseEvent) => void; } @@ -58,57 +58,49 @@ const Tooltip: CompoundedComponent = ({ color, overlayInnerStyle, mouseFollow, - closeTitle, closeIcon = false, onClose, title, + className, ...restProps }) => { const { getPrefixCls } = useContext(ConfigProvider.ConfigContext); const { prefixCls: customizePrefixCls } = restProps const prefixCls = getPrefixCls('tooltip', customizePrefixCls); + const { wrapSSR, hashId } = useStyle(prefixCls); - const [open, setOpen] = React.useState(undefined); + const tooltipCls = classNames(className, hashId); const handleCloseClick = (e: React.MouseEvent) => { e.stopPropagation(); onClose?.(e); - - if (e.defaultPrevented) { - return; - } - setOpen(false); }; - const hasCloseIcon = !(closeIcon === false || closeIcon === null) - const spaceStyle = { display: 'flex', justifyContent: 'space-between' } + const hasCloseIcon = !!closeIcon const CloseIconNode = useMemo(() => { if (!hasCloseIcon) { return null } - const IconNode = closeIcon === true ? : + const IconNode = closeIcon === true ? : {closeIcon} - return closeTitle ? - {closeTitle} - {IconNode} - : IconNode + return IconNode }, [closeIcon]) const titleNode = typeof title === 'function' ? title() : title - const titleWithCloseIcon = [ - closeTitle && CloseIconNode, - !closeTitle ? + const titleWithCloseIcon = ( + {titleNode} {CloseIconNode} - : titleNode - ] + + ) + const typeList = getTooltipTypeList(); const typeItem = typeList.find(item => item.type === type); - return mouseFollow ? ( + return wrapSSR(mouseFollow ? ( {children} ) : ( { - if (v && hasCloseIcon) { - setOpen(v) - } - }} + title={hasCloseIcon ? titleWithCloseIcon : title} color={color || typeItem?.backgroundColor} overlayInnerStyle={{ color: typeItem?.color, ...overlayInnerStyle, }} + className={tooltipCls} {...restProps} > {children} - ); + )); }; if (process.env.NODE_ENV !== 'production') { diff --git a/packages/design/src/tooltip/style/index.ts b/packages/design/src/tooltip/style/index.ts new file mode 100644 index 000000000..59873dd07 --- /dev/null +++ b/packages/design/src/tooltip/style/index.ts @@ -0,0 +1,28 @@ +import type { CSSObject } from '@ant-design/cssinjs'; +import type { FullToken, GenerateStyle } from 'antd/es/theme/internal'; +import { genComponentStyleHook } from '../../_util/genComponentStyleHook'; + +export type TooltipToken = FullToken<'Tooltip'>; + +export const genTooltipStyle: GenerateStyle = (token: TooltipToken): CSSObject => { + const { componentCls, colorInfo, colorInfoBg, colorTextSecondary } = token; + return { + [componentCls]: { + [`&${componentCls}-close-icon`]: { + cursor: 'pointer', + }, + [`${componentCls}-close-icon-wrap`]: { + display: 'flex', + justifyContent: 'space-between', alignItems: 'flex-start', + wordBreak: 'break-all' + }, + }, + }; +}; + +export default (prefixCls: string) => { + const useStyle = genComponentStyleHook('Tabs', token => { + return [genTooltipStyle(token)]; + }); + return useStyle(prefixCls); +}; From 6a0eb63b35a1181f19026718381304aba4d2c368 Mon Sep 17 00:00:00 2001 From: linhf <1959099976@qq.com> Date: Sun, 6 Aug 2023 18:34:38 +0800 Subject: [PATCH 06/14] docs: update doc and demo --- packages/design/src/tooltip/demo/basic.tsx | 34 ++++--------------- .../design/src/tooltip/demo/close-icon.tsx | 33 ++++++++++++++++++ packages/design/src/tooltip/index.md | 6 +++- 3 files changed, 44 insertions(+), 29 deletions(-) create mode 100644 packages/design/src/tooltip/demo/close-icon.tsx diff --git a/packages/design/src/tooltip/demo/basic.tsx b/packages/design/src/tooltip/demo/basic.tsx index 9b98568a9..316d1c725 100644 --- a/packages/design/src/tooltip/demo/basic.tsx +++ b/packages/design/src/tooltip/demo/basic.tsx @@ -1,32 +1,10 @@ -import { Space, Tooltip, Button } from '@oceanbase/design'; -import { CloseCircleTwoTone } from '@oceanbase/icons'; +import { Tooltip } from '@oceanbase/design'; import React from 'react'; -const App: React.FC = () => { - const log = (e: React.MouseEvent) => { - console.log(e); - }; +const App: React.FC = () => ( + + Tooltip will show on mouse enter. + +); - const preventDefault = (e: React.MouseEvent) => { - e.preventDefault(); - console.log('Clicked! But prevent default.'); - }; - - return ( - - - - - - - - } onClose={log}> - - - - - - - ); -} export default App; diff --git a/packages/design/src/tooltip/demo/close-icon.tsx b/packages/design/src/tooltip/demo/close-icon.tsx new file mode 100644 index 000000000..4b765c53a --- /dev/null +++ b/packages/design/src/tooltip/demo/close-icon.tsx @@ -0,0 +1,33 @@ +import { Space, Tooltip, Button } from '@oceanbase/design'; +import { CloseCircleOutlined } from '@oceanbase/icons'; +import React, { useState } from 'react'; + +const App: React.FC = () => { + const [open, setOpen] = useState(true) + const log = (e: React.MouseEvent) => { + console.log(e); + }; + + return ( + + { + setOpen(false) + }} + onOpenChange={(v) => { + if (v) { + setOpen(v) + } + }} + > + + + } onClose={log}> + + + + ); +} +export default App; diff --git a/packages/design/src/tooltip/index.md b/packages/design/src/tooltip/index.md index c871e1b1c..0f26e637c 100644 --- a/packages/design/src/tooltip/index.md +++ b/packages/design/src/tooltip/index.md @@ -9,10 +9,14 @@ nav: - 💄 定制主题和样式,符合 OceanBase Design 设计规范。 - 🆕 新增 `type` 属性,支持 `default`、`light`、`info`、`success`、`warning` 和 `error` 五种类型的 Tooltip。 - 🆕 新增 `mouseFollow` 属性,支持鼠标跟随。 +- 🆕 新增 `closeIcon` 属性,支持展示关闭按钮。 +- 🆕 新增 `onClose` 属性,关闭按钮被点击时调用此函数,可以配合 `open` 和 `onOpenChange` 属性来控制 `Tooltip` 展示。 ## 代码演示 - + + + From 497b0a39a6ed85aeb8121352705b4b0b3596a125 Mon Sep 17 00:00:00 2001 From: linhf <1959099976@qq.com> Date: Sun, 6 Aug 2023 18:44:41 +0800 Subject: [PATCH 07/14] chore: fix style --- packages/design/src/tooltip/index.tsx | 4 +--- packages/design/src/tooltip/style/index.ts | 18 ++++++++++-------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/packages/design/src/tooltip/index.tsx b/packages/design/src/tooltip/index.tsx index 3b169dc22..e2116b844 100644 --- a/packages/design/src/tooltip/index.tsx +++ b/packages/design/src/tooltip/index.tsx @@ -83,10 +83,9 @@ const Tooltip: CompoundedComponent = ({ return null } - const IconNode = closeIcon === true ? : + return closeIcon === true ? : {closeIcon} - return IconNode }, [closeIcon]) const titleNode = typeof title === 'function' ? title() : title @@ -97,7 +96,6 @@ const Tooltip: CompoundedComponent = ({ ) - const typeList = getTooltipTypeList(); const typeItem = typeList.find(item => item.type === type); return wrapSSR(mouseFollow ? ( diff --git a/packages/design/src/tooltip/style/index.ts b/packages/design/src/tooltip/style/index.ts index 59873dd07..fde965505 100644 --- a/packages/design/src/tooltip/style/index.ts +++ b/packages/design/src/tooltip/style/index.ts @@ -3,25 +3,27 @@ import type { FullToken, GenerateStyle } from 'antd/es/theme/internal'; import { genComponentStyleHook } from '../../_util/genComponentStyleHook'; export type TooltipToken = FullToken<'Tooltip'>; - export const genTooltipStyle: GenerateStyle = (token: TooltipToken): CSSObject => { - const { componentCls, colorInfo, colorInfoBg, colorTextSecondary } = token; + const { componentCls } = token; + console.log(componentCls, 'componentCls') + return { [componentCls]: { - [`&${componentCls}-close-icon`]: { - cursor: 'pointer', - }, [`${componentCls}-close-icon-wrap`]: { display: 'flex', - justifyContent: 'space-between', alignItems: 'flex-start', - wordBreak: 'break-all' + justifyContent: 'space-between', + alignItems: 'flex-start', + wordBreak: 'break-all', + [`${componentCls}-close-icon`]: { + cursor: 'pointer', + }, }, }, }; }; export default (prefixCls: string) => { - const useStyle = genComponentStyleHook('Tabs', token => { + const useStyle = genComponentStyleHook('Tooltip', token => { return [genTooltipStyle(token)]; }); return useStyle(prefixCls); From 01ca16b7bcb9699dc85f764a755050ba1297a801 Mon Sep 17 00:00:00 2001 From: linhf <1959099976@qq.com> Date: Mon, 21 Aug 2023 01:03:44 +0800 Subject: [PATCH 08/14] chore(Tooltip): Controlled logic --- packages/design/src/tooltip/demo/close-icon.tsx | 6 ++++++ packages/design/src/tooltip/index.tsx | 17 ++++++++++++++++- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/packages/design/src/tooltip/demo/close-icon.tsx b/packages/design/src/tooltip/demo/close-icon.tsx index 4b765c53a..3cd36ad9c 100644 --- a/packages/design/src/tooltip/demo/close-icon.tsx +++ b/packages/design/src/tooltip/demo/close-icon.tsx @@ -10,6 +10,12 @@ const App: React.FC = () => { return ( + + + + { const { getPrefixCls } = useContext(ConfigProvider.ConfigContext); @@ -71,10 +73,19 @@ const Tooltip: CompoundedComponent = ({ const { wrapSSR, hashId } = useStyle(prefixCls); const tooltipCls = classNames(className, hashId); + const [innerOpen, setInnerOpen] = useState(undefined) + + const open = isNil(propOpen) ? innerOpen : propOpen const handleCloseClick = (e: React.MouseEvent) => { e.stopPropagation(); onClose?.(e); + + if (e.defaultPrevented) { + return; + } + + setInnerOpen(false); }; const hasCloseIcon = !!closeIcon @@ -115,6 +126,10 @@ const Tooltip: CompoundedComponent = ({ { + setInnerOpen(open) + }} overlayInnerStyle={{ color: typeItem?.color, ...overlayInnerStyle, From d530b2a06f0f4ec42e950279bc4b3c0eb414bd88 Mon Sep 17 00:00:00 2001 From: linhf <1959099976@qq.com> Date: Mon, 21 Aug 2023 01:06:04 +0800 Subject: [PATCH 09/14] chore: demo --- packages/design/src/tooltip/demo/close-icon.tsx | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/packages/design/src/tooltip/demo/close-icon.tsx b/packages/design/src/tooltip/demo/close-icon.tsx index 3cd36ad9c..808a77a8f 100644 --- a/packages/design/src/tooltip/demo/close-icon.tsx +++ b/packages/design/src/tooltip/demo/close-icon.tsx @@ -23,9 +23,7 @@ const App: React.FC = () => { setOpen(false) }} onOpenChange={(v) => { - if (v) { - setOpen(v) - } + setOpen(v) }} > From 427f5c31fc1edba8e0cc2f16ece03482c8198cb1 Mon Sep 17 00:00:00 2001 From: linhf <1959099976@qq.com> Date: Mon, 21 Aug 2023 01:08:54 +0800 Subject: [PATCH 10/14] docs: tooltip demo --- packages/design/src/tooltip/index.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/design/src/tooltip/index.md b/packages/design/src/tooltip/index.md index 0f26e637c..5aebb889b 100644 --- a/packages/design/src/tooltip/index.md +++ b/packages/design/src/tooltip/index.md @@ -27,7 +27,7 @@ nav: | 参数 | 说明 | 类型 | 默认值 | 版本 | | :-- | :-- | :-- | :-- | :-- | | type | 类型 | default \| light \| info \| success \| warning \| error | default | - | -| mouseFollow | 是否跟随鼠标移动,开启后会去掉箭头,并且 `placement`、`open` 和 `trigger` 等属性也将失效 | boolean | false | - | +| mouseFollow | 是否跟随鼠标移动,开启后会去掉箭头,并且 `placement`、`open`、`closeIcon` 和 `trigger` 等属性也将失效 | boolean | false | - | | closeIcon | 自定义关闭按钮 | boolean \| ReactNode | false | - | | closeTitle | 自定义关闭标题 | ReactNode | - | - | | onClose | 关闭时的回调(可通过 e.preventDefault() 来阻止默认行为) | (e) => void | - | - | From 60b9940d4eb735ae3e323d1964acc68fb825b5bf Mon Sep 17 00:00:00 2001 From: linhf <1959099976@qq.com> Date: Mon, 21 Aug 2023 01:17:01 +0800 Subject: [PATCH 11/14] docs: Type column setting magenta7 color --- .dumi/theme/common/styles/Markdown.tsx | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/.dumi/theme/common/styles/Markdown.tsx b/.dumi/theme/common/styles/Markdown.tsx index f6c8bac06..811e22a78 100644 --- a/.dumi/theme/common/styles/Markdown.tsx +++ b/.dumi/theme/common/styles/Markdown.tsx @@ -324,6 +324,12 @@ const GlobalStyle: React.FC = () => { max-width: unset; } } + + td { + &:nth-child(3) { + color: ${token.magenta7}; + } + } th { color: #5c6b77; From 588b401215aa9f9b4bbc181cd02576ee129bdb82 Mon Sep 17 00:00:00 2001 From: linhf <1959099976@qq.com> Date: Mon, 28 Aug 2023 00:03:15 +0800 Subject: [PATCH 12/14] chore: test code --- packages/design/src/config-provider/index.tsx | 15 ++++++------ .../src/tooltip/__tests__/index.test.tsx | 24 +++++++++++++++++++ packages/design/src/tooltip/style/index.ts | 11 +++++---- tests/util.ts | 22 +++++++++++++++++ 4 files changed, 60 insertions(+), 12 deletions(-) create mode 100644 packages/design/src/tooltip/__tests__/index.test.tsx create mode 100644 tests/util.ts diff --git a/packages/design/src/config-provider/index.tsx b/packages/design/src/config-provider/index.tsx index 8fc58c955..c8758a20b 100644 --- a/packages/design/src/config-provider/index.tsx +++ b/packages/design/src/config-provider/index.tsx @@ -58,14 +58,14 @@ const ConfigProvider = ({ children, theme, navigate, spin, ...restProps }: Confi // Because defaultThemeToken is designed for light theme token: theme?.isDark ? { - ...defaultSeed, - ...theme?.token, - } + ...defaultSeed, + ...theme?.token, + } : { - ...defaultSeed, - ...defaultThemeToken, - ...theme?.token, - }, + ...defaultSeed, + ...defaultThemeToken, + ...theme?.token, + }, components: { ...components, ...theme?.components, @@ -94,7 +94,6 @@ const ConfigProvider = ({ children, theme, navigate, spin, ...restProps }: Confi ConfigProvider.ConfigContext = AntConfigProvider.ConfigContext as React.Context; ConfigProvider.ExtendedConfigContext = ExtendedConfigContext; -ConfigProvider.SizeContext = AntConfigProvider.SizeContext; ConfigProvider.config = AntConfigProvider.config; ConfigProvider.useConfig = AntConfigProvider.useConfig; diff --git a/packages/design/src/tooltip/__tests__/index.test.tsx b/packages/design/src/tooltip/__tests__/index.test.tsx new file mode 100644 index 000000000..e91239fe1 --- /dev/null +++ b/packages/design/src/tooltip/__tests__/index.test.tsx @@ -0,0 +1,24 @@ +import React from 'react'; +import { render, fireEvent } from '@testing-library/react'; +import { Tooltip } from '@oceanbase/design'; +import { waitFakeTimer } from '../../../../../tests/util' + +describe('Tooltip', () => { + it('default close icon should render correctly', async () => { + const { container, asFragment } = render( +
Hello world!
+
); + + const divElement = container.querySelector('#hello'); + fireEvent.mouseEnter(divElement!); + waitFakeTimer() + expect(container.querySelectorAll('.ant-tooltip-close-icon').length).toBe(1); + expect(container.querySelector('.ant-tooltip-hidden')).toBeNull() + + // After clicking the close icon, the tooltip disappears. + fireEvent.click(container.querySelector('.ant-tooltip-close-icon')) + expect(container.querySelector('.ant-tooltip-hidden')).not.toBeNull() + }); +}); diff --git a/packages/design/src/tooltip/style/index.ts b/packages/design/src/tooltip/style/index.ts index fde965505..0b36c271f 100644 --- a/packages/design/src/tooltip/style/index.ts +++ b/packages/design/src/tooltip/style/index.ts @@ -1,11 +1,11 @@ import type { CSSObject } from '@ant-design/cssinjs'; -import type { FullToken, GenerateStyle } from 'antd/es/theme/internal'; +import type { FullToken, AliasToken, GenerateStyle } from 'antd/es/theme/internal'; import { genComponentStyleHook } from '../../_util/genComponentStyleHook'; export type TooltipToken = FullToken<'Tooltip'>; + export const genTooltipStyle: GenerateStyle = (token: TooltipToken): CSSObject => { const { componentCls } = token; - console.log(componentCls, 'componentCls') return { [componentCls]: { @@ -24,7 +24,10 @@ export const genTooltipStyle: GenerateStyle = (token: TooltipToken export default (prefixCls: string) => { const useStyle = genComponentStyleHook('Tooltip', token => { - return [genTooltipStyle(token)]; - }); + return [genTooltipStyle(token as TooltipToken)]; + }, ({ zIndexPopupBase, colorBgSpotlight }) => ({ + zIndexPopup: zIndexPopupBase + 70, + colorBgDefault: colorBgSpotlight, + }),); return useStyle(prefixCls); }; diff --git a/tests/util.ts b/tests/util.ts new file mode 100644 index 000000000..7d09ea60c --- /dev/null +++ b/tests/util.ts @@ -0,0 +1,22 @@ +import { act } from '@testing-library/react'; + +/** + * Wait for a time delay. Will wait `advanceTime * times` ms. + * + * @param advanceTime Default 1000 + * @param times Default 20 + */ +export async function waitFakeTimer(advanceTime = 1000, times = 20) { + for (let i = 0; i < times; i += 1) { + // eslint-disable-next-line no-await-in-loop + await act(async () => { + await Promise.resolve(); + + if (advanceTime > 0) { + jest.advanceTimersByTime(advanceTime); + } else { + jest.runAllTimers(); + } + }); + } +} \ No newline at end of file From d81288eb39f959ec40f4db8f120c64ee1576355e Mon Sep 17 00:00:00 2001 From: linhf Date: Wed, 30 Aug 2023 14:41:17 +0800 Subject: [PATCH 13/14] fix(ConfigProvider): Add SizeContext carry out compatibility --- packages/design/src/config-provider/index.tsx | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/packages/design/src/config-provider/index.tsx b/packages/design/src/config-provider/index.tsx index c8758a20b..8fc58c955 100644 --- a/packages/design/src/config-provider/index.tsx +++ b/packages/design/src/config-provider/index.tsx @@ -58,14 +58,14 @@ const ConfigProvider = ({ children, theme, navigate, spin, ...restProps }: Confi // Because defaultThemeToken is designed for light theme token: theme?.isDark ? { - ...defaultSeed, - ...theme?.token, - } + ...defaultSeed, + ...theme?.token, + } : { - ...defaultSeed, - ...defaultThemeToken, - ...theme?.token, - }, + ...defaultSeed, + ...defaultThemeToken, + ...theme?.token, + }, components: { ...components, ...theme?.components, @@ -94,6 +94,7 @@ const ConfigProvider = ({ children, theme, navigate, spin, ...restProps }: Confi ConfigProvider.ConfigContext = AntConfigProvider.ConfigContext as React.Context; ConfigProvider.ExtendedConfigContext = ExtendedConfigContext; +ConfigProvider.SizeContext = AntConfigProvider.SizeContext; ConfigProvider.config = AntConfigProvider.config; ConfigProvider.useConfig = AntConfigProvider.useConfig; From bf2f702dc07bed79e7296db70303e0b096526762 Mon Sep 17 00:00:00 2001 From: linhf Date: Wed, 30 Aug 2023 15:39:32 +0800 Subject: [PATCH 14/14] feat(tooltip): add test --- .../src/tooltip/__tests__/index.test.tsx | 69 ++++++++++++++++--- 1 file changed, 58 insertions(+), 11 deletions(-) diff --git a/packages/design/src/tooltip/__tests__/index.test.tsx b/packages/design/src/tooltip/__tests__/index.test.tsx index e91239fe1..a974ea529 100644 --- a/packages/design/src/tooltip/__tests__/index.test.tsx +++ b/packages/design/src/tooltip/__tests__/index.test.tsx @@ -1,24 +1,71 @@ import React from 'react'; import { render, fireEvent } from '@testing-library/react'; import { Tooltip } from '@oceanbase/design'; -import { waitFakeTimer } from '../../../../../tests/util' +import { waitFakeTimer } from '../../../../../tests/util'; +import { CloseCircleOutlined } from '@oceanbase/icons'; describe('Tooltip', () => { + beforeEach(() => { + jest.useFakeTimers(); + }); + afterEach(() => { + jest.useRealTimers(); + jest.clearAllTimers(); + }); + it('default close icon should render correctly', async () => { - const { container, asFragment } = render( -
Hello world!
-
); + const { container } = render( + +
Hello world!
+
+ ); const divElement = container.querySelector('#hello'); fireEvent.mouseEnter(divElement!); - waitFakeTimer() - expect(container.querySelectorAll('.ant-tooltip-close-icon').length).toBe(1); - expect(container.querySelector('.ant-tooltip-hidden')).toBeNull() + await waitFakeTimer(); + expect(container.querySelector('.ant-tooltip-open')).not.toBeNull(); + expect(document.querySelectorAll('.ant-tooltip-close-icon').length).toBe(1); // After clicking the close icon, the tooltip disappears. - fireEvent.click(container.querySelector('.ant-tooltip-close-icon')) - expect(container.querySelector('.ant-tooltip-hidden')).not.toBeNull() + fireEvent.click(document.querySelector('.ant-tooltip-close-icon')); + await waitFakeTimer(); + expect(container.querySelector('.ant-tooltip-open')).toBeNull(); + }); + + it('custom close icon should render correctly', async () => { + const { container } = render( + }> +
Hello world!
+
+ ); + + const divElement = container.querySelector('#hello'); + fireEvent.mouseEnter(divElement!); + await waitFakeTimer(); + expect(document.querySelectorAll('.anticon-close-circle').length).toBe(1); + + // After clicking the close icon, the tooltip disappears. + fireEvent.click(document.querySelector('.ant-tooltip-close-icon')); + await waitFakeTimer(); + expect(container.querySelector('.ant-tooltip-open')).toBeNull(); + }); + + it('check `onOpenChange` arguments', async () => { + const onClose = jest.fn(); + const { container } = render( + +
Hello world!
+
+ ); + + const divElement = container.querySelector('#hello'); + fireEvent.mouseEnter(divElement!); + await waitFakeTimer(); + + fireEvent.click(document.querySelector('.ant-tooltip-close-icon')); + expect(onClose).toHaveBeenCalled(); + + await waitFakeTimer(); + expect(container.querySelector('.ant-tooltip-open')).toBeNull(); }); });