SelectBox.vue
2.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<!--
* @Description: 下拉框组件
* @Author: huyue
* @Date: 2023-07-05 15:11:53
* @LastEditors: huyue
* @LastEditTime: 2023-07-12 15:46:51
-->
<template>
<div v-click-outside="handleClickOutside" @mouseleave="handleMouseLeave" v-show="isShow" class="select-box" :style="{'width': boxWidth}">
<slot name="search"></slot>
<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
}
},
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: ''
}
},
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)
},
handleMouseLeave() {
// 在此处处理鼠标移出事件的逻辑
},
handleClickOutside() {
this.$emit('clickOutside')
// 在此处处理鼠标点击事件的逻辑
}
}
}
</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;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
cursor: pointer;
&:hover {
color: #FFFFFF;
}
&:last-child {
margin-bottom: 0;
}
&.active {
color: #FFFFFF;
}
}
}
</style>