From 9b54bd728d7e1082a99c6c04febda86fdad97613 Mon Sep 17 00:00:00 2001 From: xrkffgg Date: Wed, 27 May 2020 22:19:07 +0800 Subject: [PATCH] fix: menu inline collapsed show without icon (#24330) close #24311 --- components/menu/MenuItem.tsx | 11 ++++++++--- components/menu/SubMenu.tsx | 15 ++++++++++----- components/menu/style/index.less | 10 ++++++++++ 3 files changed, 28 insertions(+), 8 deletions(-) diff --git a/components/menu/MenuItem.tsx b/components/menu/MenuItem.tsx index 78d17b2747..be632b1d4c 100644 --- a/components/menu/MenuItem.tsx +++ b/components/menu/MenuItem.tsx @@ -39,11 +39,16 @@ export default class MenuItem extends React.Component { this.menuItem = menuItem; }; - renderItemChildren() { - const { icon, children } = this.props; + renderItemChildren(inlineCollapsed: boolean) { + const { icon, children, level, rootPrefixCls } = this.props; // inline-collapsed.md demo 依赖 span 来隐藏文字,有 icon 属性,则内部包裹一个 span // ref: https://github.com/ant-design/ant-design/pull/23456 if (!icon || (isValidElement(children) && children.type === 'span')) { + if (children && inlineCollapsed && level === 1 && typeof children === 'string') { + return ( +
{children.charAt(0)}
+ ); + } return children; } return {children}; @@ -89,7 +94,7 @@ export default class MenuItem extends React.Component { ref={this.saveMenuItem} > {icon} - {this.renderItemChildren()} + {this.renderItemChildren(inlineCollapsed)} ); diff --git a/components/menu/SubMenu.tsx b/components/menu/SubMenu.tsx index dd4f1cd152..e50b55b886 100644 --- a/components/menu/SubMenu.tsx +++ b/components/menu/SubMenu.tsx @@ -15,6 +15,7 @@ export interface SubMenuProps { rootPrefixCls?: string; className?: string; disabled?: boolean; + level?: number; title?: React.ReactNode; icon?: React.ReactNode; style?: React.CSSProperties; @@ -43,10 +44,14 @@ class SubMenu extends React.Component { this.subMenu = subMenu; }; - renderTitle() { - const { icon, title } = this.props; + renderTitle(inlineCollapsed: boolean) { + const { icon, title, level, rootPrefixCls } = this.props; if (!icon) { - return title; + return inlineCollapsed && level === 1 && title && typeof title === 'string' ? ( +
{title.charAt(0)}
+ ) : ( + title + ); } // inline-collapsed.md demo 依赖 span 来隐藏文字,有 icon 属性,则内部包裹一个 span // ref: https://github.com/ant-design/ant-design/pull/23456 @@ -63,10 +68,10 @@ class SubMenu extends React.Component { const { rootPrefixCls, popupClassName } = this.props; return ( - {({ antdMenuTheme }: MenuContextProps) => ( + {({ inlineCollapsed, antdMenuTheme }: MenuContextProps) => ( .@{menu-prefix-cls}-inline-collapsed-noicon { + font-size: @menu-icon-size-lg; + text-align: center; + } + } + } + &-sub&-inline { padding: 0; border: 0;