diff --git a/components/_util/isPrimitive.ts b/components/_util/isPrimitive.ts new file mode 100644 index 0000000000..77d9b63345 --- /dev/null +++ b/components/_util/isPrimitive.ts @@ -0,0 +1,3 @@ +const isPrimitive = (value: unknown) => (typeof value !== 'object' && typeof value !== 'function') || value === null; + +export default isPrimitive; diff --git a/components/dropdown/__tests__/index.test.tsx b/components/dropdown/__tests__/index.test.tsx index 873e59510d..e5190e2384 100644 --- a/components/dropdown/__tests__/index.test.tsx +++ b/components/dropdown/__tests__/index.test.tsx @@ -352,6 +352,17 @@ describe('Dropdown', () => { expect(container3.querySelector('button')).not.toHaveAttribute('disabled'); }); + it('should support Primitive', () => { + expect(() => { + render(antd); + render({123}); + render({undefined}); + render({true}); + render({false}); + render({null}); + }).not.toThrow(); + }); + it('menu item with extra prop', () => { const text = '⌘P'; const { container } = render( diff --git a/components/dropdown/dropdown.tsx b/components/dropdown/dropdown.tsx index e5fec1d991..714d04345d 100644 --- a/components/dropdown/dropdown.tsx +++ b/components/dropdown/dropdown.tsx @@ -8,6 +8,7 @@ import useMergedState from 'rc-util/lib/hooks/useMergedState'; import omit from 'rc-util/lib/omit'; import { useZIndex } from '../_util/hooks/useZIndex'; +import isPrimitive from '../_util/isPrimitive'; import type { AdjustOverflow } from '../_util/placements'; import getPlacements from '../_util/placements'; import genPurePanel from '../_util/PurePanel'; @@ -175,7 +176,9 @@ const Dropdown: CompoundedComponent = (props) => { const [, token] = useToken(); - const child = React.Children.only(children) as React.ReactElement; + const child = React.Children.only( + isPrimitive(children) ? {children} : children, + ) as React.ReactElement; const dropdownTrigger = cloneElement(child, { className: classNames(