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 <afc163@gmail.com>
Co-authored-by: codefactor-io <support@codefactor.io>
Co-authored-by: afc163 <afc163@gmail.com>
This commit is contained in:
ice 2024-12-14 23:17:52 +08:00 committed by GitHub
parent 5c582fcbe2
commit fbb11f0a38
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 18 additions and 1 deletions

View File

@ -0,0 +1,3 @@
const isPrimitive = (value: unknown) => (typeof value !== 'object' && typeof value !== 'function') || value === null;
export default isPrimitive;

View File

@ -352,6 +352,17 @@ describe('Dropdown', () => {
expect(container3.querySelector('button')).not.toHaveAttribute('disabled');
});
it('should support Primitive', () => {
expect(() => {
render(<Dropdown>antd</Dropdown>);
render(<Dropdown>{123}</Dropdown>);
render(<Dropdown>{undefined}</Dropdown>);
render(<Dropdown>{true}</Dropdown>);
render(<Dropdown>{false}</Dropdown>);
render(<Dropdown>{null}</Dropdown>);
}).not.toThrow();
});
it('menu item with extra prop', () => {
const text = '⌘P';
const { container } = render(

View File

@ -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<any>;
const child = React.Children.only(
isPrimitive(children) ? <span>{children}</span> : children,
) as React.ReactElement;
const dropdownTrigger = cloneElement(child, {
className: classNames(