u-checkbox-group.vue 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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} width 宽度,需带单位
  16. * @property {Boolean} wrap 是否每个checkbox都换行(默认false)
  17. * @property {String} active-color 选中时的颜色,应用到所有子Checkbox组件(默认#2979ff)
  18. * @event {Function} change 任一个checkbox状态发生变化时触发,回调为一个对象
  19. * @example <u-checkbox-group></u-checkbox-group>
  20. */
  21. export default {
  22. name: 'u-checkbox-group',
  23. mixins: [Emitter],
  24. props: {
  25. // 最多能选中多少个checkbox
  26. max: {
  27. type: [Number, String],
  28. default: 999
  29. },
  30. // 所有选中项的 name
  31. // value: {
  32. // default: Array,
  33. // default() {
  34. // return []
  35. // }
  36. // },
  37. // 是否禁用所有复选框
  38. disabled: {
  39. type: Boolean,
  40. default: false
  41. },
  42. // 在表单内提交时的标识符
  43. name: {
  44. type: [Boolean, String],
  45. default: ''
  46. },
  47. // 选中状态下的颜色
  48. activeColor: {
  49. type: String,
  50. default: '#2979ff'
  51. },
  52. // 组件的整体大小
  53. size: {
  54. type: [String, Number],
  55. default: 34
  56. },
  57. // 每个checkbox占u-checkbox-group的宽度
  58. width: {
  59. type: String,
  60. default: 'auto'
  61. },
  62. // 是否每个checkbox都换行
  63. wrap: {
  64. type: Boolean,
  65. default: false
  66. }
  67. },
  68. provide() {
  69. return {
  70. checkboxGroup: this
  71. }
  72. },
  73. data() {
  74. return {
  75. }
  76. },
  77. created() {
  78. // 如果将children定义在data中,在微信小程序会造成循环引用而报错
  79. this.children = [];
  80. },
  81. methods: {
  82. emitEvent() {
  83. let values = [];
  84. this.children.map(val => {
  85. if(val.value) values.push(val.name);
  86. })
  87. this.$emit('change', values);
  88. // 发出事件,用于在表单组件中嵌入checkbox的情况,进行验证
  89. this.$nextTick(() => {
  90. // 将当前的值发送到 u-form-item 进行校验
  91. this.dispatch('u-form-item', 'on-form-change', values);
  92. });
  93. }
  94. }
  95. }
  96. </script>
  97. <style lang="scss" scoped>
  98. .u-checkbox-group {
  99. /* #ifndef MP */
  100. display: inline-flex;
  101. flex-wrap: wrap;
  102. /* #endif */
  103. }
  104. </style>