evan-checkbox-group.vue 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <template>
  2. <view class="evan-checkbox-group">
  3. <slot></slot>
  4. </view>
  5. </template>
  6. <script>
  7. export default {
  8. name: 'EvanCheckboxGroup',
  9. props: {
  10. value: {
  11. type: Array,
  12. default: null
  13. },
  14. disabled: {
  15. type: Boolean,
  16. default: false
  17. },
  18. max: {
  19. type: Number,
  20. default: null
  21. }
  22. },
  23. watch: {
  24. value: {
  25. deep: true,
  26. handler(value) {
  27. this.deepSetValue(this.$children)
  28. }
  29. }
  30. },
  31. methods: {
  32. onCheckboxChange(label) {
  33. const value = this.value || []
  34. const index = value.findIndex((v) => v === label)
  35. if (index !== -1) {
  36. value.splice(index, 1)
  37. } else {
  38. value.push(label)
  39. }
  40. this.$emit('input', value)
  41. this.$emit('change', value)
  42. },
  43. deepSetValue(array) {
  44. if (Array.isArray(array)) {
  45. array.forEach((child) => {
  46. let childName = child.$options.name
  47. if (childName === 'EvanCheckbox') {
  48. if (typeof child.setValue === 'function') {
  49. child.setValue(this.value)
  50. }
  51. } else if (child.$children) {
  52. this.deepSetValue(child.$children)
  53. }
  54. })
  55. }
  56. }
  57. }
  58. }
  59. </script>
  60. <style lang="scss" scoped>
  61. </style>