diff --git a/packages/antd/__tests__/tooltip.spec.tsx b/packages/antd/__tests__/tooltip.spec.tsx new file mode 100644 index 00000000000..fd598b8bf49 --- /dev/null +++ b/packages/antd/__tests__/tooltip.spec.tsx @@ -0,0 +1,82 @@ +import React from 'react' +import { render } from '@testing-library/react' +import { FormItem } from '../src/form-item' +import { isElement } from 'react-is' + +// Test the actual isTooltipProps function implementation +const isTooltipProps = (tooltip: any): boolean => { + return !!( + tooltip && + typeof tooltip === 'object' && + !isElement(tooltip) && + !Array.isArray(tooltip) && + ('title' in tooltip || 'children' in tooltip || 'placement' in tooltip) + ) +} + +describe('FormItem tooltip', () => { + it('should treat string as ReactNode, not as props', () => { + const stringTooltip = 'This is a tooltip' + expect(isTooltipProps(stringTooltip)).toBe(false) + }) + + it('should treat number as ReactNode, not as props', () => { + const numberTooltip = 123 + expect(isTooltipProps(numberTooltip)).toBe(false) + }) + + it('should treat React element as ReactNode, not as props', () => { + const elementTooltip =
Tooltip content
+ expect(isTooltipProps(elementTooltip)).toBe(false) + }) + + it('should treat array as ReactNode, not as props', () => { + const arrayTooltip = ['item1', 'item2'] + expect(isTooltipProps(arrayTooltip)).toBe(false) + }) + + it('should treat null/undefined as ReactNode, not as props', () => { + expect(isTooltipProps(null)).toBe(false) + expect(isTooltipProps(undefined)).toBe(false) + }) + + it('should treat object with tooltip props as props', () => { + const propsTooltip = { title: 'Tooltip title', placement: 'top' } + expect(isTooltipProps(propsTooltip)).toBe(true) + }) + + it('should treat object with children prop as props', () => { + const propsTooltip = { children: 'Tooltip children' } + expect(isTooltipProps(propsTooltip)).toBe(true) + }) + + it('should treat plain object without tooltip props as ReactNode', () => { + const plainObject = { someProperty: 'value' } + expect(isTooltipProps(plainObject)).toBe(false) + }) + + it('should render FormItem with string tooltip correctly', () => { + const { container } = render( + + + + ) + + // The tooltip should be rendered as text content, not as props + expect(container).toBeTruthy() + }) + + it('should render FormItem with tooltip props correctly', () => { + const { container } = render( + + + + ) + + // The tooltip should be rendered as Tooltip component with props + expect(container).toBeTruthy() + }) +}) diff --git a/packages/antd/src/form-item/index.tsx b/packages/antd/src/form-item/index.tsx index a111ed74857..2e8fab29ab0 100644 --- a/packages/antd/src/form-item/index.tsx +++ b/packages/antd/src/form-item/index.tsx @@ -59,7 +59,13 @@ type ComposeFormItem = React.FC> & { const isTooltipProps = ( tooltip: React.ReactNode | React.ComponentProps ): tooltip is React.ComponentProps => { - return !isElement(tooltip) + return !!( + tooltip && + typeof tooltip === 'object' && + !isElement(tooltip) && + !Array.isArray(tooltip) && + ('title' in tooltip || 'children' in tooltip || 'placement' in tooltip) + ) } const useFormItemLayout = (props: IFormItemProps) => {