index.vue
3.4 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
<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="select(opt)"
>
<text class="label">{{ displayOf(opt) }}</text>
</view>
</view>
</view>
</uni-popup>
</template>
<script>
export default {
name: 'SingleSelectSheet',
props: {
visible: { type: Boolean, default: false },
title: { type: String, default: '请选择' },
// 统一约定:options 为 [{ label: string, value: string|number }]
options: { type: Array, default: () => [] },
value: { type: [String, Number], default: '' }
},
data() {
return {
innerValue: this.value,
innerOption: null
}
},
watch: {
value(v) { this.innerValue = v },
visible(v) { v ? this.open() : this.close() }
},
mounted() {
if (this.visible) this.open()
},
methods: {
open() {
// 打开弹框时回显当前已选中的值
this.innerValue = this.value
this.innerOption = (this.options || []).find(o => this.valueOf(o) === this.value) || null
this.$refs.popup && this.$refs.popup.open()
this.$emit('update:visible', true)
},
close() {
// 关闭时同步回显值,避免取消后残留未确认值
this.innerValue = this.value
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 ? opt.value : ''
},
isSelected(opt) {
return this.valueOf(opt) === this.innerValue
},
select(opt) {
this.innerValue = this.valueOf(opt)
this.innerOption = opt
},
onCancel() {
this.close()
this.$emit('cancel')
},
onConfirm() {
const val = this.innerValue
const opt = this.innerOption || (this.options || []).find(o => this.valueOf(o) === val) || null
const label = opt ? this.displayOf(opt) : ''
this.$emit('confirm', { value: val, label })
this.$emit('input', val)
this.$emit('update:value', val)
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 {
height: 80rpx;
line-height: 80rpx;
padding: 0 20rpx;
background: #fff;
text-align: center;
border-radius: 12rpx;
font-size: 32rpx;
.label {
color: rgba(0,0,0,0.6);
font-size: 32rpx;
}
&.selected {
background: #f3f3f3;
.label {
color: rgba(0,0,0,0.9);
}
}
}
</style>