wd-checkbox-group.vue 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <template>
  2. <view :class="`wd-checkbox-group ${shape === 'button' && cell ? 'is-button' : ''} ${customClass}`" :style="customStyle">
  3. <slot />
  4. </view>
  5. </template>
  6. <script lang="ts">
  7. export default {
  8. name: 'wd-checkbox-group',
  9. options: {
  10. addGlobalClass: true,
  11. virtualHost: true,
  12. styleIsolation: 'shared'
  13. }
  14. }
  15. </script>
  16. <script lang="ts" setup>
  17. import { watch } from 'vue'
  18. import { checkNumRange, deepClone } from '../common/util'
  19. import { useChildren } from '../composables/useChildren'
  20. import { CHECKBOX_GROUP_KEY, checkboxGroupProps } from './types'
  21. const props = defineProps(checkboxGroupProps)
  22. const emit = defineEmits(['change', 'update:modelValue'])
  23. const { linkChildren } = useChildren(CHECKBOX_GROUP_KEY)
  24. linkChildren({ props, changeSelectState })
  25. watch(
  26. () => props.modelValue,
  27. (newValue) => {
  28. // 传入的value数组中包括重复的元素,这种情况非法。
  29. if (new Set(newValue).size !== newValue.length) {
  30. // eslint-disable-next-line quotes
  31. console.error("checkboxGroup's bound value includes same value")
  32. }
  33. if (newValue.length < props.min) {
  34. // eslint-disable-next-line quotes
  35. console.error("checkboxGroup's bound value's length can't be less than min")
  36. }
  37. if (props.max !== 0 && newValue.length > props.max) {
  38. // eslint-disable-next-line quotes
  39. console.error("checkboxGroup's bound value's length can't be large than max")
  40. }
  41. // 每次value变化都会触发重新匹配选中项
  42. },
  43. { deep: true, immediate: true }
  44. )
  45. watch(
  46. () => props.shape,
  47. (newValue) => {
  48. const type = ['circle', 'square', 'button']
  49. if (type.indexOf(newValue) === -1) console.error(`shape must be one of ${type.toString()}`)
  50. },
  51. { deep: true, immediate: true }
  52. )
  53. watch(
  54. () => props.min,
  55. (newValue) => {
  56. checkNumRange(newValue, 'min')
  57. },
  58. { deep: true, immediate: true }
  59. )
  60. watch(
  61. () => props.max,
  62. (newValue) => {
  63. checkNumRange(newValue, 'max')
  64. },
  65. { deep: true, immediate: true }
  66. )
  67. /**
  68. * @description 子节点通知父节点修改子节点选中状态
  69. * @param {any} value 子组件的标识符
  70. */
  71. function changeSelectState(value: string | number | boolean) {
  72. const temp: (string | number | boolean)[] = deepClone(props.modelValue)
  73. const index = temp.indexOf(value)
  74. if (index > -1) {
  75. // 已经选中,则从 value 列表中删除子节点的标识符。
  76. temp.splice(index, 1)
  77. } else {
  78. // 之前未选中,则现在把加子节点的标识符加到 value 列表中。
  79. temp.push(value)
  80. }
  81. emit('update:modelValue', temp)
  82. emit('change', {
  83. value: temp
  84. })
  85. }
  86. </script>
  87. <style lang="scss" scoped>
  88. @import './index.scss';
  89. </style>