u-checkbox-group.vue 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <template>
  2. <view class="u-checkbox-group" :class="bemClass">
  3. <slot></slot>
  4. </view>
  5. </template>
  6. <script>
  7. import props from './props.js'
  8. import mixin from '../../libs/mixin/mixin'
  9. import mpMixin from '../../libs/mixin/mpMixin'
  10. /**
  11. * checkboxGroup 复选框组
  12. * @description 复选框组件一般用于需要多个选择的场景,该组件功能完整,使用方便
  13. * @tutorial https://uview.d3u.cn/components/checkbox.html
  14. * @property {String} name 标识符
  15. * @property {Array} value 绑定的值
  16. * @property {String} shape 形状,circle-圆形,square-方形 (默认 'square' )
  17. * @property {Boolean} disabled 是否禁用全部checkbox (默认 false )
  18. * @property {String} activeColor 选中状态下的颜色,如设置此值,将会覆盖parent的activeColor值 (默认 '#2979ff' )
  19. * @property {String} inactiveColor 未选中的颜色 (默认 '#c8c9cc' )
  20. * @property {String | Number} size 整个组件的尺寸 单位px (默认 18 )
  21. * @property {String} placement 布局方式,row-横向,column-纵向 (默认 'row' )
  22. * @property {String | Number} labelSize label的字体大小,px单位 (默认 14 )
  23. * @property {String} labelColor label的字体颜色 (默认 '#303133' )
  24. * @property {String} activeLabelColor 选中状态下的label颜色
  25. * @property {Boolean} labelDisabled 是否禁止点击文本操作 (默认 false )
  26. * @property {String} iconColor 图标颜色 (默认 '#ffffff' )
  27. * @property {String | Number} iconSize 图标的大小,单位px (默认 12 )
  28. * @property {String} iconPlacement 勾选图标的对齐方式,left-左边,right-右边 (默认 'left' )
  29. * @property {Boolean} borderBottom placement为row时,是否显示下边框 (默认 false )
  30. * @event {Function} change 任一个checkbox状态发生变化时触发,回调为一个对象
  31. * @event {Function} input 修改通过v-model绑定的值时触发,回调为一个对象
  32. * @example <u-checkbox-group></u-checkbox-group>
  33. */
  34. export default {
  35. name: 'u-checkbox-group',
  36. mixins: [mpMixin, mixin, props],
  37. computed: {
  38. // 这里computed的变量,都是子组件u-checkbox需要用到的,由于头条小程序的兼容性差异,子组件无法实时监听父组件参数的变化
  39. // 所以需要手动通知子组件,这里返回一个parentData变量,供watch监听,在其中去通知每一个子组件重新从父组件(u-checkbox-group)
  40. // 拉取父组件新的变化后的参数
  41. parentData() {
  42. return [
  43. //#ifdef VUE2
  44. this.value,
  45. // #endif
  46. // #ifdef VUE3
  47. this.modelValue,
  48. // #endif
  49. this.disabled,
  50. this.inactiveColor,
  51. this.activeColor,
  52. this.size,
  53. this.labelDisabled,
  54. this.shape,
  55. this.iconSize,
  56. this.borderBottom,
  57. this.placement,
  58. this.activeLabelColor,
  59. this.plain
  60. ]
  61. },
  62. bemClass() {
  63. // this.bem为一个computed变量,在mixin中
  64. return this.bem('checkbox-group', ['placement'])
  65. },
  66. },
  67. watch: {
  68. // 当父组件需要子组件需要共享的参数发生了变化,手动通知子组件
  69. parentData() {
  70. if (this.children.length) {
  71. this.children.map(child => {
  72. // 判断子组件(u-checkbox)如果有init方法的话,就就执行(执行的结果是子组件重新从父组件拉取了最新的值)
  73. typeof (child.init) === 'function' && child.init()
  74. })
  75. }
  76. },
  77. },
  78. data() {
  79. return {
  80. }
  81. },
  82. created() {
  83. this.children = []
  84. },
  85. // #ifdef VUE3
  86. emits: ['update:modelValue', 'change'],
  87. // #endif
  88. methods: {
  89. // 将其他的checkbox设置为未选中的状态
  90. unCheckedOther(childInstance) {
  91. const values = []
  92. this.children.map(child => {
  93. // 将被选中的checkbox,放到数组中返回
  94. if (child.isChecked) {
  95. values.push(child.name)
  96. }
  97. })
  98. // 发出事件
  99. this.$emit('change', values)
  100. // 修改通过v-model绑定的值
  101. // #ifdef VUE2
  102. this.$emit('input', values)
  103. // #endif
  104. // #ifdef VUE3
  105. this.$emit('update:modelValue', values)
  106. // #endif
  107. },
  108. }
  109. }
  110. </script>
  111. <style lang="scss" scoped>
  112. @import "../../libs/css/components.scss";
  113. .u-checkbox-group {
  114. &--row {
  115. @include flex;
  116. flex-wrap: wrap;
  117. }
  118. &--column {
  119. @include flex(column);
  120. }
  121. }
  122. </style>