index.vue 3.7 KB

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