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