cl-tree-item.uvue 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <template>
  2. <view class="cl-tree-item__wrapper" :class="[`cl-tree-item--level-${level}`, pt.className]">
  3. <view
  4. class="cl-tree-item"
  5. :class="[
  6. {
  7. 'is-expand': hover,
  8. 'is-dark': isDark
  9. },
  10. pt.className
  11. ]"
  12. @touchstart="onTouchStart"
  13. @touchend="onTouchEnd"
  14. @touchcancel="onTouchEnd"
  15. >
  16. <view class="cl-tree-item__expand" :class="[pt.expand?.className]">
  17. <cl-icon
  18. :name="icon"
  19. :size="pt.expandIcon?.size ?? 36"
  20. :color="pt.expandIcon?.color"
  21. :pt="{
  22. className: pt.expandIcon?.className
  23. }"
  24. v-if="hasChildren"
  25. ></cl-icon>
  26. </view>
  27. <cl-text
  28. :pt="{
  29. className: parseClass(['flex-1 mx-1', pt.label?.className])
  30. }"
  31. >{{ item.label }}</cl-text
  32. >
  33. <template v-if="ClTree?.showCheckbox">
  34. <view
  35. class="cl-tree-item__checkbox"
  36. :class="[pt.checkbox?.className]"
  37. @touchstart.stop="toChecked"
  38. >
  39. <cl-icon
  40. v-if="item.isChecked"
  41. :name="pt.checkedIcon?.name ?? 'checkbox-circle-fill'"
  42. :size="pt.checkedIcon?.size ?? 38"
  43. :color="pt.checkedIcon?.color ?? 'primary'"
  44. ></cl-icon>
  45. <cl-icon
  46. v-else-if="item.isHalfChecked"
  47. :name="pt.halfCheckedIcon?.name ?? 'indeterminate-circle-line'"
  48. :size="pt.halfCheckedIcon?.size ?? 38"
  49. :color="pt.halfCheckedIcon?.color ?? 'primary'"
  50. ></cl-icon>
  51. <cl-icon
  52. v-else
  53. :name="pt.uncheckedIcon?.name ?? 'checkbox-blank-circle-line'"
  54. :size="pt.uncheckedIcon?.size ?? 38"
  55. :color="pt.uncheckedIcon?.color ?? 'info'"
  56. ></cl-icon>
  57. </view>
  58. </template>
  59. </view>
  60. <template v-if="hasChildren && item.isExpand == true">
  61. <cl-tree-item
  62. v-for="item in item.children"
  63. :key="item.id"
  64. :item="item"
  65. :level="level + 1"
  66. :pt="props.pt"
  67. ></cl-tree-item>
  68. </template>
  69. </view>
  70. </template>
  71. <script lang="ts" setup>
  72. import { computed, ref, type PropType } from "vue";
  73. import { isDark, parseClass, parsePt, useParent } from "@/cool";
  74. import type { ClTreeItem, PassThroughProps } from "../../types";
  75. import type { ClIconProps } from "../cl-icon/props";
  76. defineOptions({
  77. name: "cl-tree-item"
  78. });
  79. const props = defineProps({
  80. pt: {
  81. type: Object,
  82. default: () => ({})
  83. },
  84. item: {
  85. type: Object as PropType<ClTreeItem>,
  86. default: () => ({})
  87. },
  88. level: {
  89. type: Number,
  90. default: 0
  91. }
  92. });
  93. // 透传属性类型定义,支持自定义各部分样式和图标
  94. type PassThrough = {
  95. className?: string; // 外层自定义类名
  96. wrapper?: PassThroughProps; // 外层包裹属性
  97. expand?: PassThroughProps; // 展开区域属性
  98. expandIcon?: ClIconProps; // 展开图标属性
  99. checkbox?: PassThroughProps; // 复选框区域属性
  100. checkedIcon?: ClIconProps; // 选中图标属性
  101. halfCheckedIcon?: ClIconProps; // 半选图标属性
  102. uncheckedIcon?: ClIconProps; // 未选中图标属性
  103. label?: PassThroughProps; // 标签属性
  104. };
  105. // 解析pt透传属性,便于自定义样式和图标
  106. const pt = computed(() => parsePt<PassThrough>(props.pt));
  107. // 获取父级cl-tree组件实例,用于调用树的相关方法
  108. const ClTree = useParent<ClTreeComponentPublicInstance>("cl-tree");
  109. // 判断当前节点是否有子节点
  110. const hasChildren = computed(() => props.item.children != null && props.item.children.length > 0);
  111. // 计算当前节点应显示的图标(展开/收起)
  112. const icon = computed(() => {
  113. if (ClTree == null) {
  114. return "";
  115. }
  116. return props.item.isExpand == true ? ClTree.expandIcon : ClTree.icon;
  117. });
  118. // 切换当前节点的展开状态
  119. function toExpand() {
  120. ClTree!.setExpanded(props.item.id, !(props.item.isExpand ?? false));
  121. }
  122. // 切换当前节点的选中状态
  123. function toChecked() {
  124. ClTree!.setChecked(props.item.id, !(props.item.isChecked ?? false));
  125. }
  126. // 控制节点按下时的hover状态
  127. const hover = ref(false);
  128. // 触摸开始时触发,设置hover并展开/收起
  129. function onTouchStart() {
  130. hover.value = true;
  131. toExpand();
  132. }
  133. // 触摸结束时触发,取消hover
  134. function onTouchEnd() {
  135. hover.value = false;
  136. }
  137. </script>
  138. <style lang="scss" scoped>
  139. .cl-tree-item__wrapper {
  140. @apply pl-8;
  141. }
  142. .cl-tree-item {
  143. @apply flex flex-row items-center w-full p-2 rounded-lg;
  144. &__expand {
  145. @apply w-6 flex items-center justify-center;
  146. }
  147. &.is-expand {
  148. @apply bg-surface-50;
  149. &.is-dark {
  150. @apply bg-surface-700;
  151. }
  152. }
  153. &--level-0 {
  154. @apply pl-0;
  155. }
  156. }
  157. </style>