mirror of
https://github.com/ant-design/ant-design.git
synced 2025-06-08 01:53:34 +08:00
fix: isPrimitive
This commit is contained in:
parent
6eb9425d55
commit
5c307e6c4c
5
components/_util/isPrimitive.ts
Normal file
5
components/_util/isPrimitive.ts
Normal file
@ -0,0 +1,5 @@
|
||||
const isPrimitive = (value: unknown) => {
|
||||
return (typeof value !== 'object' && typeof value !== 'function') || value === null;
|
||||
};
|
||||
|
||||
export default isPrimitive;
|
@ -89,13 +89,3 @@ Array [
|
||||
</div>,
|
||||
]
|
||||
`;
|
||||
|
||||
exports[`Dropdown should support ReactNode type value 1`] = `
|
||||
<div>
|
||||
<span
|
||||
class="ant-dropdown-trigger"
|
||||
>
|
||||
hello
|
||||
</span>
|
||||
</div>
|
||||
`;
|
||||
|
@ -352,8 +352,14 @@ describe('Dropdown', () => {
|
||||
expect(container3.querySelector('button')).not.toHaveAttribute('disabled');
|
||||
});
|
||||
|
||||
it('should support ReactNode type value', () => {
|
||||
const { container } = render(<Dropdown>hello</Dropdown>);
|
||||
expect(container).toMatchSnapshot();
|
||||
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();
|
||||
});
|
||||
});
|
||||
|
@ -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';
|
||||
@ -177,8 +178,8 @@ const Dropdown: CompoundedComponent = (props) => {
|
||||
const [, token] = useToken();
|
||||
|
||||
const child = React.Children.only(
|
||||
React.isValidElement(children) ? children : <span>{children}</span>,
|
||||
) as React.ReactElement<any>;
|
||||
isPrimitive(children) ? <span>{children}</span> : children,
|
||||
) as React.ReactElement;
|
||||
|
||||
const dropdownTrigger = cloneElement(child, {
|
||||
className: classNames(
|
||||
|
Loading…
Reference in New Issue
Block a user