blob: 1d9a439e6e7d0ef2bea98cc0386cc0a1da9dd018 [file] [log] [blame]
guangchao.xu070005a2020-12-07 09:56:40 +08001<template>
2 <view class="u-radio" :style="[radioStyle]">
3 <view class="u-radio__icon-wrap" @tap="toggle" :class="[iconClass]" :style="[iconStyle]">
4 <u-icon
5 class="u-radio__icon-wrap__icon"
6 name="checkbox-mark"
7 :size="elIconSize"
8 :color="iconColor"/>
9 </view>
10 <view class="u-radio__label" @tap="onClickLabel" :style="{
11 fontSize: $u.addUnit(labelSize)
12 }">
13 <slot />
14 </view>
15 </view>
16</template>
17
18<script>
19 /**
20 * radio 单选框
21 * @description 单选框用于有一个选择,用户只能选择其中一个的场景。搭配u-radio-group使用
22 * @tutorial https://www.uviewui.com/components/radio.html
23 * @property {String Number} icon-size 图标大小,单位rpx(默认24)
24 * @property {String Number} label-size label字体大小,单位rpx(默认28)
25 * @property {String Number} name radio组件的标示符
26 * @property {String} shape 形状,见上方说明(默认circle)
27 * @property {Boolean} disabled 是否禁用(默认false)
28 * @property {Boolean} label-disabled 点击文本是否可以操作radio(默认true)
29 * @property {String} active-color 选中时的颜色,如设置parent的active-color将失效
30 * @event {Function} change 某个radio状态发生变化时触发(选中状态)
31 * @example <u-radio :label-disabled="false">门掩黄昏,无计留春住</u-radio>
32 */
33 export default {
34 name: "u-radio",
35 props: {
36 // radio的名称
37 name: {
38 type: [String, Number],
39 default: ''
40 },
41 // 形状,square为方形,circle为原型
42 shape: {
43 type: String,
44 default: ''
45 },
46 // 是否禁用
47 disabled: {
48 type: [String, Boolean],
49 default: ''
50 },
51 // 是否禁止点击提示语选中复选框
52 labelDisabled: {
53 type: [String, Boolean],
54 default: ''
55 },
56 // 选中状态下的颜色,如设置此值,将会覆盖parent的activeColor值
57 activeColor: {
58 type: String,
59 default: ''
60 },
61 // 图标的大小,单位rpx
62 iconSize: {
63 type: [String, Number],
64 default: ''
65 },
66 // label的字体大小,rpx单位
67 labelSize: {
68 type: [String, Number],
69 default: ''
70 },
71 },
72 data() {
73 return {
74 // 父组件的默认值,因为头条小程序不支持在computed中使用this.parent.shape的形式
75 // 故只能使用如此方法
76 parentData: {
77 iconSize: null,
78 labelDisabled: null,
79 disabled: null,
80 shape: null,
81 activeColor: null,
82 size: null,
83 width: null,
84 height: null,
85 value: null,
86 wrap: null
87 }
88 };
89 },
90 created() {
91 this.parent = false;
92 // 支付宝小程序不支持provide/inject,所以使用这个方法获取整个父组件,在created定义,避免循环引用
93 this.updateParentData();
94 this.parent.children.push(this);
95 },
96 computed: {
97 // 是否禁用,如果父组件u-raios-group禁用的话,将会忽略子组件的配置
98 elDisabled() {
99 return this.disabled !== '' ? this.disabled : this.parentData.disabled !== null ? this.parentData.disabled : false;
100 },
101 // 是否禁用label点击
102 elLabelDisabled() {
103 return this.labelDisabled !== '' ? this.labelDisabled : this.parentData.labelDisabled !== null ? this.parentData.labelDisabled : false;
104 },
105 // 组件尺寸,对应size的值,默认值为34rpx
106 elSize() {
107 return this.size ? this.size : (this.parentData.size ? this.parentData.size : 34);
108 },
109 // 组件的勾选图标的尺寸,默认20
110 elIconSize() {
111 return this.iconSize ? this.iconSize : (this.parentData.iconSize ? this.parentData.iconSize : 20);
112 },
113 // 组件选中激活时的颜色
114 elActiveColor() {
115 return this.activeColor ? this.activeColor : (this.parentData.activeColor ? this.parentData.activeColor : 'primary');
116 },
117 // 组件的形状
118 elShape() {
119 return this.shape ? this.shape : (this.parentData.shape ? this.parentData.shape : 'circle');
120 },
121 // 设置radio的状态,要求radio的name等于parent的value时才为选中状态
122 iconStyle() {
123 let style = {};
124 if (this.elActiveColor && this.parentData.value == this.name && !this.elDisabled) {
125 style.borderColor = this.elActiveColor;
126 style.backgroundColor = this.elActiveColor;
127 }
128 style.width = this.$u.addUnit(this.elSize);
129 style.height = this.$u.addUnit(this.elSize);
130 return style;
131 },
132 iconColor() {
133 return this.name == this.parentData.value ? '#ffffff' : 'transparent';
134 },
135 iconClass() {
136 let classes = [];
137 classes.push('u-radio__icon-wrap--' + this.elShape);
138 if (this.name == this.parentData.value) classes.push('u-radio__icon-wrap--checked');
139 if (this.elDisabled) classes.push('u-radio__icon-wrap--disabled');
140 if (this.name == this.parentData.value && this.elDisabled) classes.push(
141 'u-radio__icon-wrap--disabled--checked');
142 // 支付宝小程序无法动态绑定一个数组类名,否则解析出来的结果会带有",",而导致失效
143 return classes.join(' ');
144 },
145 radioStyle() {
146 let style = {};
147 if (this.parentData.width) {
148 style.width = this.$u.addUnit(this.parentData.width);
149 // #ifdef MP
150 // 各家小程序因为它们特殊的编译结构,使用float布局
151 style.float = 'left';
152 // #endif
153 // #ifndef MP
154 // H5和APP使用flex布局
155 style.flex = `0 0 ${this.$u.addUnit(this.parentData.width)}`;
156 // #endif
157 }
158 if (this.parentData.wrap) {
159 style.width = '100%';
160 // #ifndef MP
161 // H5和APP使用flex布局,将宽度设置100%,即可自动换行
162 style.flex = '0 0 100%';
163 // #endif
164 }
165 return style;
166 }
167 },
168 methods: {
169 updateParentData() {
170 this.getParentData('u-radio-group');
171 },
172 onClickLabel() {
173 if (!this.elLabelDisabled && !this.elDisabled) {
174 this.setRadioCheckedStatus();
175 }
176 },
177 toggle() {
178 if (!this.elDisabled) {
179 this.setRadioCheckedStatus();
180 }
181 },
182 emitEvent() {
183 // u-radio的name不等于父组件的v-model的值时(意味着未选中),才发出事件,避免多次点击触发事件
184 if(this.parentData.value != this.name) this.$emit('change', this.name);
185 },
186 // 改变组件选中状态
187 // 这里的改变的依据是,更改本组件的parentData.value值为本组件的name值,同时通过父组件遍历所有u-radio实例
188 // 将本组件外的其他u-radio的parentData.value都设置为空(由computed计算后,都被取消选中状态),因而只剩下一个为选中状态
189 setRadioCheckedStatus() {
190 this.emitEvent();
191 if(this.parent) {
192 this.parent.setValue(this.name);
193 this.parentData.value = this.name;
194 }
195 }
196 }
197 };
198</script>
199
200<style lang="scss" scoped>
201 @import "../../libs/css/style.components.scss";
202
203 .u-radio {
204 /* #ifndef APP-NVUE */
205 display: inline-flex;
206 /* #endif */
207 align-items: center;
208 overflow: hidden;
209 user-select: none;
210 line-height: 1.8;
211
212 &__icon-wrap {
213 color: $u-content-color;
214 @include vue-flex;
215 flex: none;
216 align-items: center;
217 justify-content: center;
218 box-sizing: border-box;
219 width: 42rpx;
220 height: 42rpx;
221 color: transparent;
222 text-align: center;
223 transition-property: color, border-color, background-color;
224 font-size: 20px;
225 border: 1px solid #c8c9cc;
226 transition-duration: 0.2s;
227
228 /* #ifdef MP-TOUTIAO */
229 // 头条小程序兼容性问题,需要设置行高为0,否则图标偏下
230 &__icon {
231 line-height: 0;
232 }
233 /* #endif */
234
235 &--circle {
236 border-radius: 100%;
237 }
238
239 &--square {
240 border-radius: 3px;
241 }
242
243 &--checked {
244 color: #fff;
245 background-color: #2979ff;
246 border-color: #2979ff;
247 }
248
249 &--disabled {
250 background-color: #ebedf0;
251 border-color: #c8c9cc;
252 }
253
254 &--disabled--checked {
255 color: #c8c9cc !important;
256 }
257 }
258
259 &__label {
260 word-wrap: break-word;
261 margin-left: 10rpx;
262 margin-right: 24rpx;
263 color: $u-content-color;
264 font-size: 30rpx;
265
266 &--disabled {
267 color: #c8c9cc;
268 }
269 }
270 }
271</style>