From fbb11f0a38c6e2dc47f28905083ebad32a79fba3 Mon Sep 17 00:00:00 2001 From: ice <49827327+coding-ice@users.noreply.github.com> Date: Sat, 14 Dec 2024 23:17:52 +0800 Subject: [PATCH] fix(dropdown): support ReactNode type value (#50174) * fix(dropdown): support ReactNode type value * refactor: ternary operation * fix: isPrimitive * [CodeFactor] Apply fixes to commit 5c307e6 [ci skip] [skip ci] --------- Signed-off-by: afc163 Co-authored-by: codefactor-io Co-authored-by: afc163 --- components/_util/isPrimitive.ts | 3 +++ components/dropdown/__tests__/index.test.tsx | 11 +++++++++++ components/dropdown/dropdown.tsx | 5 ++++- 3 files changed, 18 insertions(+), 1 deletion(-) create mode 100644 components/_util/isPrimitive.ts 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(