schangxiang@126.com
2025-05-18 7a361430f4088048d367e191195798f12c5955f3
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
<template>
    <u-popup mode="center" :show="visible" @close="close" :round="10" :safeAreaInsetTop="true" :safeAreaInsetBottom="true">
        <view class="picker-box" :style="{maxHeight:maxHeight+'px'}">
            <view class="picker-item-group">
                <view class="picker-item auto-wrap" v-for="(item,index) in list" :key="'picker-item-'+index" @tap.stop="onItemClick(index,item)">
                    {{(typeof(item)==='string'||typeof(item)==='number'||typeof(item)==='boolean'||item===null)?item:item[labelField]}}
                </view>
            </view>
        </view>
    </u-popup>
</template>
 
<script>
const MAX_HEIGHT = 500
export default {
    name:'easyPickerCompontent',
    emits:['update:visible','select'],
    props:{
        visible:{
            type:Boolean,
            default:false
        },
        list:{
            type:Array,
            default:function(){
                return []
            }
        },
        valueField:{
            type:String,
            default:'value'
        },
        labelField:{
            type:String,
            default:'label'
        }
    },
    data(){
        return {
            maxHeight:MAX_HEIGHT
        }
    },
    methods:{
        close(){
            this.$emit('update:visible',false)
        },
        onItemClick(index,selection){
            let valObj = selection;
            let val;
            if (typeof selection === 'string' || typeof selection === 'number'||typeof(selection)==='boolean'||selection===null) {
                val = selection;
            } else {
                try{
                    val = selection[this.valueField];
                }catch(e){
                    val = selection;
                }
            }
            this.close()
            this.$emit('select',val,valObj,index)
        },
        setMaxHeight(calHeight) {
            const spaceHeight = 10
            if (calHeight>spaceHeight) {
                calHeight = calHeight - spaceHeight
                this.maxHeight = calHeight
            }
        }
    },
    beforeMount(){
        let safety = this.$store.getters['system/getSafety'];
        if (safety) {
            this.setMaxHeight(safety.height)
        } else {
            uni.getSystemInfo({
                success:(res=>{
                    if (!res.safeAreaInsets) res.safeArea = {}
                    this.setMaxHeight(res.safeArea.height || 0)
                })
            })
        }
    }
}
</script>
 
<style scoped lang="scss">
.picker-box{
    width: 80vw;
    background-color:#f0f8ff;
    border-radius: 20rpx;
    padding-top: 30rpx;
    overflow: auto;
    display: flex;
    justify-content: center;
    box-sizing: border-box;
    .picker-item-group{
        width: 80%;
    }
    .picker-item{
        margin: 0 20rpx 30rpx 20rpx;
        text-align: center;
        background-image: linear-gradient(to right, $color-paimary-darken, $color-paimary-lighten);
        border: 1px solid #e4e4e4;
        border-radius: 10rpx;
        font-size: 1.2rem;
        padding: 20rpx;
    }
}
</style>