diff --git a/packages/vant/src/coupon-list/test/__snapshots__/index.spec.ts.snap b/packages/vant/src/coupon-list/test/__snapshots__/index.spec.ts.snap index e29cb2abb..311c8b913 100644 --- a/packages/vant/src/coupon-list/test/__snapshots__/index.spec.ts.snap +++ b/packages/vant/src/coupon-list/test/__snapshots__/index.spec.ts.snap @@ -346,7 +346,7 @@ exports[`should have two "van-coupon-list__empty" classes when render coupon lis
diff --git a/packages/vant/src/tabs/Tabs.tsx b/packages/vant/src/tabs/Tabs.tsx index 7ea456177..5f20e1f67 100644 --- a/packages/vant/src/tabs/Tabs.tsx +++ b/packages/vant/src/tabs/Tabs.tsx @@ -216,7 +216,10 @@ export default defineComponent({ } }; - const setCurrentIndex = (currentIndex: number) => { + const setCurrentIndex = ( + currentIndex: number, + skipScrollIntoView?: boolean + ) => { const newIndex = findAvailableTab(currentIndex); if (!isDef(newIndex)) { @@ -236,16 +239,31 @@ export default defineComponent({ emit('change', newName, newTab.title); } } + + if (!skipScrollIntoView) { + scrollIntoView(); + } + setLine(); + + // scroll to correct position + if (stickyFixed && !props.scrollspy) { + setRootScrollTop( + Math.ceil(getElementTop(root.value!) - offsetTopPx.value) + ); + } }; // correct the index of active tab - const setCurrentIndexByName = (name: Numeric) => { + const setCurrentIndexByName = ( + name: Numeric, + skipScrollIntoView?: boolean + ) => { const matched = children.find( (tab, index) => getTabName(tab, index) === name ); const index = matched ? children.indexOf(matched) : 0; - setCurrentIndex(index); + setCurrentIndex(index, skipScrollIntoView); }; const scrollToCurrentContent = (immediate = false) => { @@ -418,23 +436,8 @@ export default defineComponent({ } ); - watch( - () => state.currentIndex, - () => { - scrollIntoView(); - setLine(); - - // scroll to correct position - if (stickyFixed && !props.scrollspy) { - setRootScrollTop( - Math.ceil(getElementTop(root.value!) - offsetTopPx.value) - ); - } - } - ); - const init = () => { - setCurrentIndexByName(props.active); + setCurrentIndexByName(props.active, true); nextTick(() => { state.inited = true; if (wrapRef.value) {