TypeTabComponent.uvue 7.59 KB
<template>
  <view class="type-tab">
    <template v-if="styleV2">
      <!-- 样式一:固定宽度 tab,滑动背景滑块 -->
      <view class="type-box">
        <view
          :class="['slider-block', getSliderBlockClass()]"
          :style="{ left: blockLeft, width: sliderBlockWidth }"
        ></view>
        <view
          :class="['tab-item', { active: tabIndex == idx }]"
          v-for="(item, idx) in typeList"
          :key="idx"
          :style="{ width: getTabItemWidth(idx) }"
          @click="handleTabClick(idx)"
        >
          <view
            class="text-wrap"
            :class="{ active: tabIndex == idx }"
            :style="{ width: getTabItemWidth(idx) }"
          >
            <text class="tab-text">{{ getItemName(item) }}</text>
            <view v-if="tabIndex == idx" class="underline-wrap"></view>
          </view>
        </view>
      </view>
      <!-- <view class="mask-box"></view> -->
    </template>
    <template v-else>
      <!-- 样式二:横向滚动 tab,切换时平滑滑动到选中项 -->
      <scroll-view
        scroll-x
        enable-flex
        class="scroll-box"
        :scroll-into-view="`tab-id-${tabIndex}`"
        scroll-with-animation
      >
        <view class="type-box-v2">
          <view
            :class="['tab-item-v2', { active: tabIndex == idx }]"
            v-for="(item, idx) in typeList"
            :key="idx"
            :style="getTabItemV2Style(idx)"
            @click="handleTabClick(idx)"
            :id="`tab-id-${idx}`"
          >
            <template v-if="idx == 0">
              <view class="sprite-icon icon-first-right"></view>
            </template>
            <template v-else>
              <view class="sprite-icon icon-second-left"></view>
              <view class="sprite-icon icon-second-right"></view>
            </template>
            <view class="text-wrap-v2">
              <text class="tab-text-v2">{{ getItemName(item) }}</text>
            </view>
            <view
              v-if="tabIndex == idx"
              class="underline-bar"
            ></view>
          </view>
        </view>
      </scroll-view>
    </template>
  </view>
</template>

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

/** Tab 项数据类型 */
type TabItem = UTSJSONObject;

const props = withDefaults(
  defineProps<{
    /** Tab 数据列表 */
    typeList?: TabItem[]
    /** 当前选中索引 */
    tabIndex?: number
    /** 显示名称的字段 key,默认 tabName */
    nameKey?: string
    /** 是否使用样式二(横向滚动) */
    styleV2?: boolean
  }>(),
  {
    typeList: () => [],
    tabIndex: 0,
    nameKey: "tabName",
    styleV2: false
  }
);

const emit = defineEmits<{
  "update:tabIndex": [number]
  tabClick: [number]
}>();

const FIRST_TAB_ITEM_WIDTH = 152
const TAB_ITEM_WIDTH = 208

function getTabItemWidth(index: number): string {
  if (index == 0) {
    return `${FIRST_TAB_ITEM_WIDTH}px`
  }
  return `${TAB_ITEM_WIDTH}px`
}

const sliderBlockWidth = computed((): string => {
  return getTabItemWidth(props.tabIndex)
})

/** 滑块位置(样式一) */
const blockLeft = computed((): string => {
  const idx = props.tabIndex
  if (idx <= 0) {
    return "0px"
  }
  const left = FIRST_TAB_ITEM_WIDTH + (idx - 1) * TAB_ITEM_WIDTH
  return `${left}px`
});

/** 获取 tab 项显示名称 */
function getItemName(item: TabItem): string {
  const key = props.nameKey
  if (key == null || key == "") {
    return ""
  }
  const val = item[key]
  if (val == null) {
    return ""
  }
  return `${val}`
}

/** 获取滑块圆角 class(样式一) */
function getSliderBlockClass(): string {
  const idx = props.tabIndex
  const len = props.typeList != null ? props.typeList.length : 0
  if (len <= 1) {
    return "slider-only"
  }
  if (idx == 0) {
    return "slider-first"
  }
  if (idx == len - 1) {
    return "slider-last"
  }
  return "slider-middle"
}

/** 获取 tab-item-v2 的内联样式,tabIndex == 1 时非激活项追加左下/右下圆角及红色背景 */
function getTabItemV2Style(idx: number): UTSJSONObject {
  if (props.tabIndex == 1 && idx != props.tabIndex) {
    return {
      width: getTabItemWidth(idx),
      borderBottomLeftRadius: '24px',
      borderBottomRightRadius: '24px',
      backgroundColor: '#eef4fe'
    }
  }
  return { width: getTabItemWidth(idx) }
}

/** 点击 tab */
function handleTabClick(index: number): void {
  if (props.tabIndex == index) {
    return
  }
  emit("update:tabIndex", index)
  emit("tabClick", index)
}
</script>

<style scoped lang="scss">
/* 遵循 uni-app x 规范:px 单位、无 vh/calc/gap、font-weight 仅 400|700、伪元素改为真实节点 */
.type-tab {
  position: relative;
}

.type-box {
  position: relative;
  display: flex;
  flex-direction: row;
  width: 100%;
  background-color: #eef4fe;
  height: 80px;
  border-radius: 24px 24px 0 0;
  overflow: hidden;
  z-index: 0;
}

.tab-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  position: relative;
}

.tab-item .text-wrap {
  position: relative;
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: center;
}

.tab-item .text-wrap.active {
  position: relative;
  z-index: 1;
}

.tab-item .tab-text {
  font-size: 20px;
  font-weight: 400;
  color: #1f2133;
  text-align: center;
}

.tab-item .text-wrap.active .tab-text {
  font-weight: 700;
  color: #2c66f7;
}

.tab-item .underline-wrap {
  position: absolute;
  bottom: 0;
  left: 50%;
  margin-left: -12px;
  width: 24px;
  height: 4px;
  background-color: #2c66f7;
  border-radius: 8px;
}

/* 样式一:滑动背景滑块,left 由 :style 动态绑定,切换时平滑过渡 */
.type-box .slider-block {
  position: absolute;
  top: 0;
  height: 80px;
  z-index: 0;
  background-color: #fff;
  transition: left 0.2s ease-out;
}

.slider-block.slider-first {
  border-radius: 24px 0 0 0;
}

.slider-block.slider-last {
  border-radius: 0 24px 0 0;
}

.slider-block.slider-middle {
  border-radius: 0;
}

.slider-block.slider-only {
  border-radius: 24px 24px 0 0;
}

// .mask-box {
//   position: absolute;
//   height: 100%;
//   width: 100%;
//   background: #fff;
//   left: 0;
//   top: 80px;
//   border-radius: 24px;
//   overflow: hidden;
// }

.scroll-box {
  width: 100%;
}

.type-box-v2 {
  position: relative;
  display: flex;
  flex-direction: row;
  flex-wrap: nowrap;
  width: 100%;
  background-color: #eef4fe;
  height: 48px;
  border-radius: 24px 24px 0 0;
  z-index: 0;
}

.tab-item-v2 {
  padding: 0 20px;
  margin-right: 0;
  flex-shrink: 0;
  position: relative;
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: center;
  background-color: #eef4fe;
  transition: background-color 0.25s ease-out;
}

.tab-item-v2:first-child {
  padding-left: 20px;
}

.tab-item-v2 .text-wrap-v2 {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  z-index: 2;
}

.tab-item-v2 .tab-text-v2 {
  color: #292d33;
  font-size: 16px;
}

.tab-item-v2.active .tab-text-v2 {
  color: #3b73ff;
  font-weight: 400;
}

.tab-item-v2 .underline-bar {
  position: absolute;
  bottom: 0;
  left: 50%;
  margin-left: -10px;
  width: 20px;
  height: 3px;
  border-radius: 2px;
  background: #3b73ff;
}

.tab-item-v2 .sprite-icon {
  position: absolute;
  display: none;
}

.tab-item-v2.active .sprite-icon {
  display: flex;  // ✅ UVUE 不支持 display: block,改为 flex
}

.tab-item-v2.active {
  background: #fff;
  border-radius: 24px 24px 0 0;
  overflow: hidden;
  z-index: 1;
}

.icon-first-right {
  right: -50px;
}

.icon-second-left {
  left: -50px;
}

.icon-second-right {
  right: -50px;
  background-color: #eef4fe;
}
</style>