fix: tabs control mode not work, close #5180
parent
f0385d7c24
commit
ab79b4b078
|
@ -11,7 +11,7 @@ import type {
|
||||||
Tab,
|
Tab,
|
||||||
} from './interface';
|
} from './interface';
|
||||||
import type { CSSProperties, PropType, ExtractPropTypes } from 'vue';
|
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 { flattenChildren, initDefaultProps, isValidElement } from '../../_util/props-util';
|
||||||
import useConfigInject from '../../_util/hooks/useConfigInject';
|
import useConfigInject from '../../_util/hooks/useConfigInject';
|
||||||
import useState from '../../_util/hooks/useState';
|
import useState from '../../_util/hooks/useState';
|
||||||
|
@ -181,24 +181,19 @@ const InternalTabs = defineComponent({
|
||||||
});
|
});
|
||||||
|
|
||||||
// ====================== Active Key ======================
|
// ====================== Active Key ======================
|
||||||
// use activeKey & mergedActiveKey to control
|
const [mergedActiveKey, setMergedActiveKey] = useMergedState<Key>(() => props.tabs[0]?.key, {
|
||||||
// https://github.com/vueComponent/ant-design-vue/issues/5056
|
|
||||||
const [activeKey] = useMergedState<Key>(() => props.tabs[0]?.key, {
|
|
||||||
value: computed(() => props.activeKey),
|
value: computed(() => props.activeKey),
|
||||||
defaultValue: props.defaultActiveKey,
|
defaultValue: props.defaultActiveKey,
|
||||||
});
|
});
|
||||||
const mergedActiveKey = ref<Key>();
|
|
||||||
const [activeIndex, setActiveIndex] = useState(() =>
|
const [activeIndex, setActiveIndex] = useState(() =>
|
||||||
props.tabs.findIndex(tab => tab.key === mergedActiveKey.value),
|
props.tabs.findIndex(tab => tab.key === mergedActiveKey.value),
|
||||||
);
|
);
|
||||||
|
|
||||||
watchEffect(() => {
|
watchEffect(() => {
|
||||||
let newActiveIndex = props.tabs.findIndex(tab => tab.key === activeKey.value);
|
let newActiveIndex = props.tabs.findIndex(tab => tab.key === mergedActiveKey.value);
|
||||||
if (newActiveIndex === -1) {
|
if (newActiveIndex === -1) {
|
||||||
newActiveIndex = Math.max(0, Math.min(activeIndex.value, props.tabs.length - 1));
|
newActiveIndex = Math.max(0, Math.min(activeIndex.value, props.tabs.length - 1));
|
||||||
mergedActiveKey.value = props.tabs[newActiveIndex]?.key;
|
setMergedActiveKey(props.tabs[newActiveIndex]?.key);
|
||||||
} else {
|
|
||||||
mergedActiveKey.value = activeKey.value;
|
|
||||||
}
|
}
|
||||||
setActiveIndex(newActiveIndex);
|
setActiveIndex(newActiveIndex);
|
||||||
});
|
});
|
||||||
|
@ -227,8 +222,8 @@ const InternalTabs = defineComponent({
|
||||||
const onInternalTabClick = (key: Key, e: MouseEvent | KeyboardEvent) => {
|
const onInternalTabClick = (key: Key, e: MouseEvent | KeyboardEvent) => {
|
||||||
props.onTabClick?.(key, e);
|
props.onTabClick?.(key, e);
|
||||||
const isActiveChanged = key !== mergedActiveKey.value;
|
const isActiveChanged = key !== mergedActiveKey.value;
|
||||||
|
setMergedActiveKey(key);
|
||||||
if (isActiveChanged) {
|
if (isActiveChanged) {
|
||||||
mergedActiveKey.value = key;
|
|
||||||
props.onChange?.(key);
|
props.onChange?.(key);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue