index.vue 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. <template>
  2. <view class="page-select-mask" v-if="modelValue" @click="onClose" @touchmove.stop.prevent>
  3. <view class="page-select-container" @click.stop @touchmove.stop>
  4. <!-- 标题栏 -->
  5. <view class="select-header">
  6. <text class="select-title">{{ title }}</text>
  7. <view class="close-btn" @click="onClose">
  8. <view class="line line1"></view>
  9. <view class="line line2"></view>
  10. </view>
  11. </view>
  12. <!-- 搜索栏(可选) -->
  13. <view class="search-bar" v-if="searchable">
  14. <view class="search-box">
  15. <view class="search-icon"></view>
  16. <input class="search-input" v-model="localSearchKey" :placeholder="searchPlaceholder" @confirm="onSearch" confirm-type="search" />
  17. <view class="search-btn" @click="onSearch">查询</view>
  18. </view>
  19. </view>
  20. <!-- 列表区 -->
  21. <view class="select-list-wrapper" @touchmove.stop>
  22. <scroll-view scroll-y class="select-list" @scrolltolower="onScrollBottom" lower-threshold="80">
  23. <view class="select-list-inner">
  24. <view
  25. v-for="(item, index) in options"
  26. :key="index"
  27. class="select-item"
  28. :class="{ 'active': isSelected(item) }"
  29. @click="onSelect(item)"
  30. >
  31. <!-- 默认插槽:自定义每行内容 -->
  32. <slot name="item" :item="item" :index="index">
  33. <text class="item-label">{{ getLabel(item) }}</text>
  34. </slot>
  35. <!-- 选中对勾 -->
  36. <view class="checkmark" v-if="isSelected(item)"></view>
  37. </view>
  38. <view class="loading-tip" v-if="loading">加载中...</view>
  39. <view class="no-more-tip" v-else-if="finished && options.length > 0">没有更多了</view>
  40. <view class="empty-tip" v-if="options.length === 0 && !loading">{{ emptyText }}</view>
  41. </view>
  42. </scroll-view>
  43. </view>
  44. </view>
  45. </view>
  46. </template>
  47. <script setup>
  48. /**
  49. * 分页选择器组件(纯 UI,不涉及网络调用)
  50. * 支持搜索 + 滑动到底部触发分页
  51. * @Author: Antigravity
  52. */
  53. import { ref, watch } from 'vue'
  54. const props = defineProps({
  55. modelValue: Boolean,
  56. title: { type: String, default: '请选择' },
  57. options: { type: Array, default: () => [] },
  58. value: { type: [String, Number], default: '' },
  59. labelKey: { type: String, default: 'label' },
  60. valueKey: { type: String, default: 'value' },
  61. loading: { type: Boolean, default: false },
  62. finished: { type: Boolean, default: true },
  63. searchable: { type: Boolean, default: false },
  64. searchKey: { type: String, default: '' },
  65. searchPlaceholder: { type: String, default: '请输入关键词搜索' },
  66. emptyText: { type: String, default: '暂无选项' }
  67. })
  68. const emit = defineEmits(['update:modelValue', 'select', 'loadMore', 'search'])
  69. const localSearchKey = ref(props.searchKey)
  70. watch(() => props.searchKey, (val) => { localSearchKey.value = val })
  71. const getLabel = (item) => {
  72. if (typeof item === 'string' || typeof item === 'number') return item
  73. return item[props.labelKey]
  74. }
  75. const getValue = (item) => {
  76. if (typeof item === 'string' || typeof item === 'number') return item
  77. return item[props.valueKey]
  78. }
  79. const isSelected = (item) => getValue(item) === props.value
  80. const onSelect = (item) => {
  81. emit('select', item)
  82. emit('update:modelValue', false)
  83. }
  84. const onClose = () => {
  85. emit('update:modelValue', false)
  86. }
  87. const onScrollBottom = () => {
  88. if (!props.loading && !props.finished) {
  89. emit('loadMore')
  90. }
  91. }
  92. const onSearch = () => {
  93. emit('search', localSearchKey.value)
  94. }
  95. </script>
  96. <style lang="scss" scoped>
  97. .page-select-mask {
  98. position: fixed;
  99. top: 0; left: 0; right: 0; bottom: 0;
  100. background: rgba(0, 0, 0, 0.6);
  101. display: flex;
  102. align-items: center;
  103. justify-content: center;
  104. z-index: 9999;
  105. backdrop-filter: blur(2px);
  106. }
  107. .page-select-container {
  108. width: 620rpx;
  109. background: #fff;
  110. border-radius: 32rpx;
  111. display: flex;
  112. flex-direction: column;
  113. max-height: 70vh;
  114. overflow: hidden;
  115. animation: popIn 0.25s ease-out;
  116. }
  117. @keyframes popIn {
  118. from { transform: scale(0.8); opacity: 0; }
  119. to { transform: scale(1); opacity: 1; }
  120. }
  121. .select-header {
  122. display: flex;
  123. justify-content: space-between;
  124. align-items: center;
  125. padding: 32rpx 40rpx;
  126. border-bottom: 2rpx solid #f2f2f2;
  127. .select-title {
  128. font-size: 32rpx;
  129. font-weight: bold;
  130. color: #333;
  131. }
  132. }
  133. .close-btn {
  134. width: 40rpx;
  135. height: 40rpx;
  136. position: relative;
  137. .line {
  138. position: absolute;
  139. top: 50%; left: 50%;
  140. width: 30rpx; height: 4rpx;
  141. background: #999;
  142. border-radius: 4rpx;
  143. }
  144. .line1 { transform: translate(-50%, -50%) rotate(45deg); }
  145. .line2 { transform: translate(-50%, -50%) rotate(-45deg); }
  146. }
  147. .search-bar {
  148. padding: 20rpx 32rpx;
  149. border-bottom: 2rpx solid #f2f2f2;
  150. }
  151. .search-box {
  152. display: flex;
  153. align-items: center;
  154. background: #f5f5f5;
  155. border-radius: 36rpx;
  156. padding: 0 24rpx;
  157. height: 72rpx;
  158. .search-icon {
  159. width: 20rpx; height: 20rpx;
  160. border: 3rpx solid #999;
  161. border-radius: 50%;
  162. margin-right: 12rpx;
  163. position: relative;
  164. &::after {
  165. content: '';
  166. width: 10rpx; height: 3rpx;
  167. background: #999;
  168. position: absolute;
  169. bottom: -4rpx; right: -4rpx;
  170. transform: rotate(45deg);
  171. }
  172. }
  173. .search-input {
  174. flex: 1;
  175. font-size: 26rpx;
  176. }
  177. .search-btn {
  178. font-size: 26rpx;
  179. color: #ff9500;
  180. font-weight: bold;
  181. margin-left: 20rpx;
  182. }
  183. }
  184. .select-list-wrapper {
  185. flex: 1;
  186. min-height: 0;
  187. overflow: hidden;
  188. }
  189. .select-list {
  190. max-height: 55vh;
  191. }
  192. .select-list-inner {
  193. padding: 0 32rpx;
  194. }
  195. .select-item {
  196. display: flex;
  197. align-items: center;
  198. justify-content: space-between;
  199. padding: 30rpx 0;
  200. border-bottom: 2rpx solid #f9f9f9;
  201. &:active { background: #f7f8fa; }
  202. &.active .item-label { color: #ff9500; font-weight: bold; }
  203. .item-label { font-size: 28rpx; color: #333; flex: 1; }
  204. }
  205. .checkmark {
  206. width: 12rpx; height: 22rpx;
  207. border-right: 4rpx solid #ff9500;
  208. border-bottom: 4rpx solid #ff9500;
  209. transform: rotate(45deg);
  210. flex-shrink: 0;
  211. }
  212. .loading-tip, .no-more-tip {
  213. padding: 30rpx 0;
  214. text-align: center;
  215. color: #bbb;
  216. font-size: 24rpx;
  217. }
  218. .empty-tip {
  219. padding: 80rpx 0;
  220. text-align: center;
  221. color: #ccc;
  222. font-size: 26rpx;
  223. }
  224. </style>