popup-dict.vue
3.22 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
<template>
<tui-bottom-popup :show="visible" @close="onClose">
<view class="popup-dict">
<view class="popup-dict__content">
<picker-view class="picker-view" indicator-class="popup-dict__indicator" :value="pickerValue" @change="onPickerChange">
<picker-view-column class="popup-dict__column">
<template v-for="(item, index) in options">
<view class="popup-dict__item" :key="index">
<slot v-if="$slots.default || $slots.$default" :item="item"></slot>
<text v-else>{{ item[labelKey] }}</text>
</view>
</template>
</picker-view-column>
</picker-view>
</view>
<view class="popup-dict__footer">
<zui-button block @click="onCancel">取消</zui-button>
<zui-button block type="primary" @click="onSubmit">确定</zui-button>
</view>
</view>
</tui-bottom-popup>
</template>
<script>
import { mapGetters } from 'vuex';
export default {
name: 'popup-dict',
props: {
value: Boolean,
dict: String,
list: Array,
labelKey: {
type: String,
default: 'valueName'
},
valueKey: {
type: String,
default: 'valueCode'
},
custom: Boolean
},
data: function() {
return {
pickerValue: [0],
visible: this.value,
};
},
computed: {
...mapGetters(['dictList', 'dictValue']),
options: function() {
if (this.list && this.list.length > 0) {
return this.list;
} else {
return this.dict ? this.dictList(this.dict) : [];
}
}
},
watch: {
value: function(val) {
if (this.options.length === 0) {
if (val) {
uni.showToast({
title: '没有可选值',
icon: 'none'
});
this.$emit('input', false);
}
} else if (this.options.length > 0 && this.options.length <= 6 && !this.custom) {
if (val) {
uni.showActionSheet({
cancelButtonText: '取消',
itemList: this.options.map(i => i[this.labelKey]),
success: (res) => {
this.$emit('confirm', this.options[res.tapIndex]);
},
complete: () => {
this.$emit('input', false);
}
});
}
} else {
this.visible = !!val;
}
}
},
methods: {
onPickerChange: function (e) {
this.pickerValue = e.detail.value;
},
onClose: function() {
this.$emit('visible', false);
},
onSubmit: function() {
const index = this.pickerValue[0];
this.$emit('confirm', this.options[index]);
},
onCancel: function() {
this.$emit('input', false);
}
}
};
</script>
<style lang="scss">
.popup-dict {
background: #fff;
padding-bottom: $padding-md;
&__content {
display: flex;
justify-content: space-between;
.picker-view {
width: 100%;
}
}
&__column {
height: 400upx;
}
&__item {
display: flex;
align-items: center;
justify-content: center;
}
&__footer {
padding: $padding-md;
display: flex;
flex: auto;
align-items: center;
justify-content: center;
.zui-button:first-child {
margin-right: $padding-md;
}
}
}
</style>