TabList.vue 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <template>
  2. <div
  3. class="tabs-container"
  4. :class="[`tabs-type-${type}`, { 'tabs-small': tabs.length <= 2 }]"
  5. ref="tabsContainerRef"
  6. >
  7. <div
  8. v-for="(tab, index) in tabs"
  9. :key="index"
  10. class="tab-item"
  11. :class="{ active: isActive(index, tab) }"
  12. @click="handleTabClick(index, tab)"
  13. >
  14. {{ getTabTitle(tab) }}
  15. </div>
  16. </div>
  17. </template>
  18. <script setup>
  19. import { ref, watch, nextTick } from "vue";
  20. const props = defineProps({
  21. tabs: {
  22. type: Array,
  23. required: true,
  24. default: () => [],
  25. },
  26. modelValue: {
  27. type: [Number, String],
  28. default: 0,
  29. },
  30. type: {
  31. type: String,
  32. default: "default", // 'default' | 'light'
  33. validator: (value) => ["default", "light"].includes(value),
  34. },
  35. // 控制滚动方向:'left' | 'right' | 'auto' | ''
  36. scrollType: {
  37. type: String,
  38. default: "",
  39. },
  40. });
  41. const emit = defineEmits(["update:modelValue", "change"]);
  42. const tabsContainerRef = ref(null);
  43. // 判断是否为对象格式的 tab
  44. const isObjectTab = (tab) => {
  45. return (
  46. typeof tab === "object" &&
  47. tab !== null &&
  48. (tab.title !== undefined || tab.label !== undefined || tab.name !== undefined)
  49. );
  50. };
  51. // 获取 tab 的标题
  52. const getTabTitle = (tab) => {
  53. if (isObjectTab(tab)) {
  54. return tab.title || tab.label || tab.name || "";
  55. }
  56. return tab;
  57. };
  58. // 判断当前 tab 是否激活
  59. const isActive = (index, tab) => {
  60. if (isObjectTab(tab)) {
  61. // 对象格式:比较 value 或 id
  62. if (tab.value) {
  63. return props.modelValue === tab.value;
  64. } else {
  65. return props.modelValue === tab.id;
  66. }
  67. }
  68. // 字符串/简单格式:比较索引
  69. return props.modelValue === index;
  70. };
  71. const handleTabClick = (index, tab) => {
  72. if (isObjectTab(tab)) {
  73. // 对象格式:传递 value
  74. emit("update:modelValue", tab.value);
  75. emit("change", tab.value || tab.id, tab, index);
  76. } else {
  77. // 字符串/简单格式:传递索引
  78. emit("update:modelValue", index);
  79. emit("change", index, tab);
  80. }
  81. };
  82. // 根据 scrollType 控制滚动:left -> 最左边;right -> 最右边;auto/空 -> 不动
  83. const scrollByType = (type) => {
  84. const wrap = tabsContainerRef.value;
  85. if (!wrap) return;
  86. if (type === "left") {
  87. wrap.scrollTo({
  88. left: 0,
  89. behavior: "smooth",
  90. });
  91. return;
  92. }
  93. if (type === "right") {
  94. wrap.scrollTo({
  95. left: wrap.scrollWidth,
  96. behavior: "smooth",
  97. });
  98. }
  99. };
  100. watch(
  101. () => props.scrollType,
  102. (val) => {
  103. if (!val || val === "auto") return;
  104. nextTick(() => {
  105. scrollByType(val);
  106. });
  107. }
  108. );
  109. </script>
  110. <style scoped lang="scss">
  111. .tabs-container {
  112. display: flex;
  113. align-items: center;
  114. gap: 8px;
  115. overflow-x: auto;
  116. overflow-y: hidden;
  117. -webkit-overflow-scrolling: touch;
  118. scrollbar-width: none; /* Firefox */
  119. -ms-overflow-style: none; /* IE and Edge */
  120. &::-webkit-scrollbar {
  121. display: none; /* Chrome, Safari, Opera */
  122. }
  123. .tab-item {
  124. border-radius: 20px;
  125. text-align: center;
  126. white-space: nowrap;
  127. flex-shrink: 0;
  128. }
  129. // 标签数量 <= 2 时的样式
  130. &.tabs-small {
  131. gap: 8px;
  132. .tab-item {
  133. padding: 4px 8px;
  134. }
  135. }
  136. // 默认样式
  137. &.tabs-type-default {
  138. justify-content: center;
  139. background: #fff;
  140. padding: 5px 0;
  141. .tab-item {
  142. padding: 4px 12px;
  143. color: rgba(0, 0, 0, 0.5);
  144. border: 1px solid transparent;
  145. &.active {
  146. background: rgba(33, 153, 248, 0.1);
  147. color: #2199F8;
  148. border: 1px solid #2199F8;
  149. }
  150. }
  151. }
  152. // 浅色样式(激活:浅蓝色背景+深蓝色文字,未激活:浅灰色背景+中灰色文字)
  153. &.tabs-type-light {
  154. .tab-item {
  155. background: #F7F8FA;
  156. color: #8B8B8B;
  157. padding: 4px 12px;
  158. &.active {
  159. background: rgba(33, 153, 248, 0.1);
  160. color: #2199F8;
  161. }
  162. }
  163. }
  164. }
  165. </style>