uni-app小程序开发-下拉菜单的实现

前言

uni-ui中有uni-combox组件,但是没有下拉的按钮组。

https://uniapp.dcloud.net.cn/component/uniui/uni-combox.html

我们可以自己实现一个。

image-20240729110754716

自定义组件

z-drop-down-menu.vue

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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<template>
<view class="z-drop-down-menu">
<view class="z-drop-down-menu__input-box" @click="toggleSelector">
<slot></slot>
<uni-icons class="stat_icon" :type="showSelector ? 'top' : 'bottom'" size="14" color="#999"></uni-icons>
</view>
<view class="z-drop-down-menu__selector" v-if="showSelector">
<view class="uni-popper__arrow"></view>
<scroll-view scroll-y="true" class="z-drop-down-menu__selector-scroll">
<view class="z-drop-down-menu__selector-empty" v-if="candidates === 0">
<text>{{ emptyTips }}</text>
</view>
<view class="z-drop-down-menu__selector-item" v-for="(item, index) in candidates" :key="index" @click="onSelectorClick(index)">
<text>{{ item }}</text>
</view>
</scroll-view>
</view>
</view>
</template>

<script>
/**
* 下拉菜单
* @description
* @property {String} placeholder 输入框占位符
* @property {Array} candidates 候选项列表
* @property {String} emptyTips 筛选结果为空时显示的文字
*/
export default {
name: 'z-drop-down-menu',
emits: ['changeSelect'],
props: {
tag: {
type: [Object, Number, String],
default: null
},
placeholder: {
type: String,
default: ''
},
candidates: {
type: Array,
default() {
return [];
}
},
emptyTips: {
type: String,
default: '无匹配项'
}
},
data() {
return {
compId: '',
showSelector: false
};
},
computed: {},
watch: {},
beforeMount() {},
methods: {
toggleSelector() {
this.showSelector = !this.showSelector;
},
onSelectorClick(index) {
this.showSelector = false;
this.$emit('changeSelect', {
opt: this.candidates[index],
tag: this.tag
});
}
}
};
</script>

<style lang="scss" scoped>
.z-drop-down-menu {
font-size: 14px;
padding: 6px 10px;
position: relative;
width: 100%;
z-index: 999;
}

.stat_icon {
margin-left: 10rpx;
}

.z-drop-down-menu__input-box {
position: relative;
display: flex;
align-items: center;
}

.z-drop-down-menu__input-plac {
font-size: 14px;
color: #999;
}

.z-drop-down-menu__selector {
/* #ifndef APP-NVUE */
box-sizing: border-box;
/* #endif */
position: absolute;
top: calc(100% + 12px);
left: 0;
width: 100%;
background-color: #ffffff;
border: 1px solid #ebeef5;
border-radius: 6px;
box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
z-index: 2;
padding: 4px 0;
}

.z-drop-down-menu__selector-scroll {
/* #ifndef APP-NVUE */
max-height: 200px;
box-sizing: border-box;
/* #endif */
}

.z-drop-down-menu__selector-empty,
.z-drop-down-menu__selector-item {
/* #ifndef APP-NVUE */
display: flex;
cursor: pointer;
/* #endif */
line-height: 36px;
font-size: 14px;
text-align: center;
// border-bottom: solid 1px #DDDDDD;
padding: 0px 10px;
}

.z-drop-down-menu__selector-item:hover {
background-color: #f9f9f9;
}

.z-drop-down-menu__selector-empty:last-child,
.z-drop-down-menu__selector-item:last-child {
/* #ifndef APP-NVUE */
border-bottom: none;
/* #endif */
}

// picker 弹出层通用的指示小三角
.uni-popper__arrow,
.uni-popper__arrow::after {
position: absolute;
display: block;
width: 0;
height: 0;
border-color: transparent;
border-style: solid;
border-width: 6px;
}

.uni-popper__arrow {
filter: drop-shadow(0 2px 12px rgba(0, 0, 0, 0.03));
top: -6px;
left: 10%;
margin-right: 3px;
border-top-width: 0;
border-bottom-color: #ebeef5;
}

.uni-popper__arrow::after {
content: ' ';
top: 1px;
margin-left: -6px;
border-top-width: 0;
border-bottom-color: #fff;
}

.z-drop-down-menu__no-border {
border: none;
}
</style>

使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<z-drop-down-menu :candidates="options" :tag="index" @changeSelect="itemSelect">
<view class="z_text_base">更多</view>
</z-drop-down-menu>

<script>
export default {
data() {
return {
options: ['买断', '续租', '评价']
};
},
methods: {
itemSelect(item) {
let { opt, tag } = item;
console.info(opt);
console.info(tag);
}
}
};
</script>