WebApp【公共组件库】@前端(For Git Submodule)
Tevin
2021-04-01 d7cf84bd603bcab89d44e9942348f017665cb756
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
/**
 * CFilterSelect - 筛选项目,单项选择
 * @author Tevin
 */
 
<template>
    <view
        class="c-filter-select"
        :class="'type-'+type"
    >
        <view
            class="label"
            v-if="type==='item'"
        >{{label}}</view>
        <picker
            mode="selector"
            range-key="name"
            :range="options2"
            @change="evt=>handleChange(evt.detail.value)"
        >
            <view class="content">
                <view
                    class="label"
                    v-if="type==='bar'"
                >
                    {{label}}:
                </view>
                <view :class="selected ? 'filled':'empty'">
                    {{selected ? options2[current].name : ('请选择' + label)}}
                </view>
                <view class="at-icon at-icon-chevron-down" />
            </view>
        </picker>
    </view>
</template>
 
<script>
import Taro from '@tarojs/taro';
 
export default {
    name: 'CFilterSelect',
    props: {
        type: String,
        label: String,
        options: Array,
        value: null,
        onChange: Function,
    },
    data() {
        return {};
    },
    computed: {
        options2() {
            return [{ name: '- 取消选择 -' }, ...this.options];
        },
        current() {
            for (let i = 0, item; (item = this.options2[i]); i++) {
                if (this.value === item.value) {
                    return i;
                }
            }
            return -1;
        },
        selected() {
            for (let item of this.options) {
                if (this.value === item.value) {
                    return true;
                }
            }
            return false;
        },
    },
    methods: {
        handleChange(index) {
            const selectIndex = Number(index);
            if (selectIndex > 0) {
                this.onChange(this.options2[selectIndex].value);
            } else {
                this.onChange();
            }
        },
    },
};
</script>