Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(menu): fix custom expand icon cannot be hidden #40071

Merged
merged 8 commits into from
Jan 7, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
59 changes: 59 additions & 0 deletions components/menu/__tests__/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1047,4 +1047,63 @@ describe('Menu', () => {
);
errorSpy.mockRestore();
});

it('expandIconClassName', () => {
const Demo = ({ expandIcon }: Pick<MenuProps, 'expandIcon'>) => (
<Menu
expandIcon={expandIcon}
inlineCollapsed
items={[
{
label: 'Option 1',
key: '1',
icon: '112',
children: [
{
label: 'Option 1-1',
key: '1-1',
},
],
},
]}
/>
);

const { container, rerender } = render(
<Demo expandIcon={<span className="custom-expand-icon" />} />,
);
expect(container.querySelector('.custom-expand-icon')).toBeTruthy();

rerender(<Demo expandIcon={() => <span className="function-custom-expand-icon" />} />);
expect(container.querySelector('.function-custom-expand-icon')).toBeTruthy();
});

// https://github.com/ant-design/ant-design/issues/40041
it('should not show icon when inlineCollapsed', () => {
const { container } = render(
<Menu
expandIcon={<span className="bamboo">I</span>}
inlineCollapsed
items={[
{
label: 'Option 1',
key: '1',
icon: '112',
children: [
{
label: 'Option 1-1',
key: '1-1',
},
],
},
]}
/>,
);

expect(container.querySelector('.bamboo')).toBeTruthy();
expect(getComputedStyle(container.querySelector('.bamboo') as HTMLElement)).toHaveProperty(
'opacity',
'0',
);
});
});
li-jia-nan marked this conversation as resolved.
Show resolved Hide resolved
25 changes: 17 additions & 8 deletions components/menu/menu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -121,14 +121,23 @@ const InternalMenu = forwardRef<RcMenuRef, InternalMenuProps>((props, ref) => {
const menuClassName = classNames(`${prefixCls}-${theme}`, className);

// ====================== Expand Icon ========================
let mergedExpandIcon: MenuProps[`expandIcon`];
if (typeof expandIcon === 'function') {
mergedExpandIcon = expandIcon;
} else {
mergedExpandIcon = cloneElement(expandIcon || overrideObj.expandIcon, {
className: `${prefixCls}-submenu-expand-icon`,
});
}
const mergedExpandIcon: MenuProps[`expandIcon`] = (expandIconProps) => {
li-jia-nan marked this conversation as resolved.
Show resolved Hide resolved
const nonUnderwriteExpandIcon: any =
typeof expandIcon === 'function'
? expandIcon(expandIconProps)
: expandIcon || overrideObj.expandIcon;

if (nonUnderwriteExpandIcon || nonUnderwriteExpandIcon === 0) {
li-jia-nan marked this conversation as resolved.
Show resolved Hide resolved
return cloneElement(nonUnderwriteExpandIcon, {
className: classNames(
`${prefixCls}-submenu-expand-icon`,
nonUnderwriteExpandIcon?.props?.className,
),
});
}

return <i className={`${prefixCls}-submenu-arrow`} />;
};

// ======================== Context ==========================
const contextValue = React.useMemo<MenuContextProps>(
Expand Down
5 changes: 4 additions & 1 deletion components/menu/style/vertical.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,10 @@ const getVerticalStyle: GenerateStyle<MenuToken> = (token) => {
paddingInline: `calc(50% - ${fontSizeSM}px)`,
textOverflow: 'clip',

[`${componentCls}-submenu-arrow`]: {
[`
${componentCls}-submenu-arrow,
${componentCls}-submenu-expand-icon
`]: {
opacity: 0,
},

Expand Down