SelectBox.vue 4.17 KB
<!--
 * @Description: 下拉框组件
 * @Author: huyue
 * @Date: 2023-07-05 15:11:53
 * @LastEditors: huyue
 * @LastEditTime: 2023-08-14 14:06:17
-->
<template>
  <div v-click-outside="handleClickOutside" @mouseleave="handleMouseLeave" v-show="isShow" class="select-box" :style="{'width': boxWidth}">
    <slot name="search"></slot>
    <div v-if="dataList.length > 0" class="select-scroll">
      <div class="select-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>
    <div v-else class="empty">
      <slot name="empty"></slot>
    </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
    },
    defaultValue: {
      type: [String, Number, Object]
    }
  },
  directives: {
    'click-outside': {
      bind: function(el, binding, vnode) {
        el.clickOutsideEvent = function(event) {
          // 判断点击事件是否在绑定元素的内部
          if (!(el == event.target || el.contains(event.target))) {
            // 调用绑定的回调函数
            vnode.context[binding.expression](event)
          }
        }
        document.body.addEventListener('click', el.clickOutsideEvent)
      },
      unbind: function(el) {
        document.body.removeEventListener('click', el.clickOutsideEvent)
      }
    }
  },
  data() {
    return {
      value: '',
      empty: '暂无数据'
    }
  },
  watch: {
    'defaultValue'(val) {
      this.value = val
    }
  },
  mounted() {
    const { valueName, isObject, dataList, defaultValue } = this
    const isDefaultObject = (defaultValue instanceof Object)
    this.value = isDefaultObject ? defaultValue[valueName] : defaultValue || (isObject ? dataList[0][valueName] : dataList[0])
  },
  methods: {
    select(item) {
      const { valueName, isObject } = this
      this.value = isObject ? item[valueName] : item
      this.$emit('selectVal', item)
    },
    handleMouseLeave() {
      // 在此处处理鼠标移出事件的逻辑
    },
    handleClickOutside() {
      this.$emit('clickOutside')
      // 在此处处理鼠标点击事件的逻辑
    }
  }
}
</script>
<style scoped lang='scss'>
  .select-box {
    position: absolute;
    top: 4.8vh;
    left: 0;
    background-color: #051526;
    padding: 16px 0;
    box-sizing: border-box;
    z-index: 1;
    border: 2px solid;
    border-image: linear-gradient(90deg, #074989, #30DCFF, #074989) 1;
    .select-item {
      margin-bottom: 8px;
      color: #C9C9C9;
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
      padding: 0 1.39vw;
      cursor: pointer;
      &:hover {
        color: #5894FF;
        background-image: -webkit-linear-gradient(left, #24D8FF, #7787FF);
        -webkit-background-clip: text;
        -webkit-text-fill-color: transparent;
      }
      &:last-child {
        margin-bottom: 0;
      }
      &.active {
        color: #5894FF;
        background-image: -webkit-linear-gradient(left, #24D8FF, #7787FF);
        -webkit-background-clip: text;
        -webkit-text-fill-color: transparent;
      }
    }
  }
  .select-scroll {
    max-height: 23.33vh;
    overflow-y: auto;
  }
</style>
<style>
::-webkit-scrollbar {
  width: 5px; /* 设置滚动条的宽度 */
}
::-webkit-scrollbar-track {
  background-image: linear-gradient(90deg, #0A214A 0%, #061422 50%, #0A214A 100%);
 /* 设置滚动条的轨道背景颜色 */
}
::-webkit-scrollbar-thumb {
  border-radius: 5px;
  background-color: rgba(244, 244, 244, 0.26); /* 设置滑块的颜色 */
}
</style>