WebApp【公共组件库】@前端(For Git Submodule)
YFeng
2022-03-11 e60616d3269a25eba78d435dade0719700fce30e
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
/**
 * CCheckBox
 * @author Tevin
 */
 
<template>
    <view class="c-check-box">
        <AtInput
            ref="input"
            :name="itemRes.name"
            :title="itemRes.label"
            :required="itemRes.required"
            :disabled="itemRes.disabled"
            :error="itemRes.error"
        />
        <AtCheckbox
            ref="check"
            :class="'c-check-box-' + boxType"
            :options="options"
            :selectedList="selectedList"
            :onChange="evt => handleChange(evt)"
        />
    </view>
</template>
 
<script>
import Taro from '@tarojs/taro';
import { $ } from '@tarojs/extend';
import { AtInput, AtCheckbox } from 'taro-ui-vue';
import { Tools } from '@components/common/Tools';
import './cCheckBox.scss';
 
export default {
    name: 'CCheckBox',
    components: {
        AtInput,
        AtCheckbox,
    },
    props: {
        // 表单数据资源(表单组件内部机制专用)
        itemRes: Object,
        // 选项列表,单项为 {label,value}
        options: {
            type: Array,
            default: () => [],
        },
        // 勾选类型
        boxType: {
            type: String,
            default: 'checkbox', // checkbox 多选、radio 单选
        },
    },
    data() {
        return {};
    },
    computed: {
        selectedList() {
            const value = this.itemRes.formData[this.itemRes.name];
            if (Tools.isArray(value)) {
                return value;
            } else {
                return [value];
            }
        },
    },
    methods: {
        handleChange(evt) {
            // 多选
            if (this.boxType === 'checkbox') {
                const next = [];
                evt.forEach(item => {
                    if (typeof item === 'undefined') {
                        return;
                    }
                    next.push(item);
                });
                this.itemRes.onChange(next);
            }
            // 单选
            else if (this.boxType === 'radio') {
                const next = evt[evt.length - 1];
                this.itemRes.onChange(next);
            }
        },
    },
    mounted() {
        if (process.env.TARO_ENV === 'h5') {
            $(this.$refs.input.$el)
                .find('.at-input__container')
                .prepend(this.$refs.check.$el);
        } else if (process.env.TARO_ENV === 'weapp') {
            $(this.$refs.input.$el)
                .find('.at-input__container')
                .append(this.$refs.check.$el);
        }
    },
};
</script>