u-checkbox-group.vue 3.3 KB

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