推广小程序前端代码
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.

119 lines
4.3 KiB

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