fix: isPrimitive

This commit is contained in:
ice 2024-08-06 10:28:28 +08:00
parent 6eb9425d55
commit 5c307e6c4c
4 changed files with 17 additions and 15 deletions

View File

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

View File

@ -89,13 +89,3 @@ Array [
</div>, </div>,
] ]
`; `;
exports[`Dropdown should support ReactNode type value 1`] = `
<div>
<span
class="ant-dropdown-trigger"
>
hello
</span>
</div>
`;

View File

@ -352,8 +352,14 @@ describe('Dropdown', () => {
expect(container3.querySelector('button')).not.toHaveAttribute('disabled'); expect(container3.querySelector('button')).not.toHaveAttribute('disabled');
}); });
it('should support ReactNode type value', () => { it('should support Primitive', () => {
const { container } = render(<Dropdown>hello</Dropdown>); expect(() => {
expect(container).toMatchSnapshot(); 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();
}); });
}); });

View File

@ -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';
@ -177,8 +178,8 @@ const Dropdown: CompoundedComponent = (props) => {
const [, token] = useToken(); const [, token] = useToken();
const child = React.Children.only( const child = React.Children.only(
React.isValidElement(children) ? children : <span>{children}</span>, isPrimitive(children) ? <span>{children}</span> : children,
) as React.ReactElement<any>; ) as React.ReactElement;
const dropdownTrigger = cloneElement(child, { const dropdownTrigger = cloneElement(child, {
className: classNames( className: classNames(