vdesign-ui 0.2.13 → 0.2.15

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -208,44 +208,61 @@ export default {
208
208
  if (tab.disabled) {
209
209
  this.$emit('disabled', tab.name !== undefined ? tab.name : index, index);
210
210
  } else {
211
- this.currentName = tab.name !== undefined ? tab.name : index;
212
- // 如果 currentName 是索引值,确保为数字类型
213
- if (tab.name === undefined) {
214
- this.currentName = Number(this.currentName);
211
+ const newName = tab.name !== undefined ? tab.name : index;
212
+
213
+ // 始终触发 click 事件
214
+ this.$emit('click', newName, index);
215
+
216
+ // 检查是否需要切换标签
217
+ if (newName !== this.currentName) {
218
+ this.currentName = newName;
219
+
220
+ // 如果 currentName 是索引值,确保为数字类型
221
+ if (tab.name === undefined) {
222
+ this.currentName = Number(this.currentName);
223
+ }
224
+
225
+ this.$emit('input', this.currentName);
226
+ this.$emit('change', this.currentName, index); // 仅在切换时触发 change 事件
227
+
228
+ // 切换标签后,滚动到当前标签
229
+ this.$nextTick(() => {
230
+ this.scrollToActiveTab();
231
+ });
215
232
  }
216
- this.$emit('input', this.currentName);
217
- this.$emit('change', this.currentName, index);
218
- // 切换标签后,滚动到当前标签
219
- this.$nextTick(() => {
220
- this.scrollToActiveTab();
221
- });
222
233
  }
223
234
  },
224
- scrollToActiveTab() {
225
- const scrollWrapper = this.$refs.nav; // 修改这里
226
- const tabItems = this.$refs.tabItems;
227
-
228
- if (!scrollWrapper || !tabItems || tabItems.length === 0) {
235
+ scrollToActiveTab(targetIndex = null) {
236
+ const scrollWrapper = this.$refs.nav;
237
+ const tabItems = this.$refs.tabItems;
238
+ if (!scrollWrapper || !tabItems || tabItems.length === 0) {
239
+ return;
240
+ }
241
+ // 如果传入 targetIndex,设置当前激活标签的 name
242
+ if (targetIndex !== null) {
243
+ const targetTab = this.tabs[targetIndex];
244
+ if (targetTab) {
245
+ this.currentName = targetTab.name !== undefined ? targetTab.name : targetIndex;
246
+ } else {
247
+ console.warn('Invalid target index:', targetIndex);
229
248
  return;
230
249
  }
250
+ }
231
251
 
232
- const activeIndex = this.tabs.findIndex((tab, index) => {
233
- return this.isTabActive(tab, index);
234
- });
235
-
236
- const activeTab = tabItems[activeIndex];
237
-
238
- if (activeTab) {
239
- const tabOffsetLeft = activeTab.offsetLeft;
240
- const tabWidth = activeTab.offsetWidth;
241
- const wrapperWidth = scrollWrapper.offsetWidth;
242
- const scrollLeft = tabOffsetLeft - (wrapperWidth - tabWidth) / 2;
252
+ const activeIndex = targetIndex !== null ? targetIndex : this.tabs.findIndex((tab, index) => this.isTabActive(tab, index));
253
+ const activeTab = tabItems[activeIndex];
243
254
 
244
- scrollWrapper.scrollTo({
245
- left: scrollLeft,
246
- behavior: 'smooth',
247
- });
248
- }
255
+ setTimeout(() => {
256
+ const tabOffsetLeft = activeTab.offsetLeft;
257
+ const tabWidth = activeTab.offsetWidth;
258
+ const wrapperWidth = scrollWrapper.offsetWidth;
259
+ const scrollLeft = tabOffsetLeft - (wrapperWidth - tabWidth) / 2;
260
+
261
+ scrollWrapper.scrollTo({
262
+ left: scrollLeft,
263
+ behavior: 'smooth'
264
+ });
265
+ }, 0); // You can adjust the timeout value if needed
249
266
  },
250
267
  renderTitle(titleEl, tab) {
251
268
  this.$nextTick(() => {
@@ -261,6 +278,26 @@ export default {
261
278
  }
262
279
  });
263
280
  },
281
+ /**
282
+ * resize 方法
283
+ * 当外层元素大小或组件显示状态变化时调用,触发重绘
284
+ */
285
+ resize() {
286
+ // 重新计算当前激活标签的位置
287
+ this.scrollToActiveTab();
288
+ // 如果有其他需要重绘或重新计算的逻辑,可以在这里添加
289
+ // 例如,重新计算动画轨道的位置等
290
+ },
291
+ /**
292
+ * scrollTo 方法
293
+ * 刷新页面,滚动到指定位置
294
+ */
295
+ scrollTo(targetIndex = null){
296
+ // 重新计算当前激活标签的位置
297
+ this.scrollToActiveTab(targetIndex);
298
+ // 如果有其他需要重绘或重新计算的逻辑,可以在这里添加
299
+ // 例如,重新计算动画轨道的位置等
300
+ }
264
301
  },
265
302
  mounted() {
266
303
  if (this.currentName !== undefined) {
@@ -271,10 +308,10 @@ export default {
271
308
  }
272
309
  // 初始加载时,滚动到当前激活的标签
273
310
  this.$nextTick(() => {
274
- this.scrollToActiveTab();
275
- });
276
- },
277
- };
311
+ this.scrollToActiveTab();
312
+ });
313
+ },
314
+ };
278
315
  </script>
279
316
  <style lang="less">
280
317
  @import "./style.less";