WebApp【公共组件库】@前端(For Git Submodule)
‘chensiAb’
2025-03-25 3b03f87a02458f719e2eb4bf112a13441b427d14
forms/select/CSelect.vue
@@ -1,33 +1,74 @@
/**
 * CSelect
 * 下拉选择组件,用于在表单中提供选项选择功能
 * 支持两种选择模式:下拉选择模式和跳转页面选择模式
 * 可配置为只读模式,并支持自定义占位文本
 * @author Tevin
 */
<template>
    <view class="c-select">
        <picker
            mode="selector"
            :range="options"
            :value="current"
            range-key="name"
            @change="evt=>handleChange(evt.detail)"
    <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 || selectByPage === 'on'"
            @tap="evt => onGoSelectorPage()"
        >
            <AtInput
                ref="input"
                :name="itemData.name"
                :title="itemData.label"
                :required="itemData.required"
                :error="itemData.error"
                :name="itemRes.name"
                :title="itemRes.label"
                :required="itemRes.required"
                :disabled="itemRes.disabled"
                :error="itemRes.error"
                :placeholder="placeholder"
                :value="selected"
                :value="choose.name || selected"
            >
                <view class="at-icon at-icon-chevron-right" />
                <view
                    v-show="!readOnly"
                    class="at-icon at-icon-chevron-right"
                />
            </AtInput>
        </picker>
        </view>
    </view>
</template>
<script>
import Taro from '@tarojs/taro';
import { AtInput } from 'taro-ui-vue';
import './cSelect.scss';
@@ -37,19 +78,36 @@
        AtInput,
    },
    props: {
        // 表单数据资源(表单组件内部机制专用)
        itemRes: Object,
        // 选择菜单选项(浮窗模式)
        options: Array,
        // 占位提示
        placeholder: String,
        itemData: Object,
        // 只读模式
        readOnly: {
            type: Boolean,
            default: false,
        },
        // 开启选择菜单跳转选择页面模式,值为 'on'
        selectByPage: String,
        // 页面跳转on模式下,发起选择的回调
        onOpenSelectorPage: Function,
    },
    data() {
        return {};
        return {
            choose: {
                name: '',
                value: null,
            },
        };
    },
    computed: {
        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;
@@ -58,7 +116,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;
@@ -69,10 +127,27 @@
    },
    methods: {
        handleChange(evt) {
            const item = this.options[evt.value];
            this.itemData.onChange(item[this.optionKey]);
            if (typeof this.optionKey === 'undefined') {
                this.itemRes.onChange();
            } else {
                const item = this.options[evt.value];
                this.itemRes.onChange(item[this.optionKey]);
            }
        },
        onGoSelectorPage() {
            if (this.selectByPage === 'on') {
                this.onOpenSelectorPage && this.onOpenSelectorPage();
                return;
            }
            // 页面跳转URL模式已废弃
            console.warn('CSelect:警告,url跳转已废弃,无法再使用!');
        },
    },
    mounted() {},
    mounted() {
        if (this.selectByPage && this.selectByPage !== 'on') {
            // 页面跳转URL模式已废弃
            console.warn('CSelect:警告,url跳转已废弃,无法再使用!');
        }
    },
};
</script>