zhaojs
2023-07-18 0e1bce71603c5cbb92b3fe6387c87430528d18a9
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
<template>
    <view class="u-radio-group u-clearfix">
        <slot></slot>
    </view>
</template>
 
<script>
    import Emitter from '../../libs/util/emitter.js';
    /**
     * radioRroup 单选框父组件
     * @description 单选框用于有一个选择,用户只能选择其中一个的场景。搭配u-radio使用
     * @tutorial https://www.uviewui.com/components/radio.html
     * @property {Boolean} disabled 是否禁用所有radio(默认false)
     * @property {String Number} size 组件整体的大小,单位rpx(默认40)
     * @property {String} active-color 选中时的颜色,应用到所有子Radio组件(默认#2979ff)
     * @property {String Number} icon-size 图标大小,单位rpx(默认20)
     * @property {String} shape 外观形状,shape-方形,circle-圆形(默认circle)
     * @property {Boolean} label-disabled 是否禁止点击文本操作checkbox(默认false)
     * @property {String} width 宽度,需带单位
     * @property {Boolean} wrap 是否每个radio都换行(默认false)
     * @event {Function} change 任一个radio状态发生变化时触发
     * @example <u-radio-group v-model="value"></u-radio-group>
     */
    export default {
        name: "u-radio-group",
        mixins: [Emitter],
        props: {
            // 是否禁用所有单选框
            disabled: {
                type: Boolean,
                default: false
            },
            // 匹配某一个radio组件,如果某个radio的name值等于此值,那么这个radio就被会选中
            value: {
                type: [String, Number],
                default: ''
            },
            // 选中状态下的颜色
            activeColor: {
                type: String,
                default: '#2979ff'
            },
            // 组件的整体大小
            size: {
                type: [String, Number],
                default: 34
            },
            // 是否禁止点击提示语选中复选框
            labelDisabled: {
                type: Boolean,
                default: false
            },
            // 形状,square为方形,circle为原型
            shape: {
                type: String,
                default: 'square'
            },
            // 图标的大小,单位rpx
            iconSize: {
                type: [String, Number],
                default: 20
            },
            // 每个checkbox占u-checkbox-group的宽度
            width: {
                type: String,
                default: 'auto'
            },
            // 是否每个checkbox都换行
            wrap: {
                type: Boolean,
                default: false
            }
        },
        provide() {
            return {
                radioGroup: this
            }
        },
        methods: {
            // 该方法有子组件radio调用,当一个radio被选中的时候,给父组件设置value值(props传递的value)
            setValue(val) {
                // 通过emit事件,设置父组件通过v-model双向绑定的值
                this.$emit('input', val);
                // 等待下一个周期再执行,因为this.$emit('input')作用于父组件,再反馈到子组件内部,需要时间
                this.$nextTick(function() {
                    this.$emit('change', val);
                    // 发出事件,用于在表单组件中嵌入radio的情况,进行验证
                    // 将当前的值发送到 u-form-item 进行校验
                    this.dispatch('u-form-item', 'on-form-change', val);
                });
            }
        }
    }
</script>
 
<style lang="scss" scoped>
    @import "../../libs/css/style.components.scss";
    
    .u-radio-group {
        /* #ifndef MP */
        display: inline-flex;
        flex-wrap: wrap;
        /* #endif */
    }
</style>