SelectCustom.vue 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <template>
  2. <div class="select-wrap" v-if="disabled">
  3. <div class="select disabled" ref="selectRef">
  4. <div class="selector"><slot name="label"></slot></div>
  5. <div class="icon">
  6. <slot name="icon">
  7. <IconDown :size="14" />
  8. </slot>
  9. </div>
  10. </div>
  11. </div>
  12. <Popover
  13. class="select-wrap"
  14. trigger="click"
  15. v-model:value="popoverVisible"
  16. placement="bottom"
  17. :contentStyle="{
  18. padding: 0,
  19. boxShadow: '0 6px 16px 0 rgba(0, 0, 0, 0.08)',
  20. }"
  21. v-else
  22. >
  23. <template #content>
  24. <div class="options" :style="{ width: width + 2 + 'px' }" @click="popoverVisible = false">
  25. <slot name="options"></slot>
  26. </div>
  27. </template>
  28. <div class="select" ref="selectRef">
  29. <div class="selector"><slot name="label"></slot></div>
  30. <div class="icon">
  31. <slot name="icon">
  32. <IconDown :size="14" />
  33. </slot>
  34. </div>
  35. </div>
  36. </Popover>
  37. </template>
  38. <script lang="ts" setup>
  39. import { onMounted, onUnmounted, ref, useTemplateRef } from 'vue'
  40. import Popover from './Popover.vue'
  41. withDefaults(defineProps<{
  42. disabled?: boolean
  43. }>(), {
  44. disabled: false,
  45. })
  46. const popoverVisible = ref(false)
  47. const selectRef = useTemplateRef<HTMLElement>('selectRef')
  48. const width = ref(0)
  49. const updateWidth = () => {
  50. if (!selectRef.value) return
  51. width.value = selectRef.value.clientWidth
  52. }
  53. const resizeObserver = new ResizeObserver(updateWidth)
  54. onMounted(() => {
  55. if (!selectRef.value) return
  56. resizeObserver.observe(selectRef.value)
  57. })
  58. onUnmounted(() => {
  59. if (!selectRef.value) return
  60. resizeObserver.unobserve(selectRef.value)
  61. })
  62. </script>
  63. <style lang="scss" scoped>
  64. .select {
  65. width: 100%;
  66. height: 32px;
  67. padding-right: 32px;
  68. border-radius: $borderRadius;
  69. transition: border-color .25s;
  70. font-size: 13px;
  71. user-select: none;
  72. background-color: #fff;
  73. border: 1px solid #d9d9d9;
  74. position: relative;
  75. cursor: pointer;
  76. &:not(.disabled):hover {
  77. border-color: $themeColor;
  78. }
  79. &.disabled {
  80. background-color: #f5f5f5;
  81. border-color: #dcdcdc;
  82. color: #b7b7b7;
  83. cursor: default;
  84. }
  85. .selector {
  86. min-width: 50px;
  87. height: 30px;
  88. line-height: 30px;
  89. padding-left: 10px;
  90. @include ellipsis-oneline();
  91. }
  92. }
  93. .options {
  94. max-height: 260px;
  95. padding: 5px;
  96. overflow: auto;
  97. text-align: left;
  98. font-size: 13px;
  99. user-select: none;
  100. }
  101. .icon {
  102. width: 32px;
  103. height: 30px;
  104. color: #bfbfbf;
  105. position: absolute;
  106. top: 0;
  107. right: 0;
  108. display: flex;
  109. justify-content: center;
  110. align-items: center;
  111. }
  112. </style>