fix(Tag): renders excess content when passing only the icon (#43518)

* fix: add judgment

* chore: add test case
This commit is contained in:
JiaQi 2023-07-13 09:43:35 +08:00 committed by GitHub
parent 82e5555ff3
commit 25acc479f0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 7 additions and 1 deletions

View File

@ -1,6 +1,7 @@
import React from 'react';
import { Simulate } from 'react-dom/test-utils';
import { CheckCircleOutlined } from '@ant-design/icons';
import Tag from '..';
import { resetWarned } from '../../_util/warning';
@ -135,6 +136,11 @@ describe('Tag', () => {
expect(onClick).not.toHaveBeenCalled();
});
it('should only render icon when no children', () => {
const { container } = render(<Tag icon={<CheckCircleOutlined />} />);
expect(container.querySelector('.ant-tag ')?.childElementCount).toBe(1);
});
it('deprecated warning', () => {
resetWarned();
const errSpy = jest.spyOn(console, 'error').mockImplementation(() => {});

View File

@ -128,7 +128,7 @@ const InternalTag: React.ForwardRefRenderFunction<HTMLSpanElement, TagProps> = (
const kids: React.ReactNode = iconNode ? (
<>
{iconNode}
<span>{children}</span>
{children && <span>{children}</span>}
</>
) : (
children