index.vue 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <template>
  2. <view class="center-select-mask" v-if="modelValue" @click="onClose" @touchmove.stop.prevent>
  3. <view class="center-select-container" @click.stop>
  4. <view class="select-header">
  5. <text class="select-title">{{ title }}</text>
  6. <!-- CSS 绘制的关闭按钮 @Author: Antigravity -->
  7. <view class="close-btn" @click="onClose">
  8. <view class="line line1"></view>
  9. <view class="line line2"></view>
  10. </view>
  11. </view>
  12. <scroll-view scroll-y class="select-content" @scrolltolower="onScrollBottom" lower-threshold="80">
  13. <view
  14. v-for="(item, index) in options"
  15. :key="index"
  16. class="select-item"
  17. :class="{ 'active': isSelected(item) }"
  18. @click="onSelect(item)"
  19. >
  20. <text class="item-label">{{ getLabel(item) }}</text>
  21. <!-- CSS 绘制的选中对勾 @Author: Antigravity -->
  22. <view class="checkmark" v-if="isSelected(item)"></view>
  23. </view>
  24. <view class="loading-tip" v-if="loading">加载中...</view>
  25. <view class="no-more-tip" v-else-if="finished && options.length > 0">没有更多了</view>
  26. <view class="empty-tip" v-if="options.length === 0 && !loading">暂无选项</view>
  27. </scroll-view>
  28. </view>
  29. </view>
  30. </template>
  31. <script setup>
  32. /**
  33. * @Author: Antigravity
  34. */
  35. import { ref } from 'vue'
  36. const props = defineProps({
  37. modelValue: Boolean, // 控制显示隐藏
  38. title: {
  39. type: String,
  40. default: '请选择'
  41. },
  42. options: {
  43. type: Array,
  44. default: () => []
  45. },
  46. value: {
  47. type: [String, Number],
  48. default: ''
  49. },
  50. labelKey: {
  51. type: String,
  52. default: 'label'
  53. },
  54. valueKey: {
  55. type: String,
  56. default: 'value'
  57. },
  58. loading: {
  59. type: Boolean,
  60. default: false
  61. },
  62. finished: {
  63. type: Boolean,
  64. default: true
  65. }
  66. })
  67. const emit = defineEmits(['update:modelValue', 'select', 'loadMore'])
  68. const getLabel = (item) => {
  69. if (typeof item === 'string' || typeof item === 'number') return item
  70. return item[props.labelKey]
  71. }
  72. const getValue = (item) => {
  73. if (typeof item === 'string' || typeof item === 'number') return item
  74. return item[props.valueKey]
  75. }
  76. const isSelected = (item) => {
  77. return getValue(item) === props.value
  78. }
  79. const onSelect = (item) => {
  80. emit('select', item)
  81. emit('update:modelValue', false)
  82. }
  83. const onClose = () => {
  84. emit('update:modelValue', false)
  85. }
  86. const onScrollBottom = () => {
  87. if (!props.loading && !props.finished) {
  88. emit('loadMore')
  89. }
  90. }
  91. </script>
  92. <style lang="scss" scoped>
  93. .center-select-mask {
  94. position: fixed;
  95. top: 0;
  96. left: 0;
  97. right: 0;
  98. bottom: 0;
  99. background: rgba(0, 0, 0, 0.6);
  100. display: flex;
  101. align-items: center;
  102. justify-content: center;
  103. z-index: 9999;
  104. backdrop-filter: blur(2px);
  105. }
  106. .center-select-container {
  107. width: 600rpx;
  108. background: #ffffff;
  109. border-radius: 32rpx;
  110. overflow: hidden;
  111. display: flex;
  112. flex-direction: column;
  113. max-height: 70vh;
  114. animation: popIn 0.25s ease-out;
  115. }
  116. @keyframes popIn {
  117. from { transform: scale(0.8); opacity: 0; }
  118. to { transform: scale(1); opacity: 1; }
  119. }
  120. .select-header {
  121. display: flex;
  122. justify-content: space-between;
  123. align-items: center;
  124. padding: 32rpx 40rpx;
  125. border-bottom: 2rpx solid #f2f2f2;
  126. .select-title {
  127. font-size: 32rpx;
  128. font-weight: bold;
  129. color: #333;
  130. }
  131. }
  132. /* 原生 CSS 关闭按钮 X @Author: Antigravity */
  133. .close-btn {
  134. width: 40rpx;
  135. height: 40rpx;
  136. position: relative;
  137. .line {
  138. position: absolute;
  139. top: 50%;
  140. left: 50%;
  141. width: 30rpx;
  142. height: 4rpx;
  143. background: #999;
  144. border-radius: 4rpx;
  145. }
  146. .line1 { transform: translate(-50%, -50%) rotate(45deg); }
  147. .line2 { transform: translate(-50%, -50%) rotate(-45deg); }
  148. }
  149. .select-content {
  150. flex: 1;
  151. padding: 10rpx 0;
  152. }
  153. .select-item {
  154. display: flex;
  155. justify-content: space-between;
  156. align-items: center;
  157. padding: 32rpx 40rpx;
  158. min-height: 100rpx;
  159. box-sizing: border-box;
  160. &:active { background: #f7f8fa; }
  161. &.active {
  162. .item-label { color: #ff9500; font-weight: bold; }
  163. }
  164. .item-label { font-size: 30rpx; color: #444; }
  165. }
  166. /* 原生 CSS 对勾 @Author: Antigravity */
  167. .checkmark {
  168. width: 12rpx;
  169. height: 24rpx;
  170. border-right: 4rpx solid #ff9500;
  171. border-bottom: 4rpx solid #ff9500;
  172. transform: rotate(45deg);
  173. margin-right: 10rpx;
  174. }
  175. .empty-tip {
  176. padding: 60rpx 0;
  177. text-align: center;
  178. color: #999;
  179. font-size: 26rpx;
  180. }
  181. .loading-tip, .no-more-tip {
  182. padding: 30rpx 0;
  183. text-align: center;
  184. color: #bbb;
  185. font-size: 24rpx;
  186. }
  187. </style>