WebApp【公共组件库】@前端(For Git Submodule)
Tevin
2021-03-29 1bd2c493c9cbe47d10e4ba045d5c4635fd4eccef
forms/select/CSelect.vue
@@ -6,27 +6,43 @@
<template>
    <view class="c-select">
        <picker
            v-if="!selectByPage"
            mode="selector"
            :range="range"
            :range="options"
            :value="current"
            range-key="name"
            @change="evt=>handleChange(evt.detail)"
        >
            <AtInput
                ref="input"
                :name="itemData.name"
                :title="itemData.label"
                :required="itemData.required"
                :error="itemData.error"
                :name="itemRes.name"
                :title="itemRes.label"
                :required="itemRes.required"
                :error="itemRes.error"
                :placeholder="placeholder"
                :value="selected"
            >
                <view class="at-icon at-icon-chevron-right" />
            </AtInput>
        </picker>
        <view v-else-if="selectByPage.length > 5">
            <AtInput
                :name="itemRes.name"
                :title="itemRes.label"
                :required="itemRes.required"
                :error="itemRes.error"
                :placeholder="placeholder"
                :value="chose.name"
                :onFocus="evt=>onGoToSelectorPage()"
            >
                <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 './cSelect.scss';
@@ -36,21 +52,28 @@
        AtInput,
    },
    props: {
        // 表单数据资源(表单组件内部机制专用)
        itemRes: Object,
        // 选择菜单选项(浮窗模式)
        options: Array,
        // 占位提示
        placeholder: String,
        itemData: Object,
        // 开启选择菜单跳转选择页面模式,并指定功能页面
        selectByPage: String,
        // 页面模式下,选择完成后的回调
        onSelectFromPage: Function,
    },
    data() {
        return {
            optionKey: typeof this.options[0].value === 'undefined' ? 'id' : 'value',
            chose: {},
        };
    },
    computed: {
        range() {
            return (this.options || []).map((item) => item.name || item[this.optionKey]);
        optionKey() {
            return typeof (this.options[0] || {}).value === 'undefined' ? 'id' : 'value';
        },
        current() {
            const curVal = this.itemData.formData[this.itemData.name];
            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;
@@ -59,7 +82,7 @@
            return -1;
        },
        selected() {
            const curVal = this.itemData.formData[this.itemData.name];
            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;
@@ -71,7 +94,29 @@
    methods: {
        handleChange(evt) {
            const item = this.options[evt.value];
            this.itemData.onChange(item[this.optionKey]);
            this.itemRes.onChange(item[this.optionKey]);
        },
        onGoToSelectorPage() {
            let url = this.selectByPage;
            if (this.selectByPage.indexOf('?') > 0) {
                url += '&mode=CSelect';
            } else {
                url += '?mode=CSelect';
            }
            Taro.navigateTo({
                url,
                events: {
                    'CSelect:onSelected': data => {
                        const value = data.id || data.value || data.key;
                        this.chose = {
                            name: data.name,
                            value,
                        };
                        this.itemRes.onChange(value);
                        this.onSelectFromPage(data);
                    },
                },
            });
        },
    },
    mounted() {},