Skip to content

Commit

Permalink
chore: Internal Menu interface for ProLayout usage (ant-design#33399)
Browse files Browse the repository at this point in the history
* test: Update snapshot

* chore: disable tooltip

* chore: rename
  • Loading branch information
zombieJ authored Dec 24, 2021
1 parent ff88851 commit f1e0c9c
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 89 deletions.
2 changes: 2 additions & 0 deletions components/menu/MenuContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ export interface MenuContextProps {
antdMenuTheme?: MenuTheme;
direction?: DirectionType;
firstLevel: boolean;
/** @private Internal Usage. Safe to remove */
disableMenuItemTitleTooltip?: boolean;
}

const MenuContext = createContext<MenuContextProps>({
Expand Down
62 changes: 36 additions & 26 deletions components/menu/MenuItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ export default class MenuItem extends React.Component<MenuItemProps> {
}

renderItem = ({ siderCollapsed }: SiderContextProps) => {
const { prefixCls, firstLevel, inlineCollapsed, direction } = this.context;
const { prefixCls, firstLevel, inlineCollapsed, direction, disableMenuItemTitleTooltip } =
this.context;
const { className, children } = this.props;
const { title, icon, danger, ...rest } = this.props;

Expand All @@ -55,33 +56,42 @@ export default class MenuItem extends React.Component<MenuItemProps> {
tooltipProps.visible = false;
}
const childrenLength = toArray(children).length;
return (
<Tooltip
{...tooltipProps}
placement={direction === 'rtl' ? 'left' : 'right'}
overlayClassName={`${prefixCls}-inline-collapsed-tooltip`}

let returnNode = (
<Item
{...rest}
className={classNames(
{
[`${prefixCls}-item-danger`]: danger,
[`${prefixCls}-item-only-child`]: (icon ? childrenLength + 1 : childrenLength) === 1,
},
className,
)}
title={typeof title === 'string' ? title : undefined}
>
<Item
{...rest}
className={classNames(
{
[`${prefixCls}-item-danger`]: danger,
[`${prefixCls}-item-only-child`]: (icon ? childrenLength + 1 : childrenLength) === 1,
},
className,
)}
title={typeof title === 'string' ? title : undefined}
>
{cloneElement(icon, {
className: classNames(
isValidElement(icon) ? icon.props?.className : '',
`${prefixCls}-item-icon`,
),
})}
{this.renderItemChildren(inlineCollapsed)}
</Item>
</Tooltip>
{cloneElement(icon, {
className: classNames(
isValidElement(icon) ? icon.props?.className : '',
`${prefixCls}-item-icon`,
),
})}
{this.renderItemChildren(inlineCollapsed)}
</Item>
);

if (!disableMenuItemTitleTooltip) {
returnNode = (
<Tooltip
{...tooltipProps}
placement={direction === 'rtl' ? 'left' : 'right'}
overlayClassName={`${prefixCls}-inline-collapsed-tooltip`}
>
{returnNode}
</Tooltip>
);
}

return returnNode;
};

render() {
Expand Down
67 changes: 15 additions & 52 deletions components/menu/__tests__/__snapshots__/demo.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -879,15 +879,15 @@ Array [
<br />,
<br />,
<ul
class="ant-menu-overflow ant-menu ant-menu-root ant-menu-horizontal ant-menu-dark"
class="ant-menu ant-menu-root ant-menu-vertical ant-menu-dark ant-menu-inline-collapsed"
data-menu-list="true"
role="menu"
tabindex="0"
>
<li
class="ant-menu-overflow-item ant-menu-submenu ant-menu-submenu-horizontal"
class="ant-menu-submenu ant-menu-submenu-vertical"
role="none"
style="opacity:1;order:0"
style="background:rgba(255,255,255,0.3)"
>
<div
aria-expanded="false"
Expand Down Expand Up @@ -926,9 +926,9 @@ Array [
</div>
</li>
<li
class="ant-menu-overflow-item ant-menu-submenu ant-menu-submenu-horizontal"
class="ant-menu-submenu ant-menu-submenu-vertical"
role="none"
style="opacity:1;order:1"
style="background:rgba(255,255,255,0.3)"
>
<div
aria-expanded="false"
Expand Down Expand Up @@ -967,64 +967,27 @@ Array [
</div>
</li>
<li
class="ant-menu-overflow-item ant-menu-item ant-menu-item-only-child"
class="ant-menu-item ant-menu-item-only-child"
role="menuitem"
style="opacity:1;order:2"
style="text-decoration:underline"
tabindex="-1"
>
<span
class="ant-menu-title-content"
<div
class="ant-menu-inline-collapsed-noicon"
>
Option 11
</span>
O
</div>
</li>
<li
class="ant-menu-overflow-item ant-menu-item ant-menu-item-only-child"
class="ant-menu-item ant-menu-item-only-child"
role="menuitem"
style="opacity:1;order:3"
style="text-decoration:underline"
tabindex="-1"
>
<span
class="ant-menu-title-content"
>
Option 12
</span>
</li>
<li
aria-hidden="true"
class="ant-menu-overflow-item ant-menu-overflow-item-rest ant-menu-submenu ant-menu-submenu-horizontal"
role="none"
style="opacity:0;height:0;overflow-y:hidden;order:9007199254740991;pointer-events:none;position:absolute"
>
<div
aria-expanded="false"
aria-haspopup="true"
class="ant-menu-submenu-title"
role="menuitem"
tabindex="-1"
class="ant-menu-inline-collapsed-noicon"
>
<span
aria-label="ellipsis"
class="anticon anticon-ellipsis"
role="img"
>
<svg
aria-hidden="true"
data-icon="ellipsis"
fill="currentColor"
focusable="false"
height="1em"
viewBox="64 64 896 896"
width="1em"
>
<path
d="M176 511a56 56 0 10112 0 56 56 0 10-112 0zm280 0a56 56 0 10112 0 56 56 0 10-112 0zm280 0a56 56 0 10112 0 56 56 0 10-112 0z"
/>
</svg>
</span>
<i
class="ant-menu-submenu-arrow"
/>
O
</div>
</li>
</ul>,
Expand Down
43 changes: 35 additions & 8 deletions components/menu/demo/style-debug.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,31 @@ buggy!

buggy!

```jsx
import { Menu, Switch } from 'antd';
```tsx
import * as React from 'react';
import { Menu, MenuProps, Switch } from 'antd';
import { MailOutlined, AppstoreOutlined } from '@ant-design/icons';

const { SubMenu } = Menu;

class Sider extends React.Component {
state = {
interface DemoState {
theme: 'light' | 'dark';
current: string;
}

class Demo extends React.Component<{}, DemoState> {
state: DemoState = {
theme: 'dark',
current: '1',
};

changeTheme = value => {
changeTheme = (value: boolean) => {
this.setState({
theme: value ? 'dark' : 'light',
});
};

handleClick = e => {
handleClick: MenuProps['onClick'] = e => {
console.log('click ', e);
this.setState({
current: e.key,
Expand All @@ -54,7 +60,28 @@ class Sider extends React.Component {
theme={this.state.theme}
onClick={this.handleClick}
selectedKeys={[this.state.current]}
mode="horizontal"
mode="inline"
inlineCollapsed
// Test only. Remove in future.
_internalRenderMenuItem={node =>
React.cloneElement(node, {
style: {
...node.props.style,
textDecoration: 'underline',
},
})
}
// Test only. Remove in future.
_internalRenderSubMenuItem={node =>
React.cloneElement(node, {
style: {
...node.props.style,
background: 'rgba(255,255,255,0.3)',
},
})
}
// Test only. Remove in future.
_internalDisableMenuItemTitleTooltip
>
<SubMenu key="sub1" icon={<MailOutlined />} title="Navigation One Long Long Long Long">
<Menu.Item key="1">Option 1</Menu.Item>
Expand All @@ -78,5 +105,5 @@ class Sider extends React.Component {
}
}

ReactDOM.render(<Sider />, mountNode);
ReactDOM.render(<Demo />, mountNode);
```
13 changes: 11 additions & 2 deletions components/menu/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ export type MenuMode = 'vertical' | 'vertical-left' | 'vertical-right' | 'horizo
export interface MenuProps extends RcMenuProps {
theme?: MenuTheme;
inlineIndent?: number;

// >>>>> Private
/**
* @private Internal Usage. Not promise crash if used in production. Connect with chenshuai2144
* for removing.
*/
_internalDisableMenuItemTitleTooltip?: boolean;
}

type InternalMenuProps = MenuProps &
Expand Down Expand Up @@ -67,6 +74,7 @@ class InternalMenu extends React.Component<InternalMenuProps> {
className,
theme,
expandIcon,
_internalDisableMenuItemTitleTooltip,
...restProps
} = this.props;

Expand All @@ -83,13 +91,14 @@ class InternalMenu extends React.Component<InternalMenuProps> {
const menuClassName = classNames(`${prefixCls}-${theme}`, className);

// TODO: refactor menu with function component
const contextValue = memoize((cls, collapsed, the, dir) => ({
const contextValue = memoize((cls, collapsed, the, dir, disableMenuItemTitleTooltip) => ({
prefixCls: cls,
inlineCollapsed: collapsed || false,
antdMenuTheme: the,
direction: dir,
firstLevel: true,
}))(prefixCls, inlineCollapsed, theme, direction);
disableMenuItemTitleTooltip,
}))(prefixCls, inlineCollapsed, theme, direction, _internalDisableMenuItemTitleTooltip);

return (
<MenuContext.Provider value={contextValue}>
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@
"rc-image": "~5.2.5",
"rc-input-number": "~7.3.0",
"rc-mentions": "~1.6.1",
"rc-menu": "~9.0.12",
"rc-menu": "~9.1.1",
"rc-motion": "^2.4.4",
"rc-notification": "~4.5.7",
"rc-pagination": "~3.1.9",
Expand Down

0 comments on commit f1e0c9c

Please sign in to comment.