uv-drop-down-item.vue
4.17 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
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
<template>
<view class="uv-drop-down-item" @click="clickHandler">
<uv-text :text="label" :size="getTextStyle.size" :color="getTextStyle.color" lines="1" :custom-style="{marginRight: '10rpx',maxWidth:'200rpx'}"></uv-text>
<uv-icon :name="getDownIcon.name" :size="getDownIcon.size" :color="getDownIcon.color" v-if="[1,'1'].indexOf(type)==-1"></uv-icon>
</view>
</template>
<script>
import mpMixin from '@/uni_modules/uv-ui-tools/libs/mixin/mpMixin.js';
import mixin from '@/uni_modules/uv-ui-tools/libs/mixin/mixin.js';
/**
* DropDown 下拉框
* @description 下拉筛选
* @tutorial https://ext.dcloud.net.cn/plugin?name=uv-drop-down
* @property {String | Number} name 字段标识
* @property {String | Number} type 类型 1 没有筛选项,直接进行选中和不选中 2 有多个选项
* @property {String | Number} label 筛选项的文本
* @property {Boolean} isDrop 该项是否打开
*/
export default {
name: 'uv-drop-down-item',
mixins: [mpMixin, mixin],
emits: ['click'],
props: {
name: {
type: [String, Number],
default: ''
},
// 类型 1 没有筛选项,直接进行选中和不选中 2 有多个选项
type: {
type: [String, Number],
default: '2'
},
// 筛选的文本
label: {
type: [String],
default: ''
},
// 筛选值
value: {
type: [String, Number, null],
default: ''
},
// 是否下拉菜单打开
isDrop: {
type: Boolean,
default: false
}
},
data() {
return {
parentData: {
defaultValue: [0, '0', 'all'],
textSize: '30rpx',
textColor: '#333',
textActiveSize: '30rpx',
textActiveColor: '#3c9cff',
extraIcon: {},
extraActiveIcon: {},
sign: '',
clickHandler: Function
},
active: false,
isDroped: false,
elId: ''
}
},
watch: {
isDrop: {
handler(newVal) {
this.isDroped = newVal;
},
immediate: true
},
value: {
handler(newVal) {
this.$nextTick(()=>{
this.active = this.parentData.defaultValue.indexOf(newVal) == -1;
})
},
immediate: true
}
},
computed: {
getDownIcon() {
const style = {
size: '30rpx',
color: '#333',
...this.parentData.extraIcon
}
if (this.active || this.isDroped) {
style.color = this.parentData.extraActiveIcon?.color ? this.parentData.extraActiveIcon?.color : '#3c9cff';
style.size = this.parentData.extraActiveIcon?.size ? this.parentData.extraActiveIcon?.size : '30rpx';
}
if (this.isDroped) {
style.name = this.parentData.extraActiveIcon?.name;
}
return style;
},
getTextStyle() {
const style = {
size: this.parentData.textSize,
color: this.parentData.textColor
};
if (this.active || this.isDroped) {
style.size = this.parentData.textActiveSize;
style.color = this.parentData.textActiveColor;
}
return style;
}
},
created() {
this.init();
},
methods: {
init() {
this.elId = this.$uv.guid();
this.getParentData('uv-drop-down');
if (!this.parent) {
this.$uv.error('uv-drop-down必须搭配uv-drop-down-item组件使用');
}
uni.$on('HANDLE_DROPDOWN_ONE', id => {
if (this.isDroped && this.elId != id) {
this.isDroped = false;
}
})
uni.$on(`${this.parentData.sign}_CLOSEPOPUP`, async () => {
if (this.isDroped) {
this.isDroped = false;
}
})
},
async clickHandler() {
let data = {};
uni.$emit('HANDLE_DROPDOWN_ONE', this.elId);
switch (+this.type) {
case 1:
this.active = !this.active;
data = {
name: this.name,
active: this.active,
type: this.type
};
break;
case 2:
this.isDroped = !this.isDroped;
data = {
name: this.name,
active: this.isDroped,
type: this.type
};
break;
}
this.parentData.clickHandler(data);
this.$emit('click', data);
uni.$emit(`${this.parentData.sign}_CLICKMENU`, {
show: +this.type > 1 && this.isDroped
});
}
}
}
</script>
<style scoped lang="scss">
@import '@/uni_modules/uv-ui-tools/libs/css/components.scss';
.uv-drop-down-item {
@include flex;
align-items: center;
padding: 20rpx;
}
</style>