tn-tree-node.vue 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <template>
  2. <view class="tn-tree-node-class tn-tree-node">
  3. <view class="tn-tree__label" @tap="handleClick">
  4. <view
  5. v-if="node.children && node.children.length > 0 && triangle"
  6. class="tn-tree__triangle"
  7. :class="[{'tn-tree__triangle--90deg': !collapsed}]"
  8. ></view>
  9. <view class="tn-tree__label__item">
  10. <view v-if="collapsed && node.image" class="tn-tree__label__item__image">
  11. <image :src="node.image" mode="widthFix"></image>
  12. </view>
  13. <view v-if="!collapsed && node.activeImage" class="tn-tree__label__item__image">
  14. <image :src="node.activeImage" mode="widthFix"></image>
  15. </view>
  16. <view class="tn-tree__label__item__text">{{ node.text }}</view>
  17. </view>
  18. </view>
  19. <view v-if="!collapsed && node.children && node.children.length > 0" class="tn-tree__children">
  20. <tn-tree-node
  21. v-for="(item, index) in node.children"
  22. :key="index"
  23. :node="item"
  24. :collapsible="collapsible"
  25. :triangle="triangle"
  26. @click="nodeClick"
  27. ></tn-tree-node>
  28. </view>
  29. </view>
  30. </template>
  31. <script>
  32. //如果未开启easycom模式,请自行引入tn-tree-node组件
  33. export default {
  34. name: 'tn-tree-node',
  35. props: {
  36. // 节点信息
  37. node: {
  38. type: Object,
  39. default() {
  40. return {}
  41. }
  42. },
  43. // 可以折叠
  44. collapsible: {
  45. type: Boolean,
  46. default: true
  47. },
  48. // 显示三角形
  49. triangle: {
  50. type: Boolean,
  51. default: true
  52. }
  53. },
  54. watch: {
  55. node(val) {
  56. if (val.collapsed !== this.collapsed && this.node.children && this.node.children.length > 0) {
  57. this.collapsed = val.collapsed
  58. }
  59. }
  60. },
  61. data() {
  62. return {
  63. // 标记是否折叠
  64. collapsed: true
  65. }
  66. },
  67. created() {
  68. if (this.node.collapsed === false) {
  69. this.collapsed = false
  70. }
  71. },
  72. methods: {
  73. // 处理点击
  74. handleClick(e) {
  75. if (this.collapsible && this.node.children && this.node.children.length > 0) {
  76. this.collapsed = !this.collapsed
  77. }
  78. this.$emit('click', this.node)
  79. },
  80. nodeClick(e) {
  81. this.$emit('click', e)
  82. }
  83. }
  84. }
  85. </script>
  86. <style lang="scss" scoped>
  87. .tn-tree-node {
  88. .tn-tree {
  89. &__label {
  90. position: relative;
  91. display: inline-flex;
  92. align-items: center;
  93. padding: 20rpx 30rpx;
  94. background-color: transparent;
  95. /* #ifdef H5 */
  96. cursor: pointer;
  97. /* #endif */
  98. // 字体抗锯齿
  99. -webkit-font-smoothing: antialiased;
  100. &__item {
  101. display: flex;
  102. flex-direction: row;
  103. align-items: center;
  104. &__image {
  105. width: 40rpx;
  106. height: 40rpx;
  107. margin-right: 16rpx;
  108. image {
  109. width: 100%;
  110. height: 100%;
  111. }
  112. }
  113. }
  114. }
  115. &__children {
  116. padding-left: 60rpx;
  117. position: relative;
  118. }
  119. &__triangle {
  120. width: 0;
  121. height: 0;
  122. border-top: 12rpx solid transparent;
  123. border-bottom: 12rpx solid transparent;
  124. border-left: 16rpx solid #080808;
  125. margin-right: 20rpx;
  126. transition: transform 0.25s ease-out;
  127. flex-shrink: 0;
  128. &--90deg {
  129. transform:rotate(90deg) translate3d(0, 0, 0);
  130. }
  131. }
  132. }
  133. }
  134. </style>