schangxiang@126.com
2024-09-06 05f2a20bb792169bf7b8a101af8718b96449f55a
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
<template>
    <u-popup mode="center" :show="visible" @close="close" :round="10" :safeAreaInsetTop="true">
        <view class="picker-box" :style="{maxHeight:maxHeight+'px'}">
            <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>
    </u-popup>
</template>
 
<script>
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:500
        }
    },
    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)
        }
    }
}
</script>
 
<style scoped lang="scss">
.picker-box{
    width: 90vw;
    background-color: #fff;
    border-radius: 20rpx;
    padding: 30rpx 0;
    overflow: auto;
    .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;
        padding: 20rpx;
        &:last-child{
            margin-bottom: 0;
        }
    }
}
</style>