diff --git a/components/layout/Sider.tsx b/components/layout/Sider.tsx index b08f5188fb..b81e6eb339 100644 --- a/components/layout/Sider.tsx +++ b/components/layout/Sider.tsx @@ -17,6 +17,7 @@ if (typeof window !== 'undefined') { import React from 'react'; import classNames from 'classnames'; import omit from 'omit.js'; +import PropTypes from 'prop-types'; import Icon from '../icon'; const dimensionMap = { @@ -55,6 +56,10 @@ export default class Sider extends React.Component { style: {}, }; + static childContextTypes = { + siderCollapsed: PropTypes.bool, + }; + private mql: any; constructor(props) { @@ -78,6 +83,12 @@ export default class Sider extends React.Component { }; } + getChildContext() { + return { + siderCollapsed: this.props.collapsed, + }; + } + componentWillReceiveProps(nextProps) { if ('collapsed' in nextProps) { this.setState({ diff --git a/components/layout/demo/custom-trigger.md b/components/layout/demo/custom-trigger.md index f30a6b3a49..e059ed7f31 100644 --- a/components/layout/demo/custom-trigger.md +++ b/components/layout/demo/custom-trigger.md @@ -38,15 +38,15 @@ class SiderDemo extends React.Component { - nav 1 + nav 1 - nav 2 + nav 2 - nav 3 + nav 3 @@ -89,12 +89,4 @@ ReactDOM.render(, mountNode); border-radius: 6px; margin: 16px; } - -#components-layout-demo-custom-trigger .ant-layout-sider-collapsed .anticon { - font-size: 16px; -} - -#components-layout-demo-custom-trigger .ant-layout-sider-collapsed .nav-text { - display: none; -} ```` diff --git a/components/layout/demo/side.md b/components/layout/demo/side.md index b57aa02871..b714531b08 100644 --- a/components/layout/demo/side.md +++ b/components/layout/demo/side.md @@ -27,14 +27,10 @@ const SubMenu = Menu.SubMenu; class SiderDemo extends React.Component { state = { collapsed: false, - mode: 'inline', }; onCollapse = (collapsed) => { console.log(collapsed); - this.setState({ - collapsed, - mode: collapsed ? 'vertical' : 'inline', - }); + this.setState({ collapsed }); } render() { return ( @@ -45,27 +41,33 @@ class SiderDemo extends React.Component { onCollapse={this.onCollapse} >
- + + + + Option 1 + + + + Option 2 + User} + title={User} > - Tom - Bill - Alex + Tom + Bill + Alex Team} + title={Team} > - Team 1 - Team 2 + Team 1 + Team 2 - - - - File - + + + File @@ -99,17 +101,4 @@ ReactDOM.render(, mountNode); border-radius: 6px; margin: 16px; } - -#components-layout-demo-side .ant-layout-sider-collapsed .anticon { - font-size: 16px; - margin-left: 8px; -} - -#components-layout-demo-side .ant-layout-sider-collapsed .nav-text { - display: none; -} - -#components-layout-demo-side .ant-layout-sider-collapsed .ant-menu-submenu-vertical > .ant-menu-submenu-title:after { - display: none; -} ```` diff --git a/components/menu/index.tsx b/components/menu/index.tsx index 3a15e6761f..1e72c176fc 100644 --- a/components/menu/index.tsx +++ b/components/menu/index.tsx @@ -58,6 +58,9 @@ export default class Menu extends React.Component { static childContextTypes = { inlineCollapsed: PropTypes.bool, }; + static contextTypes = { + siderCollapsed: PropTypes.bool, + }; switchModeFromInline: boolean; inlineOpenKeys = []; constructor(props) { @@ -88,7 +91,7 @@ export default class Menu extends React.Component { } getChildContext() { return { - inlineCollapsed: this.props.inlineCollapsed, + inlineCollapsed: this.getInlineCollapsed(), }; } componentWillReceiveProps(nextProps) { @@ -131,8 +134,15 @@ export default class Menu extends React.Component { } } getRealMenuMode() { - const { mode, inlineCollapsed } = this.props; - return inlineCollapsed ? 'vertical' : mode; + const { mode } = this.props; + return this.getInlineCollapsed() ? 'vertical' : mode; + } + getInlineCollapsed() { + const { inlineCollapsed } = this.props; + if ('siderCollapsed' in this.context) { + return this.context.siderCollapsed; + } + return inlineCollapsed; } getMenuOpenAnimation() { const { openAnimation, openTransitionName } = this.props; @@ -162,12 +172,12 @@ export default class Menu extends React.Component { return menuOpenAnimation; } render() { - const { inlineCollapsed, prefixCls, className, theme } = this.props; + const { prefixCls, className, theme } = this.props; const menuMode = this.getRealMenuMode(); const menuOpenAnimation = this.getMenuOpenAnimation(); const menuClassName = classNames(className, `${prefixCls}-${theme}`, { - [`${prefixCls}-inline-collapsed`]: inlineCollapsed, + [`${prefixCls}-inline-collapsed`]: this.getInlineCollapsed(), }); const menuProps: MenuProps = {