WebApp【公共组件库】@前端(For Git Submodule)
Tevin
2021-07-09 d270af0a5ae30d20da42fccb0b33dd023bec3eac
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
/**
 * CSelect
 * @author Tevin
 */
 
<template>
    <view class="c-select">
        <picker
            v-if="!selectByPage"
            mode="selector"
            :range="options"
            :value="current"
            range-key="name"
            @change="evt=>handleChange(evt.detail)"
        >
            <AtInput
                ref="input"
                :name="itemRes.name"
                :title="itemRes.label"
                :required="itemRes.required"
                :disabled="itemRes.disabled"
                :error="itemRes.error"
                :placeholder="placeholder"
                :value="selected"
            >
                <view class="at-icon at-icon-chevron-right" />
            </AtInput>
        </picker>
        <view
            v-else-if="selectByPage.length > 5"
            @tap="evt => onGoToSelectorPage()"
        >
            <AtInput
                :name="itemRes.name"
                :title="itemRes.label"
                :required="itemRes.required"
                :disabled="itemRes.disabled"
                :error="itemRes.error"
                :placeholder="placeholder"
                :value="choose.name"
            >
                <view class="at-icon at-icon-chevron-right" />
            </AtInput>
        </view>
    </view>
</template>
 
<script>
import Taro from '@tarojs/taro';
import { AtInput } from 'taro-ui-vue';
import { $pagePoster } from '@components/common/PagePoster';
import './cSelect.scss';
 
export default {
    name: 'CSelect',
    components: {
        AtInput,
    },
    props: {
        // 表单数据资源(表单组件内部机制专用)
        itemRes: Object,
        // 选择菜单选项(浮窗模式)
        options: Array,
        // 占位提示
        placeholder: String,
        // 开启选择菜单跳转选择页面模式,并指定功能页面
        selectByPage: String,
        // 页面模式下,选择完成后的回调
        onSelectFromPage: Function,
    },
    data() {
        return {
            choose: {
                name: '',
                value: null,
            },
        };
    },
    computed: {
        optionKey() {
            return typeof (this.options[0] || {}).value === 'undefined' ? 'id' : 'value';
        },
        current() {
            const curVal = this.itemRes.formData[this.itemRes.name];
            for (let i = 0, item; (item = this.options[i]); i++) {
                if (curVal === item[this.optionKey]) {
                    return i;
                }
            }
            return -1;
        },
        selected() {
            const curVal = this.itemRes.formData[this.itemRes.name];
            for (let i = 0, item; (item = this.options[i]); i++) {
                if (curVal === item[this.optionKey]) {
                    return item.name;
                }
            }
            return '';
        },
    },
    methods: {
        handleChange(evt) {
            const item = this.options[evt.value];
            this.itemRes.onChange(item[this.optionKey]);
        },
        onGoToSelectorPage() {
            let url = this.selectByPage;
            if (this.selectByPage.indexOf('?') > 0) {
                url += '&mode=CSelect';
            } else {
                url += '?mode=CSelect';
            }
            if (this._eventor) {
                // 销毁上次事件收发器
                this._eventor.destroy();
            }
            this._eventor = $pagePoster.createEventor();
            this._eventor.on('@linked', () => {
                this._eventor.emit('lastValue', this.choose.value);
            });
            this._eventor.on('selected', data => {
                this.choose.name = data.name;
                this.choose.value = data.value;
                this.itemRes.onChange(data.value);
                this.onSelectFromPage && this.onSelectFromPage(data.value);
            });
            Taro.navigateTo({ url: url + '&eventorId=' + this._eventor.id });
        },
    },
};
</script>