dengjunjie
5 天以前 4f39dcc195f28fa275fc2d065fbf1bf6a46c21b7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
<template>
  <view class="tn-collapse-class tn-collapse">
    <slot></slot>
  </view>
</template>
 
<script>
  export default {
    name: 'tn-collapse',
    props: {
      // 是否为手风琴
      accordion: {
        type: Boolean,
        default: true
      },
      // 头部样式
      headStyle: {
        type: Object,
        default() {
          return {}
        }
      },
      // 主题样式
      bodyStyle: {
        type: Object,
        default() {
          return {}
        }
      },
      // 每一个item的样式
      itemStyle: {
        type: Object,
        default() {
          return {}
        }
      },
      // 显示箭头
      arrow: {
        type: Boolean,
        default: true
      },
      // 箭头颜色
      arrowColor: {
        type: String,
        default: '#AAAAAA'
      },
      // 点击标题栏时的按压样式
      hoverClass: {
        type: String,
        default: 'tn-hover'
      }
    },
    computed: {
      parentData() {
        return [this.headStyle, this.bodyStyle, this.itemStyle, this.arrow, this.arrowColor, this.hoverClass]
      }
    },
    data() {
      return {
        
      }
    },
    watch: {
      parentData() {
        // 如果父组件的参数发生变化重新初始化子组件的信息
        if (this.childrens.length > 0) {
          this.init()
        }
      }
    },
    created() {
      this.childrens = []
    },
    methods: {
      // 重新初始化内部所有子元素计算高度,异步获取数据时重新渲染
      init() {
        this.childrens.forEach((child, index) => {
          child.init()
        })
      },
      // collapseItem被点击时由collapseItem调用父组件
      onChange() {
        let activeItem = []
        this.childrens.forEach((child, index) => {
          if (child.isShow) {
            activeItem.push(child.nameSync)
          }
        })
        // 如果时手风琴模式,只有一个匹配结果,即activeItem长度为1
        if (this.accordion) activeItem = activeItem.join(',')
        this.$emit('change', activeItem)
      }
    }
  }
</script>
 
<style lang="scss" scoped>
</style>