You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

133 lines
4.3 KiB

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