cl-tree-item.uvue 5.1 KB

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