吉光研途前端代码仓库
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

157 lines
3.1 KiB

  1. <template>
  2. <view class="drop-down">
  3. <button class="btn" plain @click="openPicker" >
  4. <view class="flex btn-label">
  5. <view :style="{ color }">{{ label }}</view>
  6. <uv-icon name="arrow-down-fill" :color="color" size="14rpx"></uv-icon>
  7. </view>
  8. </button>
  9. <template v-if="popupVisible">
  10. <view class="drop-down-overlay" @click="closePicker"></view>
  11. <view class="card drop-down-popup" :style="popupStyle">
  12. <view
  13. v-for="item in options"
  14. class="flex drop-down-option"
  15. :class="[item.value === value ? 'is-active' : '']"
  16. :key="item.value"
  17. @click="onSelect(item.value)"
  18. >
  19. <text>{{ item.label }}</text>
  20. </view>
  21. </view>
  22. </template>
  23. </view>
  24. </template>
  25. <script>
  26. export default {
  27. props: {
  28. value: {
  29. type: String | Number,
  30. default: null,
  31. },
  32. options: {
  33. type: Array,
  34. default() {
  35. return []
  36. }
  37. },
  38. label: {
  39. type: String,
  40. default: ''
  41. },
  42. },
  43. data() {
  44. return {
  45. popupVisible: false,
  46. popupStyle: {},
  47. }
  48. },
  49. computed: {
  50. isActive() {
  51. return this.options.find(option => option.value === this.value)
  52. },
  53. color() {
  54. return this.isActive ? '#4883F9' : '#000000'
  55. }
  56. },
  57. methods: {
  58. openPicker() {
  59. this.popupVisible = true
  60. return
  61. uni.createSelectorQuery().in(this)
  62. .select('.btn')
  63. .fields({
  64. node: true,
  65. size: true,
  66. rect: true,
  67. })
  68. .exec(async (res) => {
  69. console.log('res', res)
  70. const {
  71. top,
  72. left,
  73. } = res[0]
  74. console.log('top', top, 'left', left)
  75. // this.popupStyle = {
  76. // top: `${parseInt(top)}px`,
  77. // left: `${parseInt(left)}px`,
  78. // }
  79. console.log('popupStyle', this.popupStyle)
  80. this.popupVisible = true
  81. })
  82. },
  83. closePicker() {
  84. this.popupVisible = false
  85. },
  86. onSelect(val) {
  87. const selected = this.value
  88. const newVal = selected === val ? null : val
  89. this.$emit('input', newVal)
  90. this.$emit('change', newVal)
  91. },
  92. },
  93. }
  94. </script>
  95. <style scoped lang="scss">
  96. .drop-down {
  97. position: relative;
  98. clear: both;
  99. .btn {
  100. width: 213rpx;
  101. padding: 27rpx 0;
  102. font-size: 24rpx;
  103. background: transparent;
  104. border: none;
  105. &-label {
  106. column-gap: 9rpx;
  107. }
  108. }
  109. &-overlay {
  110. position: fixed;
  111. width: 100vw;
  112. height: calc(100vh - #{$navbar-height} - var(--status-bar-height) - 20rpx);
  113. background: transparent;
  114. bottom: 0;
  115. left: 0;
  116. font-size: 0;
  117. }
  118. &-popup {
  119. position: absolute;
  120. z-index: 99;
  121. width: 213rpx;
  122. background: #FFFFFF;
  123. border-radius: 15rpx;
  124. transform: translateY(-15rpx);
  125. }
  126. &-option {
  127. color: #000000;
  128. font-size: 24rpx;
  129. padding: 7rpx 0;
  130. text-align: center;
  131. &.is-active {
  132. color: #4883F9;
  133. }
  134. & + & {
  135. border-top: 1rpx solid #EFEFEF;
  136. }
  137. }
  138. }
  139. </style>