EmployeeTreeNode.vue 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <template>
  2. <view class="tree-node">
  3. <view
  4. :class="['node-row', { 'is-dept': isDept, 'is-user': isUser }]"
  5. @click="handleClick"
  6. >
  7. <view class="toggle-icon" v-if="isDept">
  8. <u-icon :name="expanded ? 'arrow-down' : 'arrow-right'" size="16" color="#999" />
  9. </view>
  10. <view class="toggle-icon" v-else>
  11. <text class="user-dot">●</text>
  12. </view>
  13. <text class="node-label">{{ nodeLabel }}</text>
  14. <u-icon v-if="isUser && nodeId === selectedId" name="checkmark" color="#34D399" size="18" />
  15. </view>
  16. <view v-if="isDept && expanded" class="node-children">
  17. <template v-for="child in nodeChildren">
  18. <employee-tree-node
  19. :key="child.id"
  20. :node="child"
  21. :expanded-ids="expandedIds"
  22. :selected-id="selectedId"
  23. @toggle="$emit('toggle', $event)"
  24. @select="$emit('select', $event)"
  25. />
  26. </template>
  27. </view>
  28. </view>
  29. </template>
  30. <script>
  31. export default {
  32. name: 'EmployeeTreeNode',
  33. props: {
  34. node: {
  35. type: Object,
  36. required: true
  37. },
  38. expandedIds: {
  39. type: [Array, Set],
  40. default: () => []
  41. },
  42. selectedId: {
  43. type: [String, Number],
  44. default: null
  45. }
  46. },
  47. computed: {
  48. isDept() {
  49. const hasChildren = this.node.children && this.node.children.length > 0
  50. return hasChildren || this.node.nodeType === 'dept'
  51. },
  52. isUser() {
  53. return !this.isDept
  54. },
  55. nodeId() {
  56. return this.node.userId || this.node.id
  57. },
  58. nodeLabel() {
  59. return this.node.nickName || this.node.label || this.node.userName || this.node.name || ''
  60. },
  61. nodeChildren() {
  62. return this.node.children || []
  63. },
  64. expanded() {
  65. if (this.expandedIds instanceof Set) {
  66. return this.expandedIds.has(this.node.id)
  67. }
  68. return this.expandedIds.includes(this.node.id)
  69. }
  70. },
  71. methods: {
  72. handleClick() {
  73. if (this.isDept) {
  74. this.$emit('toggle', this.node.id)
  75. } else {
  76. this.$emit('select', {
  77. userId: this.nodeId,
  78. nickName: this.nodeLabel
  79. })
  80. }
  81. }
  82. }
  83. }
  84. </script>
  85. <style lang="scss" scoped>
  86. .tree-node {
  87. width: 100%;
  88. }
  89. .node-row {
  90. display: flex;
  91. align-items: center;
  92. padding: 20rpx 32rpx 20rpx 16rpx;
  93. gap: 12rpx;
  94. &.is-dept {
  95. padding-left: 16rpx;
  96. }
  97. }
  98. .toggle-icon {
  99. width: 40rpx;
  100. height: 40rpx;
  101. display: flex;
  102. align-items: center;
  103. justify-content: center;
  104. }
  105. .user-dot {
  106. font-size: 16rpx;
  107. color: #999;
  108. }
  109. .node-label {
  110. flex: 1;
  111. font-size: 28rpx;
  112. color: #333;
  113. }
  114. .node-children {
  115. padding-left: 32rpx;
  116. }
  117. </style>