mirror of
https://github.com/ant-design/ant-design.git
synced 2025-06-07 17:44:35 +08:00
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:
parent
5c582fcbe2
commit
fbb11f0a38
3
components/_util/isPrimitive.ts
Normal file
3
components/_util/isPrimitive.ts
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
const isPrimitive = (value: unknown) => (typeof value !== 'object' && typeof value !== 'function') || value === null;
|
||||||
|
|
||||||
|
export default isPrimitive;
|
@ -352,6 +352,17 @@ describe('Dropdown', () => {
|
|||||||
expect(container3.querySelector('button')).not.toHaveAttribute('disabled');
|
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', () => {
|
it('menu item with extra prop', () => {
|
||||||
const text = '⌘P';
|
const text = '⌘P';
|
||||||
const { container } = render(
|
const { container } = render(
|
||||||
|
@ -8,6 +8,7 @@ import useMergedState from 'rc-util/lib/hooks/useMergedState';
|
|||||||
import omit from 'rc-util/lib/omit';
|
import omit from 'rc-util/lib/omit';
|
||||||
|
|
||||||
import { useZIndex } from '../_util/hooks/useZIndex';
|
import { useZIndex } from '../_util/hooks/useZIndex';
|
||||||
|
import isPrimitive from '../_util/isPrimitive';
|
||||||
import type { AdjustOverflow } from '../_util/placements';
|
import type { AdjustOverflow } from '../_util/placements';
|
||||||
import getPlacements from '../_util/placements';
|
import getPlacements from '../_util/placements';
|
||||||
import genPurePanel from '../_util/PurePanel';
|
import genPurePanel from '../_util/PurePanel';
|
||||||
@ -175,7 +176,9 @@ const Dropdown: CompoundedComponent = (props) => {
|
|||||||
|
|
||||||
const [, token] = useToken();
|
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, {
|
const dropdownTrigger = cloneElement(child, {
|
||||||
className: classNames(
|
className: classNames(
|
||||||
|
Loading…
Reference in New Issue
Block a user