PageTabs.uvue 4.69 KB
<template>
  <view class="tabs-component flex">
    <!-- 背景层:替代 ::after 伪元素,App 端兼容 -->
    <!-- <view class="tabs-bg"></view> -->
    <view
      v-for="(item, index) in list"
      :class="['tab flex', size, { active: activeIndex == index }]"
      :key="index"
      @click="changeTab(index)"
      class="tab-item"
    >
      <text class="tab-text">{{ getTabText(item) }}</text>
      <view v-if="showDot(item)" class="dot"></view>
    </view>
    <view
      class="active-btn"
      :style="{
        width: blockWidth,
        left: activeBlockLeft
      }"
    ></view>
  </view>
</template>

<script setup lang="uts">
import { ref, computed, watch } from "vue";

/** Tab 项:对象或字符串 */
type TabItem = UTSJSONObject | string;

const props = withDefaults(
  defineProps<{
    /** Tab 数据列表 */
    list?: TabItem[]
    /** 默认选中索引 */
    defaultIndex?: number
    /** 显示名称的字段 key,默认 text */
    tabKey?: string
    /** 尺寸:normal | small */
    size?: string
  }>(),
  {
    list: () => [],
    defaultIndex: 0,
    tabKey: "text",
    size: "normal"
  }
);

const emit = defineEmits<{
  changeTab: [number]
}>();

const activeIndex = ref(props.defaultIndex != null ? props.defaultIndex : 0);

/** 用 computed 包装 defaultIndex,避免 watch getter 在 UTS 下的类型推断问题 */
const defaultIndexSource = computed((): number => {
  const v = props.defaultIndex;
  return v != null ? v : 0;
});

watch(
  defaultIndexSource,
  (newVal: number) => {
    activeIndex.value = newVal;
  },
  { immediate: true }
);

/** 每个 tab 的宽度(百分比,自适应) */
const blockWidth = computed((): string => {
  const len = props.list != null ? props.list.length : 0;
  if (len <= 0) {
    return "100%";
  }
  const pct = 100 / len;
  return `${pct}%`;
});

/** 激活滑块 left 位置:按 tab 等分,统一用 % 保证 transition 在所有切换时生效(px 与 % 混用会导致过渡失效) */
const activeBlockLeft = computed((): string => {
  const len = props.list != null ? props.list.length : 0;
  if (len <= 0) {
    return "0%";
  }
  const idx = activeIndex.value;
  const pct = (idx * 100) / len;
  return `${pct}%`;
});

/** 获取 tab 显示文本 */
function getTabText(item: TabItem): string {
  if (item == null) {
    return "";
  }
  if (typeof item == "string") {
    return item;
  }
  const obj = item as UTSJSONObject;
  const key = props.tabKey != null ? props.tabKey : "text";
  const val = obj[key];
  if (val == null) {
    return "";
  }
  return `${val}`;
}

/** 是否显示红点:支持 dot 为 boolean 或可调用函数 */
function showDot(item: TabItem): boolean {
  if (item == null) {
    return false;
  }
  if (typeof item == "string") {
    return false;
  }
  const obj = item as UTSJSONObject;
  const dot = obj["dot"];
  if (dot == null) {
    return false;
  }
  if (typeof dot == "boolean") {
    return dot;
  }
  return false;
}

function changeTab(index: number): void {
  activeIndex.value = index;
  emit("changeTab", index);
}
</script>

<style lang="scss">
/* 遵循 UniAppX 规范:px 单位、无 calc/伪元素、font-weight 仅 400|700、文字样式在 text 上 */
.tabs-component {
  display: flex;
  flex-direction: row;
  justify-content: flex-start;
  align-items: center;
  width: 100%;
  height: 100%;
  min-height: 28px;
  background-color: #f5f6f7;
  border-radius: 8px;
  position: relative;
  box-sizing: border-box;
  padding: 2px;
}

// .tabs-bg {
//   position: absolute;
//   z-index: 0;
//   left: 0;
//   top: 0;
//   width: 100%;
//   height: 100%;
//   background-color: #f5f6f7;
//   border-radius: 8px;
// }

.tab-item {
  flex: 1;
  height: 100%;
  min-height: 28px;
  display: flex;
  flex-direction: row;
  justify-content: center;
  align-items: center;
  color: #666666;
  z-index: 10;
  position: relative;
}

.tab-item .tab-text {
  font-size: 14px;
  font-weight: 400;
  color: #666666;
  text-align: center;
}

.tab-item.normal .tab-text {
  font-size: 14px;
}

.tab-item.small .tab-text {
  font-size: 12px;
}

/* 仅选中态加粗 */
.tab-item.active .tab-text {
  color: #1a1a1a;
}

.tab-item .dot {
  position: absolute;
  top: 2px;
  right: 14px;
  width: 6px;
  height: 6px;
  border-radius: 3px;
  background-color: #f24724;
}

/* 选中态滑块:宽度与 tab 等分,高度自适应,白色背景,滑动过渡 */
.active-btn {
  position: absolute;
  top: 2px;
  bottom: 2px;
  opacity: 1;
  background-color: #ffffff;
  /* 优化滑动:Material Design 缓动 + 略长时长 + GPU 加速 */
  transition: left 0.28s cubic-bezier(0.4, 0, 0.2, 1);
  transform: translateZ(0);
  border-radius: 8px;
  z-index: 1;
  box-sizing: border-box;
  font-weight: 700;
  margin-left: 2px;
}
</style>