Skip to content

Commit af29c8d

Browse files
authored
feat: expandIcon support hidden by null or false (#663)
* feat: expandIcon support hidden by null or false * feat: expandIcon support hidden by null or false * feat: remove console * feat: optimize code
1 parent 15ff225 commit af29c8d

File tree

3 files changed

+73
-20
lines changed

3 files changed

+73
-20
lines changed

src/Icon.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,15 @@ export interface IconProps {
1111
export default function Icon({ icon, props, children }: IconProps) {
1212
let iconNode: React.ReactElement;
1313

14+
if (icon === null || icon === false) {
15+
return null;
16+
}
17+
1418
if (typeof icon === 'function') {
1519
iconNode = React.createElement(icon as any, {
1620
...props,
1721
});
18-
} else {
22+
} else if (typeof icon !== "boolean") {
1923
// Compatible for origin definition
2024
iconNode = icon as React.ReactElement;
2125
}

src/SubMenu/index.tsx

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,8 @@ const InternalSubMenu = (props: SubMenuProps) => {
119119
}
120120

121121
// ================================ Icon ================================
122-
const mergedItemIcon = itemIcon || contextItemIcon;
123-
const mergedExpandIcon = expandIcon || contextExpandIcon;
122+
const mergedItemIcon = itemIcon ?? contextItemIcon;
123+
const mergedExpandIcon = expandIcon ?? contextExpandIcon;
124124

125125
// ================================ Open ================================
126126
const originOpen = openKeys.includes(eventKey);
@@ -248,16 +248,17 @@ const InternalSubMenu = (props: SubMenuProps) => {
248248

249249
{/* Only non-horizontal mode shows the icon */}
250250
<Icon
251-
icon={mode !== 'horizontal' ? mergedExpandIcon : null}
252-
props={{
253-
...props,
254-
isOpen: open,
255-
// [Legacy] Not sure why need this mark
256-
isSubMenu: true,
257-
}}
258-
>
259-
<i className={`${subMenuPrefixCls}-arrow`} />
260-
</Icon>
251+
icon={mode !== 'horizontal' ? mergedExpandIcon : undefined}
252+
props={{
253+
...props,
254+
isOpen: open,
255+
// [Legacy] Not sure why need this mark
256+
isSubMenu: true,
257+
}}
258+
>
259+
<i className={`${subMenuPrefixCls}-arrow`} />
260+
</Icon>
261+
261262
</div>
262263
);
263264

tests/Menu.spec.tsx

Lines changed: 55 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import KeyCode from 'rc-util/lib/KeyCode';
44
import { resetWarned } from 'rc-util/lib/warning';
55
import React from 'react';
66
import { act } from 'react-dom/test-utils';
7-
import type { MenuRef} from '../src';
7+
import type { MenuRef } from '../src';
88
import Menu, { Divider, MenuItem, MenuItemGroup, SubMenu } from '../src';
99
import { isActive, last } from './util';
1010
import type { MenuMode } from '@/interface';
@@ -329,8 +329,8 @@ describe('Menu', () => {
329329
it('openKeys should allow to be empty', () => {
330330
const { container } = render(
331331
<Menu
332-
onClick={() => {}}
333-
onOpenChange={() => {}}
332+
onClick={() => { }}
333+
onOpenChange={() => { }}
334334
openKeys={undefined}
335335
selectedKeys={['1']}
336336
mode="inline"
@@ -740,11 +740,59 @@ describe('Menu', () => {
740740
<MenuItem key="cat">Cat</MenuItem>
741741
</Menu>,
742742
);
743-
// Get the divider element with the rc-menu-item-divider class
744-
const divider = container.querySelector('.rc-menu-item-divider');
743+
// Get the divider element with the rc-menu-item-divider class
744+
const divider = container.querySelector('.rc-menu-item-divider');
745+
746+
// Assert that the divider element with rc-menu-item-divider class has role="separator"
747+
expect(divider).toHaveAttribute('role', 'separator');
748+
});
749+
it('expandIcon should be hidden when setting null or false', () => {
750+
const App = ({expand, subExpand}: {expand?: React.ReactNode, subExpand?: React.ReactNode}) => (
751+
<Menu expandIcon={expand}>
752+
<SubMenu
753+
title="sub menu"
754+
key="1"
755+
expandIcon={subExpand}
756+
>
757+
<MenuItem key="1-1">0-1</MenuItem>
758+
<MenuItem key="1-2">0-2</MenuItem>
759+
</SubMenu>,
760+
<SubMenu
761+
title="sub menu2"
762+
key="2"
763+
>
764+
<MenuItem key="2-1">0-1</MenuItem>
765+
<MenuItem key="2-2">0-2</MenuItem>
766+
</SubMenu>,
767+
<MenuItem key="cat">Cat</MenuItem>
768+
</Menu>
769+
);
770+
771+
const { container, rerender } = render(
772+
<App expand={null} subExpand={undefined} />,
773+
);
774+
expect(container.querySelectorAll(".rc-menu-submenu-arrow").length).toBe(0);
775+
776+
rerender(
777+
<App expand={null} subExpand />,
778+
);
779+
expect(container.querySelectorAll(".rc-menu-submenu-arrow").length).toBe(1);
780+
781+
rerender(
782+
<App />,
783+
);
784+
expect(container.querySelectorAll(".rc-menu-submenu-arrow").length).toBe(2);
785+
786+
rerender(
787+
<App expand={false} subExpand={undefined} />,
788+
);
789+
expect(container.querySelectorAll(".rc-menu-submenu-arrow").length).toBe(0);
790+
791+
rerender(
792+
<App expand={false} subExpand />,
793+
);
794+
expect(container.querySelectorAll(".rc-menu-submenu-arrow").length).toBe(1);
745795

746-
// Assert that the divider element with rc-menu-item-divider class has role="separator"
747-
expect(divider).toHaveAttribute('role', 'separator');
748796
});
749797
});
750798
/* eslint-enable */

0 commit comments

Comments
 (0)