SelectBox.vue 1.96 KB
<!--
 * @Description: 下拉框组件
 * @Author: huyue
 * @Date: 2023-07-05 15:11:53
 * @LastEditors: huyue
 * @LastEditTime: 2023-07-06 12:01:43
-->
<template>
  <div v-show="isShow" class="select-box" :style="{'width': boxWidth}">
    <div class="year-item" :class="(isObject ? item[valueName] : item) == value ? 'active' : ''" v-for="(item,index) in dataList" :key="index" @click="select(item)">{{isObject ? item[labelName] : item}}</div>
  </div>
</template>
<script type='text/ecmascript-6'>
export default {
  name: 'SelectBox',
  props: {
    isShow: {
      type: Boolean,
      default: false
    },
    dataList: {
      type: Array,
      default: () => []
    },
    // dataList里面的值是否是对象
    isObject: {
      type: Boolean,
      default: false
    },
    labelName: {
      type: String,
      default: 'label'
    },
    valueName: {
      type: String,
      default: 'value'
    },
    // 下拉框的宽度设置
    boxWidth: {
      type: String
    }
  },
  data() {
    return {
      value: ''
    }
  },
  mounted() {
    const { valueName, isObject, dataList } = this
    this.value = isObject ? dataList[0][valueName] : dataList[0]
  },
  methods: {
    select(item) {
      const { valueName, isObject } = this
      this.value = isObject ? item[valueName] : item
      this.$emit('selectVal', item)
    }
  }
}
</script>
<style scoped lang='scss'>
  .select-box {
    position: absolute;
    top: 4.8vh;
    left: 0;
    background-color: #051526;
    padding: 16px 20px;
    box-sizing: border-box;
    z-index: 1;
    border: 2px solid;
    border-image: linear-gradient(90deg, #074989, #30DCFF, #074989) 1;
    .year-item {
      margin-bottom: 8px;
      color: #C9C9C9;
      cursor: pointer;
      &:hover {
        color: #FFFFFF;
      }
      &:last-child {
        margin-bottom: 0;
      }
      &.active {
        color: #FFFFFF;
      }
    }
  }
</style>