吉光研途前端代码仓库
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.

134 lines
2.7 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. console.log('openPicker', this.label)
  60. this.popupVisible = true
  61. },
  62. closePicker() {
  63. console.log('closePicker', this.label)
  64. this.popupVisible = false
  65. },
  66. onSelect(val) {
  67. const selected = this.value
  68. const newVal = selected === val ? null : val
  69. this.$emit('input', newVal)
  70. this.$emit('change', newVal)
  71. this.closePicker()
  72. },
  73. },
  74. }
  75. </script>
  76. <style scoped lang="scss">
  77. .drop-down {
  78. position: relative;
  79. clear: both;
  80. .btn {
  81. width: 213rpx;
  82. padding: 27rpx 0;
  83. font-size: 24rpx;
  84. background: transparent;
  85. border: none;
  86. &-label {
  87. column-gap: 9rpx;
  88. }
  89. }
  90. &-overlay {
  91. position: fixed;
  92. width: 100vw;
  93. height: calc(100vh - #{$navbar-height} - var(--status-bar-height) - 20rpx);
  94. background: transparent;
  95. bottom: 0;
  96. left: 0;
  97. z-index: 98;
  98. font-size: 0;
  99. }
  100. &-popup {
  101. position: absolute;
  102. z-index: 99;
  103. width: 213rpx;
  104. background: #FFFFFF;
  105. border-radius: 15rpx;
  106. transform: translateY(-15rpx);
  107. }
  108. &-option {
  109. color: #000000;
  110. font-size: 24rpx;
  111. padding: 7rpx 0;
  112. text-align: center;
  113. &.is-active {
  114. color: #4883F9;
  115. }
  116. & + & {
  117. border-top: 1rpx solid #EFEFEF;
  118. }
  119. }
  120. }
  121. </style>