WebApp【公共组件库】@前端(For Git Submodule)
Tevin
2022-09-20 357b0ca77c8b646cbbc9953549edb49af0f34686
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/**
 * CSelect
 * @author Tevin
 */
 
<template>
    <view
        class="c-select"
        :class="readOnly ? 'read-only':''"
    >
        <!-- 下拉选择模式 -->
        <view
            v-if="!selectByPage"
            class="c-select-slot-mode"
        >
            <picker
                class="c-select-input"
                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
                        v-show="!readOnly"
                        class="at-icon at-icon-chevron-right"
                    />
                </AtInput>
            </picker>
            <view class="c-select-slot">
                <slot />
            </view>
        </view>
        <!-- 跳转页面模式 -->
        <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
                    v-show="!readOnly"
                    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,
        // 只读模式
        readOnly: {
            type: Boolean,
            default: false,
        },
    },
    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) {
            if (typeof this.optionKey === 'undefined') {
                this.itemRes.onChange();
            } else {
                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>