cl-tree-item.uvue 5.1 KB

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