TwoLevelSelectComponent.vue 4.33 KB
<template>
  <view class="two-level-select-box">
    <view
      class="select"
      @click.stop="isSelect = !isSelect"
    >
      {{ currentLabel }}
      <view class="sprite-icon icon-arrow-bottom"></view>
    </view>
    <view
      class="pop-box"
      v-if="isSelect"
    >
      <!-- 一级菜单 -->
      <view
        v-for="(item, index) in selectList"
        :key="index"
        class="type-item flex"
        :class="getItemClass(index, item)"
        @click="chooseFirstLevel(index, item)"
      >
        <view class="item-label">{{ item[labelKey] }}</view>
      </view>
      
      <!-- 二级菜单 -->
      <view
        class="second-level-box"
        v-if="showSecondLevel && currentFirstLevel && currentFirstLevel.children"
      >
        <view
          v-for="(child, childIndex) in currentFirstLevel.children"
          :key="childIndex"
          class="type-item flex"
          :class="childIndex === selectSecondIndex ? 'active' : ''"
          @click.stop="chooseSecondLevel(childIndex, child)"
        >
          {{ child[labelKey] }}
        </view>
      </view>
    </view>
  </view>
</template>

<script>
export default {
  props: {
    selectList: Array,
    labelKey: {
      type: String,
      default: 'label'
    }
  },
  name: 'TwoLevelSelectComponent',
  data() {
    return {
      selectFirstIndex: 0,
      selectSecondIndex: -1,
      isSelect: false,
      showSecondLevel: false,
      currentFirstLevel: null
    }
  },
  computed: {
    currentLabel() {
      const firstItem = this.selectList[this.selectFirstIndex]
      if (!firstItem) return ''
      
      // 如果有选中的二级分类
      if (this.selectSecondIndex >= 0 && firstItem.children && firstItem.children[this.selectSecondIndex]) {
        return firstItem.children[this.selectSecondIndex][this.labelKey]
      }
      
      // 否则显示一级分类
      return firstItem[this.labelKey]
    }
  },
  methods: {
    getItemClass(index, item) {
      const classes = []
      if (index === this.selectFirstIndex && this.selectSecondIndex === -1) {
        classes.push('active')
      }
      if (item.children && item.children.length) {
        classes.push('has-children')
      }
      return classes.join(' ')
    },
    hideSelect() {
      this.isSelect = false
      this.showSecondLevel = false
    },
    chooseFirstLevel(index, item) {
      // 如果有二级菜单,显示二级菜单
      if (item.children && item.children.length) {
        this.showSecondLevel = true
        this.currentFirstLevel = item
        this.selectFirstIndex = index
      } else {
        // 没有二级菜单,直接选中
        this.selectFirstIndex = index
        this.selectSecondIndex = -1
        this.isSelect = false
        this.showSecondLevel = false
        this.$emit('chooseType', { item, index, level: 1 })
      }
    },
    chooseSecondLevel(childIndex, child) {
      this.selectSecondIndex = childIndex
      this.isSelect = false
      this.showSecondLevel = false
      this.$emit('chooseType', { 
        item: child, 
        index: childIndex,
        parentIndex: this.selectFirstIndex,
        parent: this.currentFirstLevel,
        level: 2 
      })
    }
  }
}
</script>

<style scoped lang='scss'>
.two-level-select-box {
  position: relative;
  z-index: 5;
  
  .select {
    display: flex;
    align-items: center;
    justify-content: flex-end;
    height: toRpx(84);
    border-radius: toRpx(16);
    opacity: 1;
    background: #f2f2f2;
    padding: 0 toRpx(24) 0 toRpx(40);
  }
  
  .pop-box {
    position: absolute;
    top: toRpx(100);
    left: 0;
    width: 100%;
    background: #ffffff;
    box-shadow: 0 0 toRpx(16) 0 #99999933;
    border-radius: toRpx(16);
    padding: toRpx(16) 0;
    text-align: center;
    animation: show-view-opacity 0.15s forwards;
    
    .type-item {
      padding: 0 toRpx(28);
      margin-bottom: toRpx(8);
      height: toRpx(84);
      justify-content: center;
      align-items: center;
      
      &:last-child {
        margin-bottom: 0;
      }
      
      &.active {
        background-color: #ecf1ff;
      }
    }
  }
  
  .second-level-box {
    position: absolute;
    top: 0;
    left: 100%;
    width: 100%;
    background: #ffffff;
    box-shadow: 0 0 toRpx(16) 0 #99999933;
    border-radius: toRpx(16);
    padding: toRpx(16) 0;
    
    .type-item {
      justify-content: center;
    }
  }
}
</style>