tn-collapse.vue 2.1 KB

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