diff --git a/components/menu/src/Menu.tsx b/components/menu/src/Menu.tsx index e836ae702..f04b016da 100644 --- a/components/menu/src/Menu.tsx +++ b/components/menu/src/Menu.tsx @@ -67,11 +67,11 @@ export default defineComponent({ computed(() => undefined), ); const inlineCollapsed = computed(() => { - const { inlineCollapsed } = props; + console.log('siderCollapsed.value', siderCollapsed.value); if (siderCollapsed.value !== undefined) { return siderCollapsed.value; } - return inlineCollapsed; + return props.inlineCollapsed; }); const isMounted = ref(false); @@ -164,7 +164,9 @@ export default defineComponent({ watch( () => props.openKeys, (openKeys = mergedOpenKeys.value) => { - mergedOpenKeys.value = openKeys; + if (!shallowEqual(mergedOpenKeys.value, openKeys)) { + mergedOpenKeys.value = openKeys; + } }, { immediate: true }, ); @@ -177,47 +179,56 @@ export default defineComponent({ const mergedMode = ref('vertical'); const mergedInlineCollapsed = ref(false); - const isInlineMode = computed(() => mergedMode.value === 'inline'); - - // >>>>> Cache & Reset open keys when inlineCollapsed changed - const inlineCacheOpenKeys = ref([]); - - // Cache - watchEffect(() => { - if (isInlineMode.value) { - inlineCacheOpenKeys.value = mergedOpenKeys.value; - } - }); - - const mountRef = ref(false); - - // Restore - watch(isInlineMode, () => { - if (!mountRef.value) { - mountRef.value = true; - return; - } - - if (isInlineMode.value) { - mergedOpenKeys.value = inlineCacheOpenKeys.value; - } else { - const empty = []; - mergedOpenKeys.value = empty; - // Trigger open event in case its in control - emit('update:openKeys', empty); - emit('openChange', empty); - } - }); - watchEffect(() => { if (props.mode === 'inline' && inlineCollapsed.value) { mergedMode.value = 'vertical'; mergedInlineCollapsed.value = inlineCollapsed.value; + } else { + mergedMode.value = props.mode; + mergedInlineCollapsed.value = false; } - mergedMode.value = props.mode; - mergedInlineCollapsed.value = false; }); + const isInlineMode = computed(() => mergedMode.value === 'inline'); + + // >>>>> Cache & Reset open keys when inlineCollapsed changed + const inlineCacheOpenKeys = ref(mergedOpenKeys.value); + + const mountRef = ref(false); + + // Cache + watch( + mergedOpenKeys, + () => { + if (isInlineMode.value) { + inlineCacheOpenKeys.value = mergedOpenKeys.value; + } + }, + { immediate: true }, + ); + + // Restore + watch( + isInlineMode, + () => { + if (!mountRef.value) { + mountRef.value = true; + return; + } + + if (isInlineMode.value) { + mergedOpenKeys.value = inlineCacheOpenKeys.value; + } else { + const empty = []; + mergedOpenKeys.value = empty; + // Trigger open event in case its in control + emit('update:openKeys', empty); + emit('openChange', empty); + } + }, + { immediate: true }, + ); + const className = computed(() => { return { [`${prefixCls.value}`]: true, diff --git a/examples/App.vue b/examples/App.vue index 2eb2ef0d2..2a34b3910 100644 --- a/examples/App.vue +++ b/examples/App.vue @@ -89,7 +89,7 @@ export default defineComponent({ ); const toggleCollapsed = () => { state.collapsed = !state.collapsed; - state.openKeys = state.collapsed ? [] : state.preOpenKeys; + // state.openKeys = state.collapsed ? [] : state.preOpenKeys; }; return {