uniapp自定义picker

木头的喵喵拖孩

这事的起因是领导让我接手一个 uniapp 的项目的开发,
开发期间我用 picker 组件做了弹出选择框,但是领导说要修改一个按钮里的文字颜色,

很明显,使用 picker 是改不了这种细致入微的样式的

所以干脆自己做一个弹出选择框,利用 picker-view 的高灵活性,以及 uni-popup 的弹框功能来开发。

因为我们公司的设计师傅名字首字母拼音是zh,所以这个自定义组件就叫zh-picker

由于是为了替换 picker 组件,所以 zh-pickerpicker 用法都差不多。

上代码

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>
<view class="zh-picker">
<div class="slot" @click="openPicker">
<slot></slot>
</div>
<uni-popup ref="popup" type="bottom" @change="onPopupChange">
<view class="popup">
<view class="actions">
<view class="cancel" @click="onCancel">取消</view>
<view class="sure" @click="onSure">完成</view>
</view>
<picker-view
class="picker-view"
:value="indexs"
@change="onPickerViewChange"
>
<picker-view-column>
<view class="item" v-for="(item,index) in range" :key="index"
>{{getText(item)}}</view
>
</picker-view-column>
</picker-view>
</view>
</uni-popup>
</view>
</template>

<script>
export default {
name: "zh-picker",
props: {
range: {
type: Array,
default: () => [],
},
rangeText: {
type: String,
default: "text",
},
},
data() {
return {
visible: false,
indexs: [0],
};
},
methods: {
openPicker() {
this.$refs.popup.open();
},
onPopupChange({ show }) {
this.visible = show;
},
getText(item) {
if (item[this.rangeText]) return item[this.rangeText];
return JSON.stringify(item);
},
onPickerViewChange(ev) {
this.indexs = JSON.parse(JSON.stringify(ev.detail.value));
},
onSure() {
let _ev = {
detail: {
// picker-view是可以多列同时选择的,但此处只需要一列,所以只取ev.detail.value[0]
value: this.indexs[0],
},
};
this.$emit("change", _ev);
this.$refs.popup.close();
},
onCancel() {
this.$refs.popup.close();
},
},
};
</script>

<style scoped lang="scss">
.zh-picker {
.popup {
border-top-left-radius: 20rpx;
border-top-right-radius: 20rpx;
background-color: white;
overflow: hidden;

> .actions {
position: relative;
display: flex;
justify-content: space-between;
align-items: center;

&::after {
content: "";
position: absolute;
left: 0;
bottom: 0;
right: 0;
height: 1px;
clear: both;
border-bottom: 1px solid #e5e5e5;
color: #e5e5e5;
transform-origin: 0 100%;
transform: scaleY(0.5);
}

> view {
width: 120rpx;
height: 90rpx;
line-height: 90rpx;
text-align: center;
font-size: 34rpx;

&.cancel {
color: #888;
}

&.sure {
color: #30ac8f;
}
}
}

.picker-view {
width: 750rpx;
height: 30vh;

.item {
display: flex;
justify-content: center;
align-items: center;
}
}
}
}
</style>

最后,用这个组件 zh-picker ,你想改哪里的样式都能改了。

  • 标题: uniapp自定义picker
  • 作者: 木头的喵喵拖孩
  • 创建于: 2024-06-05 14:07:50
  • 更新于: 2024-06-05 14:21:28
  • 链接: https://blog.xx-xx.top/2024/06/05/uniapp自定义picker/
  • 版权声明: 本文章采用 CC BY-NC-SA 4.0 进行许可。
此页目录
uniapp自定义picker