blob: 2172eb2053222f24e2cf132a234231d385004c2d [file] [log] [blame]
guangchao.xu070005a2020-12-07 09:56:40 +08001<template>
2 <view class="u-radio-group u-clearfix">
3 <slot></slot>
4 </view>
5</template>
6
7<script>
8 import Emitter from '../../libs/util/emitter.js';
9 /**
10 * radioRroup 单选框父组件
11 * @description 单选框用于有一个选择,用户只能选择其中一个的场景。搭配u-radio使用
12 * @tutorial https://www.uviewui.com/components/radio.html
13 * @property {Boolean} disabled 是否禁用所有radio(默认false)
14 * @property {String Number} size 组件整体的大小,单位rpx(默认40)
15 * @property {String} active-color 选中时的颜色,应用到所有子Radio组件(默认#2979ff)
16 * @property {String Number} icon-size 图标大小,单位rpx(默认20)
17 * @property {String} shape 外观形状,shape-方形,circle-圆形(默认circle)
18 * @property {Boolean} label-disabled 是否禁止点击文本操作checkbox(默认false)
19 * @property {String} width 宽度,需带单位
20 * @property {Boolean} wrap 是否每个radio都换行(默认false)
21 * @event {Function} change 任一个radio状态发生变化时触发
22 * @example <u-radio-group v-model="value"></u-radio-group>
23 */
24 export default {
25 name: "u-radio-group",
26 mixins: [Emitter],
27 props: {
28 // 是否禁用所有单选框
29 disabled: {
30 type: Boolean,
31 default: false
32 },
33 // 匹配某一个radio组件,如果某个radio的name值等于此值,那么这个radio就被会选中
34 value: {
35 type: [String, Number],
36 default: ''
37 },
38 // 选中状态下的颜色
39 activeColor: {
40 type: String,
41 default: '#2979ff'
42 },
43 // 组件的整体大小
44 size: {
45 type: [String, Number],
46 default: 34
47 },
48 // 是否禁止点击提示语选中复选框
49 labelDisabled: {
50 type: Boolean,
51 default: false
52 },
53 // 形状,square为方形,circle为原型
54 shape: {
55 type: String,
56 default: 'circle'
57 },
58 // 图标的大小,单位rpx
59 iconSize: {
60 type: [String, Number],
61 default: 20
62 },
63 // 每个checkbox占u-checkbox-group的宽度
64 width: {
65 type: [String, Number],
66 default: 'auto'
67 },
68 // 是否每个checkbox都换行
69 wrap: {
70 type: Boolean,
71 default: false
72 }
73 },
74 created() {
75 // 如果将children定义在data中,在微信小程序会造成循环引用而报错
76 this.children = [];
77 },
78 watch: {
79 // 当父组件需要子组件需要共享的参数发生了变化,手动通知子组件
80 parentData() {
81 if(this.children.length) {
82 this.children.map(child => {
83 // 判断子组件(u-radio)如果有updateParentData方法的话,就就执行(执行的结果是子组件重新从父组件拉取了最新的值)
84 typeof(child.updateParentData) == 'function' && child.updateParentData();
85 })
86 }
87 },
88 },
89 computed: {
90 // 这里computed的变量,都是子组件u-radio需要用到的,由于头条小程序的兼容性差异,子组件无法实时监听父组件参数的变化
91 // 所以需要手动通知子组件,这里返回一个parentData变量,供watch监听,在其中去通知每一个子组件重新从父组件(u-radio-group)
92 // 拉取父组件新的变化后的参数
93 parentData() {
94 return [this.value, this.disabled, this.activeColor, this.size, this.labelDisabled, this.shape, this.iconSize, this.width, this.wrap];
95 }
96 },
97 methods: {
98 // 该方法有子组件radio调用,当一个radio被选中的时候,给父组件设置value值(props传递的value)
99 setValue(val) {
100 // 通过子组件传递过来的val值(此被选中的子组件内部已将parentValue设置等于val的值),将其他
101 // u-radio设置未选中的状态
102 this.children.map(child => {
103 if(child.parentData.value != val) child.parentData.value = '';
104 })
105 // 通过emit事件,设置父组件通过v-model双向绑定的值
106 this.$emit('input', val);
107 this.$emit('change', val);
108 // 等待下一个周期再执行,因为this.$emit('input')作用于父组件,再反馈到子组件内部,需要时间
109 // 由于头条小程序执行迟钝,故需要用几十毫秒的延时
110 setTimeout(() => {
111 // 将当前的值发送到 u-form-item 进行校验
112 this.dispatch('u-form-item', 'on-form-change', val);
113 }, 60)
114 }
115 }
116 }
117</script>
118
119<style lang="scss" scoped>
120 @import "../../libs/css/style.components.scss";
121
122 .u-radio-group {
123 /* #ifndef MP || APP-NVUE */
124 display: inline-flex;
125 flex-wrap: wrap;
126 /* #endif */
127 }
128</style>