blob: 6a149b33f10a82506ef566e183e039c5501f9720 [file] [log] [blame]
guangchao.xu070005a2020-12-07 09:56:40 +08001<template>
2 <view class="u-checkbox-group u-clearfix">
3 <slot></slot>
4 </view>
5</template>
6
7<script>
8 import Emitter from '../../libs/util/emitter.js';
9 /**
10 * checkboxGroup 开关选择器父组件Group
11 * @description 复选框组件一般用于需要多个选择的场景,该组件功能完整,使用方便
12 * @tutorial https://www.uviewui.com/components/checkbox.html
13 * @property {String Number} max 最多能选中多少个checkbox(默认999)
14 * @property {String Number} size 组件整体的大小,单位rpx(默认40)
15 * @property {Boolean} disabled 是否禁用所有checkbox(默认false)
16 * @property {String Number} icon-size 图标大小,单位rpx(默认20)
17 * @property {Boolean} label-disabled 是否禁止点击文本操作checkbox(默认false)
18 * @property {String} width 宽度,需带单位
19 * @property {String} width 宽度,需带单位
20 * @property {String} shape 外观形状,shape-方形,circle-圆形(默认circle)
21 * @property {Boolean} wrap 是否每个checkbox都换行(默认false)
22 * @property {String} active-color 选中时的颜色,应用到所有子Checkbox组件(默认#2979ff)
23 * @event {Function} change 任一个checkbox状态发生变化时触发,回调为一个对象
24 * @example <u-checkbox-group></u-checkbox-group>
25 */
26 export default {
27 name: 'u-checkbox-group',
28 mixins: [Emitter],
29 props: {
30 // 最多能选中多少个checkbox
31 max: {
32 type: [Number, String],
33 default: 999
34 },
35 // 所有选中项的 name
36 // value: {
37 // default: Array,
38 // default() {
39 // return []
40 // }
41 // },
42 // 是否禁用所有复选框
43 disabled: {
44 type: Boolean,
45 default: false
46 },
47 // 在表单内提交时的标识符
48 name: {
49 type: [Boolean, String],
50 default: ''
51 },
52 // 是否禁止点击提示语选中复选框
53 labelDisabled: {
54 type: Boolean,
55 default: false
56 },
57 // 形状,square为方形,circle为原型
58 shape: {
59 type: String,
60 default: 'square'
61 },
62 // 选中状态下的颜色
63 activeColor: {
64 type: String,
65 default: '#2979ff'
66 },
67 // 组件的整体大小
68 size: {
69 type: [String, Number],
70 default: 34
71 },
72 // 每个checkbox占u-checkbox-group的宽度
73 width: {
74 type: String,
75 default: 'auto'
76 },
77 // 是否每个checkbox都换行
78 wrap: {
79 type: Boolean,
80 default: false
81 },
82 // 图标的大小,单位rpx
83 iconSize: {
84 type: [String, Number],
85 default: 20
86 },
87 },
88 data() {
89 return {
90 }
91 },
92 created() {
93 // 如果将children定义在data中,在微信小程序会造成循环引用而报错
94 this.children = [];
95 },
96 methods: {
97 emitEvent() {
98 let values = [];
99 this.children.map(val => {
100 if(val.value) values.push(val.name);
101 })
102 this.$emit('change', values);
103 // 发出事件,用于在表单组件中嵌入checkbox的情况,进行验证
104 // 由于头条小程序执行迟钝,故需要用几十毫秒的延时
105 setTimeout(() => {
106 // 将当前的值发送到 u-form-item 进行校验
107 this.dispatch('u-form-item', 'on-form-change', values);
108 }, 60)
109 }
110 }
111 }
112</script>
113
114<style lang="scss" scoped>
115 @import "../../libs/css/style.components.scss";
116
117 .u-checkbox-group {
118 /* #ifndef MP || APP-NVUE */
119 display: inline-flex;
120 flex-wrap: wrap;
121 /* #endif */
122 }
123</style>