diff --git a/.umirc.js b/.umirc.js index 9397f6f..c638f38 100644 --- a/.umirc.js +++ b/.umirc.js @@ -45,7 +45,7 @@ export default { } } routes[0].routes = newRoutes - + return routes }, }, @@ -53,7 +53,7 @@ export default { include: ['dva', 'dva/router', 'dva/saga', 'dva/fetch', 'antd/es'], }, hardSource: /* isMac */ process.platform === 'darwin', - pwa: true + pwa: true, }, ], ], diff --git a/jest.config.js b/jest.config.js index 95dd98b..c52ac83 100644 --- a/jest.config.js +++ b/jest.config.js @@ -1,3 +1,3 @@ module.exports = { - testURL: 'http://localhost:8000', - } \ No newline at end of file + testURL: 'http://localhost:8000', +} diff --git a/package.json b/package.json index 69ac89d..7b8cd69 100644 --- a/package.json +++ b/package.json @@ -17,6 +17,7 @@ "echarts-for-react": "^2.0.15-beta.0", "echarts-gl": "^1.1.1", "echarts-liquidfill": "^2.0.2", + "enquire-js": "^0.2.1", "highcharts-exporting": "^0.1.7", "highcharts-more": "^0.1.7", "lodash": "^4.17.11", diff --git a/src/components/Layout/Menu.js b/src/components/Layout/Menu.js index e3da460..ba20d98 100644 --- a/src/components/Layout/Menu.js +++ b/src/components/Layout/Menu.js @@ -14,7 +14,7 @@ import store from 'store' const { SubMenu } = Menu @withRouter -class Menus extends PureComponent { +class SiderMenu extends PureComponent { state = { openKeys: store.get('openKeys') || [], } @@ -67,7 +67,14 @@ class Menus extends PureComponent { } render() { - const { collapsed, theme, menus, location } = this.props + const { + collapsed, + theme, + menus, + location, + isMobile, + onCollapseChange, + } = this.props // Generating tree-structured data for menu content. const menuTree = arrayToTree(menus, 'id', 'menuParentId') @@ -90,6 +97,13 @@ class Menus extends PureComponent { openKeys={this.state.openKeys} inlineCollapsed={collapsed} selectedKeys={selectedKeys} + onClick={ + isMobile + ? () => { + onCollapseChange(true) + } + : undefined + } > {this.generateMenus(menuTree)} @@ -97,10 +111,12 @@ class Menus extends PureComponent { } } -Menus.propTypes = { +SiderMenu.propTypes = { menus: PropTypes.array, - collapsed: PropTypes.bool, theme: PropTypes.string, + isMobile: PropTypes.bool, + collapsed: PropTypes.bool, + onCollapseChange: PropTypes.func, } -export default Menus +export default SiderMenu diff --git a/src/components/Layout/Sider.js b/src/components/Layout/Sider.js index 01b0293..177c428 100644 --- a/src/components/Layout/Sider.js +++ b/src/components/Layout/Sider.js @@ -1,9 +1,9 @@ import React, { PureComponent } from 'react' import PropTypes from 'prop-types' -import { Icon, Switch, Layout } from 'antd' +import { Icon, Switch, Layout, Drawer } from 'antd' import { withI18n, Trans } from '@lingui/react' import { config } from 'utils' -import Menus from './Menu' +import SiderMenu from './Menu' import styles from './Sider.less' @withI18n() @@ -13,6 +13,7 @@ class Sider extends PureComponent { i18n, menus, theme, + isMobile, collapsed, onThemeChange, onCollapseChange, @@ -21,18 +22,24 @@ class Sider extends PureComponent { return (
logo {collapsed ? null :

{config.siteName}

}
- +
{collapsed ? null : (
@@ -59,6 +66,7 @@ class Sider extends PureComponent { Sider.propTypes = { menus: PropTypes.array, theme: PropTypes.string, + isMobile: PropTypes.bool, collapsed: PropTypes.bool, onThemeChange: PropTypes.func, onCollapseChange: PropTypes.func, diff --git a/src/layouts/PrimaryLayout.js b/src/layouts/PrimaryLayout.js index 5cc7a43..df51fe4 100644 --- a/src/layouts/PrimaryLayout.js +++ b/src/layouts/PrimaryLayout.js @@ -5,21 +5,50 @@ import PropTypes from 'prop-types' import withRouter from 'umi/withRouter' import { connect } from 'dva' import { MyLayout } from 'components' -import { BackTop, Layout } from 'antd' +import { BackTop, Layout, Drawer } from 'antd' import { GlobalFooter } from 'ant-design-pro' +import { enquireScreen, unenquireScreen } from 'enquire-js' import { config, pathMatchRegexp } from 'utils' import Error from '../pages/404' import styles from './PrimaryLayout.less' const { Content } = Layout -const { Header, Bread } = MyLayout +const { Header, Bread, Sider } = MyLayout @withRouter @connect(({ app, loading }) => ({ app, loading })) class PrimaryLayout extends PureComponent { + state = { + isMobile: false, + } + + componentDidMount() { + this.enquireHandler = enquireScreen(mobile => { + const { isMobile } = this.state + if (isMobile !== mobile) { + this.setState({ + isMobile: mobile, + }) + } + }) + } + + componentWillUnmount() { + unenquireScreen(this.enquireHandler) + } + + onCollapseChange = collapsed => { + this.props.dispatch({ + type: 'app/handleCollapseChange', + payload: collapsed, + }) + } + render() { const { app, location, dispatch, children } = this.props const { user, theme, routeList, permissions, collapsed } = app + const { isMobile } = this.state + const { onCollapseChange } = this // Find a route that matches the pathname. const currentRoute = routeList.find( @@ -38,39 +67,47 @@ class PrimaryLayout extends PureComponent { user, menus, collapsed, + onCollapseChange, onSignOut() { dispatch({ type: 'app/signOut' }) }, - onCollapseChange(collapsed) { - dispatch({ - type: 'app/handleCollapseChange', - payload: collapsed, - }) - }, } const siderProps = { - menus, theme, + menus, + isMobile, collapsed, + onCollapseChange, onThemeChange(theme) { dispatch({ type: 'app/handleThemeChange', payload: theme, }) }, - onCollapseChange(collapsed) { - dispatch({ - type: 'app/handleCollapseChange', - payload: collapsed, - }) - }, } return ( - + {isMobile ? ( + + + + ) : ( + + )}