From b452413506f851a0b7c8605cef14fe442893baf4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=98=BF=E5=9B=9B?= <1104464423@qq.com> Date: Tue, 16 Jul 2024 12:51:51 +0800 Subject: [PATCH] feat(antd): add form-item tooltip props support (#4144) Co-authored-by: xiaosi --- package.json | 1 + packages/antd/src/form-item/index.tsx | 25 +++++++++++++++++++++---- yarn.lock | 7 +++++++ 3 files changed, 29 insertions(+), 4 deletions(-) diff --git a/package.json b/package.json index a8b87269108..7ff74d41829 100644 --- a/package.json +++ b/package.json @@ -66,6 +66,7 @@ "@types/node": "^12.6.8", "@types/react": "^18.0.0", "@types/react-dom": "^18.0.0", + "@types/react-is": "^18.3.0", "@typescript-eslint/eslint-plugin": "^4.9.1", "@typescript-eslint/parser": "^4.8.2", "@umijs/plugin-sass": "^1.1.1", diff --git a/packages/antd/src/form-item/index.tsx b/packages/antd/src/form-item/index.tsx index daeea635313..a111ed74857 100644 --- a/packages/antd/src/form-item/index.tsx +++ b/packages/antd/src/form-item/index.tsx @@ -4,6 +4,7 @@ import { usePrefixCls, pickDataProps } from '../__builtins__' import { isVoidField } from '@formily/core' import { connect, mapProps } from '@formily/react' import { useFormLayout, FormLayoutShallowContext } from '../form-layout' +import { isElement } from 'react-is' import { Tooltip, Popover, ConfigProvider } from 'antd' import { QuestionCircleOutlined, @@ -18,7 +19,7 @@ export interface IFormItemProps { prefixCls?: string label?: React.ReactNode colon?: boolean - tooltip?: React.ReactNode + tooltip?: React.ReactNode | React.ComponentProps tooltipIcon?: React.ReactNode layout?: 'vertical' | 'horizontal' | 'inline' tooltipLayout?: 'icon' | 'text' @@ -55,6 +56,12 @@ type ComposeFormItem = React.FC> & { BaseItem?: React.FC> } +const isTooltipProps = ( + tooltip: React.ReactNode | React.ComponentProps +): tooltip is React.ComponentProps => { + return !isElement(tooltip) +} + const useFormItemLayout = (props: IFormItemProps) => { const layout = useFormLayout() const layoutType = props.layout ?? layout.layout ?? 'horizontal' @@ -219,16 +226,22 @@ export const BaseItem: React.FC> = ({ const gridStyles: React.CSSProperties = {} + const tooltipNode = isTooltipProps(tooltip) ? ( + + ) : ( + tooltip + ) + const getOverflowTooltip = () => { if (overflow) { return (
{label}
-
{tooltip}
+
{tooltipNode}
) } - return tooltip + return tooltipNode } const renderLabelText = () => { @@ -266,7 +279,11 @@ export const BaseItem: React.FC> = ({ if (tooltip && tooltipLayout === 'icon' && !overflow) { return ( - + {tooltipIcon} diff --git a/yarn.lock b/yarn.lock index b0f34253ccf..636f4f90ded 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3373,6 +3373,13 @@ dependencies: "@types/react" "*" +"@types/react-is@^18.3.0": + version "18.3.0" + resolved "https://registry.yarnpkg.com/@types/react-is/-/react-is-18.3.0.tgz#07008aecacf9c788f68e72eecca43701d7e6eefb" + integrity sha512-KZJpHUkAdzyKj/kUHJDc6N7KyidftICufJfOFpiG6haL/BDQNQt5i4n1XDUL/nDZAtGLHDSWRYpLzKTAKSvX6w== + dependencies: + "@types/react" "*" + "@types/react-router-config@5.0.2": version "5.0.2" resolved "https://registry.yarnpkg.com/@types/react-router-config/-/react-router-config-5.0.2.tgz#4d3b52e71ed363a1976a12321e67b09a99ad6d10"