DeptSelector.vue 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <template>
  2. <u-popup :show="show" mode="bottom" :round="16" :mask-close-able="true" :safe-area-inset-bottom="true"
  3. :close-on-click-mask="true" @close="onClose">
  4. <view class="dept-picker">
  5. <view class="picker-header">
  6. <text class="picker-title">选择{{ title }}</text>
  7. </view>
  8. <view class="search-box">
  9. <u-input v-model="deptSearchKeyword" :placeholder="`搜索${title}`" :border="false" @confirm="onDeptSearch"
  10. @input="onDeptSearch" />
  11. </view>
  12. <scroll-view scroll-y class="dept-list" :enable-back-to-top="true">
  13. <view class="dept-item" v-for="item in filteredDeptList" :key="item.deptId"
  14. @click="onDeptSelect(item)">
  15. <text class="dept-item-name">{{ item.deptName }}</text>
  16. <u-icon v-if="item.deptId === selectedDeptId" name="checkmark" color="#34D399"
  17. size="18"></u-icon>
  18. </view>
  19. <view v-if="filteredDeptList.length === 0 && !loading" class="empty-state">
  20. <text>暂无数据</text>
  21. </view>
  22. <view v-if="loading" class="loading-state">
  23. <u-loading-icon text="加载中" textSize="14"></u-loading-icon>
  24. </view>
  25. </scroll-view>
  26. </view>
  27. </u-popup>
  28. </template>
  29. <script>
  30. export default {
  31. name: 'DeptSelector',
  32. props: {
  33. title: {
  34. type: String,
  35. default: '部门'
  36. },
  37. show: {
  38. type: Boolean,
  39. default: false
  40. },
  41. value: {
  42. type: [String, Number],
  43. default: null
  44. },
  45. options: {
  46. type: Array,
  47. default: () => []
  48. },
  49. loading: {
  50. type: Boolean,
  51. default: false
  52. }
  53. },
  54. data() {
  55. return {
  56. selectedDeptId: this.value,
  57. filteredDeptList: [...this.options],
  58. deptSearchKeyword: ''
  59. }
  60. },
  61. watch: {
  62. value(newVal) {
  63. this.selectedDeptId = newVal
  64. },
  65. options(newVal) {
  66. this.filteredDeptList = [...newVal]
  67. },
  68. show(newVal) {
  69. if (newVal) {
  70. this.deptSearchKeyword = ''
  71. this.filteredDeptList = [...this.options]
  72. }
  73. }
  74. },
  75. methods: {
  76. onDeptSearch() {
  77. const keyword = this.deptSearchKeyword.trim()
  78. if (!keyword) {
  79. this.filteredDeptList = [...this.options]
  80. } else {
  81. this.filteredDeptList = this.options.filter(item =>
  82. item.deptName.includes(keyword)
  83. )
  84. }
  85. },
  86. onDeptSelect(item) {
  87. this.selectedDeptId = item.deptId
  88. this.$emit('input', item.deptId)
  89. this.$emit('change', item)
  90. this.$emit('update:show', false)
  91. },
  92. onClose() {
  93. this.$emit('update:show', false)
  94. }
  95. }
  96. }
  97. </script>
  98. <style lang="scss" scoped>
  99. .dept-picker {
  100. background: #fff;
  101. display: flex;
  102. flex-direction: column;
  103. border-radius: 16rpx 16rpx 0 0;
  104. width: 100%;
  105. height: 70vh;
  106. }
  107. .picker-header {
  108. padding: 24rpx 32rpx;
  109. border-bottom: 1rpx solid #eee;
  110. display: flex;
  111. justify-content: center;
  112. flex-shrink: 0;
  113. }
  114. .picker-title {
  115. font-size: 32rpx;
  116. font-weight: 600;
  117. color: #333;
  118. }
  119. .search-box {
  120. padding: 16rpx 32rpx;
  121. flex-shrink: 0;
  122. }
  123. .dept-list {
  124. flex: 1;
  125. padding: 0 32rpx;
  126. height: 0;
  127. overflow: hidden;
  128. }
  129. .dept-item {
  130. display: flex;
  131. align-items: center;
  132. justify-content: space-between;
  133. padding: 24rpx 0;
  134. border-bottom: 1rpx solid #f0f0f0;
  135. }
  136. .dept-item-name {
  137. font-size: 28rpx;
  138. color: #333;
  139. }
  140. .empty-state,
  141. .loading-state {
  142. padding: 48rpx 0;
  143. display: flex;
  144. justify-content: center;
  145. align-items: center;
  146. color: #999;
  147. font-size: 24rpx;
  148. }
  149. </style>