From bd09ee3dd2dde2332eec1b41930e7d7f58b3048c Mon Sep 17 00:00:00 2001 From: Sendya <18x@loacg.com> Date: Tue, 23 Feb 2021 22:55:09 +0800 Subject: [PATCH] fix: BaseMenu menu-item class --- examples/index.tsx | 54 ++++++++++++++++++++++---------------- src/SiderMenu/BaseMenu.tsx | 8 +++--- src/SiderMenu/index.less | 7 ++++- 3 files changed, 42 insertions(+), 27 deletions(-) diff --git a/examples/index.tsx b/examples/index.tsx index b514444d..a47220ef 100644 --- a/examples/index.tsx +++ b/examples/index.tsx @@ -1,5 +1,5 @@ import 'ant-design-vue/dist/antd.less'; -import { createApp, defineComponent, watch, ref } from 'vue'; +import { createApp, defineComponent, watch, ref, watchEffect, onMounted } from 'vue'; import { createRouter, createWebHashHistory, useRoute, useRouter, RouteRecord } from 'vue-router'; import { Avatar } from 'ant-design-vue'; import { UserOutlined } from '@ant-design/icons-vue'; @@ -30,22 +30,36 @@ const BasicLayout = defineComponent({ const menuData = getMenuData(getRoutes()); globalState.menuData = menuData; - const cacheOpenKeys = ref([]); - watch( - () => state.collapsed, - (collapsed: boolean) => { - console.log('post watch', collapsed, state.collapsed); - if (collapsed) { - cacheOpenKeys.value = state.openKeys; - state.openKeys = []; - } else { - state.openKeys = cacheOpenKeys.value; - } - }, - { - flush: 'pre', - }, - ); + const updateSelectedMenu = () => { + const matched = route.matched.concat().map(item => item.path); + matched.shift(); + state.selectedKeys = matched; + }; + + onMounted(() => { + // if sider collapsed, set openKeys is null. + const cacheOpenKeys = ref([]); + watch( + () => state.collapsed, + (collapsed: boolean) => { + console.log('post watch', collapsed, state.collapsed); + if (collapsed) { + cacheOpenKeys.value = state.openKeys; + state.openKeys = []; + } else { + state.openKeys = cacheOpenKeys.value; + } + }, + { + flush: 'pre', + }, + ); + + // watch route + watchEffect(() => { + updateSelectedMenu(); + }); + }); return () => ( @@ -67,11 +81,7 @@ const BasicLayout = defineComponent({ {...{ 'onUpdate:collapsed': $event => (state.collapsed = $event), 'onUpdate:openKeys': $event => (state.openKeys = $event), - 'onUpdate:selectedKeys': () => { - const matched = route.matched.concat().map(item => item.path); - matched.shift(); - state.selectedKeys = matched; - }, + 'onUpdate:selectedKeys': updateSelectedMenu, }} v-slots={{ rightContentRender: () => ( diff --git a/src/SiderMenu/BaseMenu.tsx b/src/SiderMenu/BaseMenu.tsx index 442077fa..ec629c72 100644 --- a/src/SiderMenu/BaseMenu.tsx +++ b/src/SiderMenu/BaseMenu.tsx @@ -167,12 +167,12 @@ class MenuUtil { const { prefixCls, i18n } = this.props; const menuTitle = (i18n && i18n(item.meta?.title)) || item.meta?.title; const defaultTitle = item?.meta.icon ? ( - - + + {!isChildren && } {menuTitle} - - + + ) : ( {menuTitle} ); diff --git a/src/SiderMenu/index.less b/src/SiderMenu/index.less index d4908f98..0fbda86e 100644 --- a/src/SiderMenu/index.less +++ b/src/SiderMenu/index.less @@ -9,7 +9,8 @@ position: relative; background-color: @layout-sider-background; border-right: 0; - transition: background-color 0.3s; + transition: background-color 0.3s, min-width 0.3s, + max-width 0.3s cubic-bezier(0.645, 0.045, 0.355, 1); z-index: 9; &.@{ant-prefix}-menu-vertical .@{ant-prefix}-menu-item:not(:last-child), @@ -108,6 +109,10 @@ z-index: 10; min-height: 100%; box-shadow: 2px 0 6px rgba(0, 21, 41, 0.35); + + span.@{ant-prefix}-pro-menu-item-title { + transition: none; + } } .@{ant-prefix}-layout-sider-children {