WebApp【公共组件库】@前端(For Git Submodule)
‘chensiAb’
2025-03-25 3b03f87a02458f719e2eb4bf112a13441b427d14
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
/**
 * CSwitchRadio 开关式单选框
 * @author Tevin
 */
 
<template>
    <view
        class="c-switch c-switch-radio"
        :class="className"
    >
        <AtSwitch
            :title="itemRes.label"
            ref="switch"
        />
        <view
            class="c-switch-radio-group"
            ref="radio"
        >
            <view
                class="c-switch-radio-item"
                @tap="evt => handleChange(true)"
            >
                <view
                    class="c-switch-radio-icon"
                    :class="current === true ? 'checked' : ''"
                >
                    <AtIcon value="check" />
                </view>
                <view class="c-switch-radio-label">{{checkedLabel}}</view>
            </view>
            <view
                class="c-switch-radio-item"
                @tap="evt => handleChange(false)"
            >
                <view
                    class="c-switch-radio-icon"
                    :class="current === false ? 'checked' : ''"
                >
                    <AtIcon value="check" />
                </view>
                <view class="c-switch-radio-label">{{uncheckedLabel}}</view>
            </view>
        </view>
    </view>
</template>
 
<script>
import { AtSwitch, AtIcon } from 'taro-ui-vue';
import { $ } from '@tarojs/extend';
import './cSwitch.scss';
 
export default {
    name: 'CSwitchRadio',
    components: {
        AtSwitch,
        AtIcon,
    },
    props: {
        // 表单数据资源(表单组件内部机制专用)
        itemRes: Object,
        // 选项对齐方式,left / right(默认)
        checkAlign: {
            type: String,
            default: 'right',
        },
        // 选中文字
        checkedLabel: {
            type: String,
            default: '是',
        },
        // 不选中文字
        uncheckedLabel: {
            type: String,
            default: '否',
        },
    },
    data() {
        return {};
    },
    computed: {
        className() {
            let className = '';
            if (this.checkAlign === 'left') {
                className += 'c-switch-radio-left ';
            }
            if (this.itemRes.required) {
                className += 'c-switch-required ';
            }
            if (this.itemRes.error) {
                className += 'c-switch-error ';
            }
            return className;
        },
        current() {
            return this.itemRes.formData[this.itemRes.name];
        },
    },
    methods: {
        handleChange(value) {
            this.itemRes.onChange(value);
        },
    },
    mounted() {
        if (process.env.TARO_ENV === 'h5') {
            $(this.$refs.switch.$el).append(this.$refs.radio.$el);
        } else if (process.env.TARO_ENV === 'weapp') {
            $(this.$refs.switch.$el).find('.at-switch').append(this.$refs.radio);
        }
    },
};
</script>