vdesign-ui 0.2.8 → 0.2.11

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.
@@ -4,7 +4,7 @@
4
4
  display: flex;
5
5
  align-items: center;
6
6
  justify-content: space-between;
7
- background-color: var(--bg-color-1);
7
+ // background-color: var(--bg-color-1);
8
8
 
9
9
  &--right {
10
10
  display: flex;
@@ -1,68 +1,94 @@
1
1
  <template>
2
- <div :class="tabClasses">
3
- <slot v-if="shouldRender" />
2
+ <div v-show="shouldShow" :class="paneClasses">
3
+ <slot v-if="shouldRender" />
4
+ <!-- 标题插槽占位符 -->
5
+ <div v-if="$slots.title" ref="title">
6
+ <slot name="title" />
7
+ </div>
4
8
  </div>
5
- </template>
6
-
7
- <script>
8
- export default {
9
+ </template>
10
+
11
+ <script>
12
+ export default {
9
13
  name: 'vd-tab',
10
14
  props: {
11
- name: [String, Number],
12
- title: {
13
- type: String,
14
- },
15
- arrow: {
16
- type: Boolean,
17
- default: false,
18
- },
15
+ name: [String, Number],
16
+ title: String,
19
17
  },
20
18
  data() {
19
+ return {
20
+ parent: null,
21
+ inited: false,
22
+ };
23
+ },
24
+ computed: {
25
+ paneClasses() {
21
26
  return {
22
- isRendered: false,
27
+ 'vd-tab__pane': true,
23
28
  };
29
+ },
30
+ isActive() {
31
+ if (!this.parent) return false;
32
+ if (this.name !== undefined) {
33
+ return this.name === this.parent.currentName;
34
+ } else {
35
+ const index = this.parent.tabs.indexOf(this);
36
+ return index === Number(this.parent.currentName);
37
+ }
24
38
  },
25
- watch: {
26
- isActive(newVal) {
27
- if (newVal && !this.isRendered) {
28
- this.isRendered = true;
29
- }
30
- },
39
+ shouldShow() {
40
+ // 当 animated 为 true 时,所有标签页都需要渲染(用于动画效果)
41
+ // animated false 时,只渲染当前激活的标签页
42
+ return this.isActive || (this.parent && this.parent.animated);
43
+ },
44
+ shouldRender() {
45
+ return this.inited || !(this.parent && this.parent.lazyRender);
46
+ },
31
47
  },
32
- computed: {
33
- isActive() {
34
- return this.computedName === this.$parent.currentValue;
35
- },
36
- tabClasses() {
37
- return {
38
- 'vd-tab': true,
39
- 'vd-tab-active': this.isActive,
40
- };
41
- },
42
- computedName() {
43
- return this.name != null
44
- ? this.name
45
- : this.$parent.tabs.indexOf(this);
46
- },
47
- shouldRender() {
48
- if (this.$parent.lazyRender) {
49
- return this.isRendered || this.isActive;
50
- } else {
51
- return true;
52
- }
53
- },
54
- computedTitle() {
55
- // Since the title slot is now handled by vd-tabs, we return the title prop
56
- return this.title;
57
- },
48
+ watch: {
49
+ isActive(val) {
50
+ if (val) {
51
+ this.inited = true;
52
+ }
53
+ },
58
54
  },
59
55
  created() {
60
- this.$parent.registerTab(this);
56
+ this.parent = this.findParent('vd-tabs');
57
+ if (this.parent) {
58
+ this.parent.addTab(this);
59
+ } else {
60
+ console.error('vd-tab must be used within vd-tabs.');
61
+ }
62
+ },
63
+ mounted() {
64
+ if (this.isActive) {
65
+ this.inited = true;
66
+ }
67
+ // 如果存在 title 插槽,调用父组件的 renderTitle 方法
68
+ if (this.$slots.title && this.$refs.title) {
69
+ this.parent.renderTitle(this.$refs.title, this);
70
+ this.$refs.title.parentNode.removeChild(this.$refs.title);
71
+
72
+ }
61
73
  },
62
74
  beforeDestroy() {
63
- this.$parent.unregisterTab(this);
75
+ if (this.parent) {
76
+ this.parent.removeTab(this);
77
+ }
78
+ },
79
+ methods: {
80
+ findParent(name) {
81
+ let parent = this.$parent;
82
+ while (parent) {
83
+ if (parent.$options && parent.$options.name === name) {
84
+ return parent;
85
+ }
86
+ parent = parent.$parent;
87
+ }
88
+ return null;
89
+ },
64
90
  },
65
- };
66
- </script>
67
-
68
- <style lang="less"></style>
91
+ };
92
+ </script>
93
+
94
+
@@ -1,29 +1,50 @@
1
1
  <template>
2
2
  <div class="vd-tabs" :class="stickyClasses">
3
- <div class="vd-tabs__wrap" :class="menuClasses" ref="tabsWrap">
4
- <div class="vd-tabs__nav" :class="[barType,scrollspy]" ref="tabsContainer">
5
- <div v-for="(item, index) in navList" :key="index" :class="[tabClasses(item), lineClasses]" @click="handleChange(index)">
6
- <span class="vd-tabs-text-cell" :class="ellipsisClasses">
7
- {{ item.title }}
8
- <vd-icon class="vd-tabs-arrow" v-if="item.arrow" name="icon_btn_moredown"></vd-icon>
3
+ <div class="vd-tabs__wrap" :class="menuClasses" ref="wrap">
4
+ <div class="vd-tabs__nav" :class="[barType, scrollspyClasses]" ref="nav">
5
+ <div
6
+ v-for="(tab, index) in tabs"
7
+ :key="tab.name !== undefined ? tab.name : index"
8
+ :class="[tabClasses(tab, index), lineClasses]"
9
+ @click="onClick(tab, index)"
10
+ ref="tabItems"
11
+ >
12
+ <span class="vd-tab__text" :class="ellipsisClasses" ref="title"
13
+ >
14
+ {{ tab.title }}
15
+ <!-- <vd-icon
16
+ class="vd-tab__arrow"
17
+ v-if="tab.arrow"
18
+ name="icon_btn_moredown"
19
+ ></vd-icon> -->
9
20
  </span>
10
21
  </div>
11
22
  </div>
12
- <div class="vd-tabs-menu--right" v-if="menu">
13
- <vd-icon :name="menuIconComputed" class="vd-tabs-menu-btn" @click="emitMenuClick"></vd-icon>
23
+ <div class="vd-tabs__menu--right" v-if="menu">
24
+ <vd-icon
25
+ :name="menuIconComputed"
26
+ class="vd-tabs__menu--btn"
27
+ @click="emitMenuClick"
28
+ ></vd-icon>
14
29
  </div>
15
30
  </div>
16
- <div class="vd-tabs__content">
17
- <slot />
31
+ <div class="vd-tabs__content" ref="content" :class="{'vd-tabs__content--animated':animated}">
32
+ <div v-if="animated" :class="trackClasses" :style="trackStyle">
33
+ <slot />
34
+ </div>
35
+ <slot v-else />
18
36
  </div>
19
37
  </div>
20
38
  </template>
21
39
 
22
40
  <script>
23
41
  import VdIcon from "../icon";
42
+ import languageMixin from '../mixins/languageMixin.js';
43
+
24
44
  const prefixCls = "vd-tabs";
25
45
  export default {
26
46
  name: "vd-tabs",
47
+ mixins: [languageMixin],
27
48
  components: {
28
49
  VdIcon,
29
50
  },
@@ -33,7 +54,6 @@ export default {
33
54
  type: String,
34
55
  default: "primary",
35
56
  },
36
- // 将 menu 作为一个字段,同时控制显示和图标名称
37
57
  menu: {
38
58
  type: [Boolean, String],
39
59
  default: false,
@@ -47,43 +67,66 @@ export default {
47
67
  divider: Boolean,
48
68
  sticky: Boolean,
49
69
  lazyRender: Boolean,
70
+ animated: Boolean,
71
+ scrollspy:Boolean
50
72
  },
51
73
  data() {
52
74
  return {
53
- currentValue: this.value, // 当前选中的标签名
54
- navList: [], // 标签导航列表
55
- tabs: [], // 已注册的标签
75
+ tabs: [],
76
+ currentName: this.value !== undefined ? this.value : 0, // 默认为 0
56
77
  };
57
78
  },
58
79
  watch: {
59
80
  // 监听 value 变化,更新 currentValue
60
81
  value(val) {
61
- this.currentValue = val;
82
+ this.currentName = val;
83
+ this.setCurrentName(val);
62
84
  },
63
85
  },
64
86
  computed: {
87
+ trackClasses() {
88
+ return {
89
+ 'vd-tabs__track': true,
90
+ };
91
+ },
92
+ trackStyle() {
93
+ const activeIndex = this.tabs.findIndex((tab, index) => {
94
+ return this.isTabActive(tab, index);
95
+ });
96
+
97
+ const offset = activeIndex * 100;
98
+ // 根据文本方向调整 translateX 值
99
+ const translateX = this.language === 'ar' ? offset : -offset;
100
+ // const translateX = -activeIndex * 100;
101
+ return {
102
+ transform: `translateX(${translateX}%)`,
103
+ transitionDuration: '0.3s',
104
+ };
105
+ },
65
106
  // 计算菜单按钮的图标,根据 menu 属性的类型决定图标
66
107
  menuIconComputed() {
67
- if (typeof this.menu === 'string') {
108
+ if (typeof this.menu === "string") {
68
109
  return this.menu;
69
110
  } else {
70
- return 'icon_tab_morelist'; // 默认的菜单图标名称
111
+ return "icon_tab_morelist"; // 默认的菜单图标名称
71
112
  }
72
113
  },
73
114
  // 计算滚动监视的类名
74
- scrollspy() {
75
- return `${prefixCls}--complete`
115
+ scrollspyClasses() {
116
+ return {
117
+ [`${prefixCls}--complete`]:this.scrollspy
118
+ }
76
119
  },
77
120
  // 计算标签文字是否需要省略号的类名
78
121
  ellipsisClasses() {
79
122
  return {
80
- [`${prefixCls}-text--ellipsis`]: this.ellipsis,
123
+ [`vd-tab__text--ellipsis`]: this.ellipsis,
81
124
  };
82
125
  },
83
126
  // 计算标签包裹器的类名,根据 menu、backgroundColor 和 divider 属性来确定
84
127
  menuClasses() {
85
128
  return {
86
- [`${prefixCls}-menu`]: this.menu,
129
+ [`${prefixCls}__menu`]: this.menu,
87
130
  [`${prefixCls}__wrap--bg`]: this.backgroundColor,
88
131
  "vd-hairline--bottom": this.divider,
89
132
  };
@@ -91,13 +134,13 @@ export default {
91
134
  // 计算标签下方线条的类名
92
135
  lineClasses() {
93
136
  return {
94
- [`${prefixCls}__line`]: !this.actBorder,
137
+ [`vd-tab__none--line`]: !this.actBorder,
95
138
  };
96
139
  },
97
140
  // 根据 tabsType 属性计算标签类型的类名
98
141
  barType() {
99
142
  return {
100
- [`${prefixCls}-${this.tabsType}-bar`]: this.tabsType,
143
+ [`${prefixCls}__nav--${this.tabsType}`]: this.tabsType,
101
144
  };
102
145
  },
103
146
  stickyClasses() {
@@ -109,71 +152,129 @@ export default {
109
152
  methods: {
110
153
  // 当菜单按钮被点击时触发事件
111
154
  emitMenuClick() {
112
- this.$emit('menu-click');
155
+ this.$emit("menu-click");
113
156
  },
114
157
  // 计算每个标签项的类名
115
- tabClasses(item) {
158
+ tabClasses(tab, index) {
159
+ const isActive = this.isTabActive(tab, index);
116
160
  return {
117
- [`${prefixCls}-${this.tabsType}`]: true,
118
- [`${prefixCls}-${this.tabsType}-active`]: item.name === this.currentValue,
161
+ 'vd-tab': true,
162
+ 'vd-tab--active': isActive,
119
163
  };
120
164
  },
121
- // 注册新的标签到 tabs 数组中
122
- registerTab(tab) {
123
- if (!this.tabs.includes(tab)) {
124
- this.tabs.push(tab);
125
- this.updateNav();
165
+ setCurrentName(name) {
166
+ // 如果没有指定 name 属性,则使用索引值,确保索引值为数字类型
167
+ const nameIsNumber = typeof name === 'number' || /^\d+$/.test(name);
168
+ const parsedName = nameIsNumber ? Number(name) : name;
169
+
170
+ const matchedTab = this.tabs.find((tab, index) => {
171
+ if (tab.name !== undefined) {
172
+ return tab.name === parsedName;
173
+ } else {
174
+ return index === parsedName;
175
+ }
176
+ });
177
+
178
+ if (matchedTab) {
179
+ this.currentName = matchedTab.name !== undefined ? matchedTab.name : this.tabs.indexOf(matchedTab);
180
+ } else if (this.tabs.length > 0) {
181
+ const firstTab = this.tabs[0];
182
+ this.currentName = firstTab.name !== undefined ? firstTab.name : 0;
183
+ } else {
184
+ this.currentName = null;
126
185
  }
186
+ this.$emit('input', this.currentName);
127
187
  },
128
- // tabs 数组中注销一个标签
129
- unregisterTab(tab) {
188
+ isTabActive(tab, index) {
189
+ if (tab.name !== undefined) {
190
+ return tab.name === this.currentName;
191
+ } else {
192
+ return index === Number(this.currentName);
193
+ }
194
+ },
195
+ addTab(tab) {
196
+ this.tabs.push(tab);
197
+ if (this.currentName === undefined) {
198
+ this.setCurrentName(tab.name !== undefined ? tab.name : 0);
199
+ }
200
+ },
201
+ removeTab(tab) {
130
202
  const index = this.tabs.indexOf(tab);
131
203
  if (index !== -1) {
132
204
  this.tabs.splice(index, 1);
133
- this.updateNav();
134
205
  }
135
206
  },
136
- // 根据已注册的标签更新导航列表
137
- updateNav() {
138
- this.navList = this.tabs.map((tab, index) => ({
139
- title: tab.title,
140
- name: tab.name != null ? tab.name : index,
141
- arrow: tab.arrow,
142
- }));
143
- // 如果当前没有选中的标签且导航列表不为空,则将第一个标签设为当前值
144
- if (!this.currentValue && this.navList.length) {
145
- this.currentValue = this.navList[0].name;
146
- this.$emit("input", this.currentValue);
207
+ onClick(tab, index) {
208
+ if (tab.disabled) {
209
+ this.$emit('disabled', tab.name !== undefined ? tab.name : index, index);
210
+ } else {
211
+ this.currentName = tab.name !== undefined ? tab.name : index;
212
+ // 如果 currentName 是索引值,确保为数字类型
213
+ if (tab.name === undefined) {
214
+ this.currentName = Number(this.currentName);
215
+ }
216
+ this.$emit('input', this.currentName);
217
+ this.$emit('change', this.currentName, index);
218
+ // 切换标签后,滚动到当前标签
219
+ this.$nextTick(() => {
220
+ this.scrollToActiveTab();
221
+ });
147
222
  }
148
223
  },
149
- handleChange(index) {
150
- const nav = this.navList[index];
151
- const name = nav.name;
152
- if (this.currentValue !== name) {
153
- this.currentValue = name;
154
- this.$emit("input", name); // 触发 input 事件,更新父组件中的 v-model 绑定值
155
- this.$emit("change", { index, title: nav.title, name }); // 触发 change 事件,传递标签的详细信息
156
- this.adjustTabPosition(index); // 调整选中标签的位置
157
- }
158
- this.$emit("click", { index, title: nav.title, name }); // 触发 click 事件,传递标签的详细信息
159
- },
160
- adjustTabPosition(index) {
161
- const container = this.$refs.tabsContainer;
162
- const selectedTab = container.children[index];
163
- if (selectedTab) {
164
- const containerWidth = container.offsetWidth;
165
- const tabWidth = selectedTab.offsetWidth;
166
- const tabOffsetLeft = selectedTab.offsetLeft;
167
- const scrollLeft = tabOffsetLeft - (containerWidth - tabWidth) / 2;
168
- container.scrollTo({ left: scrollLeft, behavior: "smooth" });
169
- }
224
+ scrollToActiveTab() {
225
+ const scrollWrapper = this.$refs.nav; // 修改这里
226
+ const tabItems = this.$refs.tabItems;
227
+
228
+ if (!scrollWrapper || !tabItems || tabItems.length === 0) {
229
+ return;
230
+ }
231
+
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;
243
+
244
+ scrollWrapper.scrollTo({
245
+ left: scrollLeft,
246
+ behavior: 'smooth',
247
+ });
248
+ }
249
+ },
250
+ renderTitle(titleEl, tab) {
251
+ this.$nextTick(() => {
252
+ const index = this.tabs.indexOf(tab);
253
+ if (index !== -1 && this.$refs.title && this.$refs.title[index]) {
254
+ const navTitleEl = this.$refs.title[index];
255
+ // 清空导航标题元素的内容
256
+ navTitleEl.innerHTML = '';
257
+ // 将子组件的 title 插槽内容移动到导航标题元素中
258
+ while (titleEl.firstChild) {
259
+ navTitleEl.appendChild(titleEl.firstChild);
260
+ }
261
+ }
262
+ });
170
263
  },
171
264
  },
172
265
  mounted() {
173
- // 组件挂载后更新导航列表
174
- // this.updateNav();
175
- },
176
- };
266
+ if (this.currentName !== undefined) {
267
+ this.setCurrentName(this.currentName);
268
+ } else if (this.tabs.length > 0) {
269
+ const firstTab = this.tabs[0];
270
+ this.currentName = firstTab.name !== undefined ? firstTab.name : 0;
271
+ }
272
+ // 初始加载时,滚动到当前激活的标签
273
+ this.$nextTick(() => {
274
+ this.scrollToActiveTab();
275
+ });
276
+ },
277
+ };
177
278
  </script>
178
279
  <style lang="less">
179
280
  @import "./style.less";