index.vue
3.8 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
<template>
<uni-popup ref="popup" type="bottom" :mask-click="false" :safe-area="true">
<view class="sheet">
<view class="sheet-header">
<text class="cancel" @click="onCancel">取消</text>
<text class="title">{{ title }}</text>
<text class="ok" @click="onConfirm">确认</text>
</view>
<view class="sheet-body">
<view
v-for="(opt, i) in options"
:key="i"
:class="['option', { selected: isSelected(opt) }]"
@click="toggle(opt)"
>
<text class="label">{{ displayOf(opt) }}</text>
</view>
</view>
</view>
</uni-popup>
</template>
<script>
export default {
name: 'MultiSelectSheet',
props: {
visible: { type: Boolean, default: false },
title: { type: String, default: '请选择' },
// 统一约定:options 为 [{ label: string, value: string|number }]
options: { type: Array, default: () => [] },
// 接收逗号分隔的字符串 '111,222,333'
value: { type: String, default: '' }
},
data() {
return {
innerValue: [] // 内部维护数组形式
}
},
watch: {
value(v) { this.initInnerValue(v) },
visible(v) { v ? this.open() : this.close() }
},
mounted() {
if (this.visible) this.open()
},
methods: {
initInnerValue(val) {
if (!val) {
this.innerValue = []
} else {
this.innerValue = String(val).split(',').filter(x => x)
}
},
open() {
this.initInnerValue(this.value)
this.$refs.popup && this.$refs.popup.open()
this.$emit('update:visible', true)
},
close() {
this.$refs.popup && this.$refs.popup.close()
this.$emit('update:visible', false)
},
displayOf(opt) {
if (!opt) return ''
return opt.label != null ? String(opt.label) : ''
},
valueOf(opt) {
if (!opt) return ''
return opt.value != null ? String(opt.value) : ''
},
isSelected(opt) {
const v = this.valueOf(opt)
return this.innerValue.includes(v)
},
toggle(opt) {
const v = this.valueOf(opt)
const idx = this.innerValue.indexOf(v)
if (idx > -1) {
this.innerValue.splice(idx, 1)
} else {
this.innerValue.push(v)
}
},
onCancel() {
this.close()
this.$emit('cancel')
},
onConfirm() {
const selectedValues = this.innerValue
// 保持原始选项顺序
const sortedValues = []
const sortedLabels = []
this.options.forEach(opt => {
const v = this.valueOf(opt)
if (selectedValues.includes(v)) {
sortedValues.push(v)
sortedLabels.push(this.displayOf(opt))
}
})
const valStr = sortedValues.join(',')
const labelStr = sortedLabels.join(',')
this.$emit('confirm', { value: valStr, label: labelStr })
this.$emit('input', valStr)
this.$emit('update:value', valStr)
this.close()
}
}
}
</script>
<style lang="scss" scoped>
.sheet {
width: 100%;
max-height: 45vh;
background: #fff;
border-radius: 20rpx 20rpx 0 0;
display: flex;
flex-direction: column;
}
.sheet-header {
display: flex;
align-items: center;
justify-content: space-between;
padding: 30rpx 32rpx;
border-bottom: 1rpx solid #f0f0f0;
}
.title {
font-size: 36rpx;
font-weight: 600;
}
.cancel {
color: rgba(0,0,0,0.6);
font-size: 28rpx;
}
.ok {
color: $theme-primary;
font-size: 28rpx;
}
.sheet-body {
flex: 1 1 auto;
overflow-y: auto;
padding: 32rpx;
}
.option {
padding: 20rpx;
line-height: 40rpx;
background: #fff;
text-align: center;
border-radius: 12rpx;
font-size: 32rpx;
margin-bottom: 20rpx;
.label {
color: rgba(0,0,0,0.6);
font-size: 32rpx;
}
&.selected {
background: #f3f3f3;
.label {
color: rgba(0,0,0,0.9);
}
}
}
</style>