Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/Icon.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as React from 'react';
import { isReactRenderable } from '@rc-component/util';
import type { RenderIconInfo, RenderIconType } from './interface';

export interface IconProps {
Expand All @@ -19,10 +20,10 @@ export default function Icon({ icon, props, children }: IconProps) {
iconNode = React.createElement(icon as any, {
...props,
});
} else if (typeof icon !== "boolean") {
} else if (typeof icon !== 'boolean') {
// Compatible for origin definition
iconNode = icon as React.ReactElement;
}

return iconNode || children || null;
return isReactRenderable(iconNode) ? iconNode : children || null;
}
4 changes: 2 additions & 2 deletions src/MenuItem.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { clsx } from 'clsx';
import Overflow from '@rc-component/overflow';
import { omit, warning, KeyCode, useComposeRef } from '@rc-component/util';
import { isReactRenderable, omit, warning, KeyCode, useComposeRef } from '@rc-component/util';
import * as React from 'react';
import { useMenuId } from './context/IdContext';
import { MenuContext } from './context/MenuContext';
Expand Down Expand Up @@ -154,7 +154,7 @@ const InternalMenuItem = React.forwardRef((props: MenuItemProps, ref: React.Ref<
};

// ============================= Icon =============================
const mergedItemIcon = itemIcon || contextItemIcon;
const mergedItemIcon = isReactRenderable(itemIcon) ? itemIcon : contextItemIcon;

// ============================ Active ============================
const { active, ...activeProps } = useActive(
Expand Down
31 changes: 31 additions & 0 deletions tests/renderability.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import React from 'react';
import { render } from '@testing-library/react';
import Menu from '../src';

describe('icon renderability', () => {
it('renders a global zero item icon', () => {
const { container } = render(<Menu itemIcon={0} items={[{ key: 'a', label: 'A' }]} />);
expect(container.querySelector('.rc-menu-item').textContent).toBe('A0');
});

it('lets a zero item icon override the global icon', () => {
const { container } = render(
<Menu itemIcon="GLOBAL" items={[{ key: 'a', label: 'A', itemIcon: 0 }]} />,
);
expect(container.querySelector('.rc-menu-item').textContent).toBe('A0');
});

it.each([0, false, null])('preserves the expand icon %s', expandIcon => {
const { container } = render(
<Menu
mode="inline"
expandIcon={expandIcon}
items={[{ key: 'a', label: 'A', children: [{ key: 'b', label: 'B' }] }]}
/>,
);
expect(container.querySelector('.rc-menu-submenu-title').textContent).toBe(
expandIcon === 0 ? 'A0' : 'A',
);
expect(container.querySelector('.rc-menu-submenu-arrow')).toBeNull();
});
});
Loading