@@ -335,10 +335,10 @@ exports[`swipe to switch tab 3`] = `
-
title1
-
title2
+
title1
+
title2
title3
-
+
diff --git a/src/tab/test/index.spec.js b/src/tab/test/index.spec.js
index 1ca16f2a1..df69e2d2e 100644
--- a/src/tab/test/index.spec.js
+++ b/src/tab/test/index.spec.js
@@ -61,12 +61,17 @@ test('swipe to switch tab', async () => {
const onChange = jest.fn();
const wrapper = mount({
template: `
-
+
Text
Text
Text
`,
+ data() {
+ return {
+ active: 0,
+ };
+ },
methods: {
onChange,
},
@@ -174,7 +179,7 @@ test('name prop', async () => {
const wrapper = mount({
template: `
-
+
Text
Text
Text
@@ -185,6 +190,11 @@ test('name prop', async () => {
onChange,
onDisabled,
},
+ data() {
+ return {
+ active: 0,
+ };
+ },
});
await later();
diff --git a/src/tab/test/insert.spec.js b/src/tab/test/insert.spec.js
index 3c28b4053..3bf617316 100644
--- a/src/tab/test/insert.spec.js
+++ b/src/tab/test/insert.spec.js
@@ -27,9 +27,10 @@ test('insert tab dynamically', async () => {
});
test('insert tab with name dynamically', async () => {
+ const onChange = jest.fn();
const wrapper = mount({
template: `
-
+
2
1
@@ -37,14 +38,18 @@ test('insert tab with name dynamically', async () => {
data() {
return {
insert: false,
- active: [{ name: 'foo', title: 'foo' }],
+ active: 'foo',
};
},
+ methods: {
+ onChange,
+ },
});
await later();
wrapper.setData({ insert: true });
expect(wrapper).toMatchSnapshot();
+ expect(onChange).toHaveBeenCalledTimes(0);
});
// this case will throw wierd error in index.spec.js
diff --git a/src/tabs/index.js b/src/tabs/index.js
index 4431d447e..491cea15c 100644
--- a/src/tabs/index.js
+++ b/src/tabs/index.js
@@ -240,19 +240,23 @@ export default createComponent({
},
setCurrentIndex(currentIndex) {
- currentIndex = this.findAvailableTab(currentIndex);
+ const newIndex = this.findAvailableTab(currentIndex);
- if (isDef(currentIndex) && currentIndex !== this.currentIndex) {
- const shouldEmitChange = this.currentIndex !== null;
- this.currentIndex = currentIndex;
- this.$emit('input', this.currentName);
+ if (!isDef(newIndex)) {
+ return;
+ }
+
+ const newTab = this.children[newIndex];
+ const newName = newTab.computedName;
+ const shouldEmitChange = this.currentIndex !== null;
+
+ this.currentIndex = newIndex;
+
+ if (newName !== this.active) {
+ this.$emit('input', newName);
if (shouldEmitChange) {
- this.$emit(
- 'change',
- this.currentName,
- this.children[currentIndex].title
- );
+ this.$emit('change', newName, newTab.title);
}
}
},