From ab79b4b078e051e7ac9775c443dcc2a6259bfa35 Mon Sep 17 00:00:00 2001 From: tangjinzhou <415800467@qq.com> Date: Fri, 21 Jan 2022 22:58:58 +0800 Subject: [PATCH] fix: tabs control mode not work, close #5180 --- components/tabs/src/Tabs.tsx | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/components/tabs/src/Tabs.tsx b/components/tabs/src/Tabs.tsx index 9d94784f2..26820aa03 100644 --- a/components/tabs/src/Tabs.tsx +++ b/components/tabs/src/Tabs.tsx @@ -11,7 +11,7 @@ import type { Tab, } from './interface'; import type { CSSProperties, PropType, ExtractPropTypes } from 'vue'; -import { ref, defineComponent, computed, onMounted, watchEffect, camelize } from 'vue'; +import { defineComponent, computed, onMounted, watchEffect, camelize } from 'vue'; import { flattenChildren, initDefaultProps, isValidElement } from '../../_util/props-util'; import useConfigInject from '../../_util/hooks/useConfigInject'; import useState from '../../_util/hooks/useState'; @@ -181,24 +181,19 @@ const InternalTabs = defineComponent({ }); // ====================== Active Key ====================== - // use activeKey & mergedActiveKey to control - // https://github.com/vueComponent/ant-design-vue/issues/5056 - const [activeKey] = useMergedState(() => props.tabs[0]?.key, { + const [mergedActiveKey, setMergedActiveKey] = useMergedState(() => props.tabs[0]?.key, { value: computed(() => props.activeKey), defaultValue: props.defaultActiveKey, }); - const mergedActiveKey = ref(); const [activeIndex, setActiveIndex] = useState(() => props.tabs.findIndex(tab => tab.key === mergedActiveKey.value), ); watchEffect(() => { - let newActiveIndex = props.tabs.findIndex(tab => tab.key === activeKey.value); + let newActiveIndex = props.tabs.findIndex(tab => tab.key === mergedActiveKey.value); if (newActiveIndex === -1) { newActiveIndex = Math.max(0, Math.min(activeIndex.value, props.tabs.length - 1)); - mergedActiveKey.value = props.tabs[newActiveIndex]?.key; - } else { - mergedActiveKey.value = activeKey.value; + setMergedActiveKey(props.tabs[newActiveIndex]?.key); } setActiveIndex(newActiveIndex); }); @@ -227,8 +222,8 @@ const InternalTabs = defineComponent({ const onInternalTabClick = (key: Key, e: MouseEvent | KeyboardEvent) => { props.onTabClick?.(key, e); const isActiveChanged = key !== mergedActiveKey.value; + setMergedActiveKey(key); if (isActiveChanged) { - mergedActiveKey.value = key; props.onChange?.(key); } };