Combox.vue 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <template>
  2. <div class="com-combox" :class="{show:showPopup}" @click="switchShowHide" v-clickoutside="hide">
  3. <div class="input">
  4. <p v-if="current">{{current.name}}</p>
  5. </div>
  6. <a class="iconfont icon-material_preview_upload_collect"></a>
  7. <ul ref="ul"
  8. :style="{
  9. top: needTranslateUp ? '' : '100%',
  10. bottom: needTranslateUp ? '100%' : '',
  11. }"
  12. >
  13. <li
  14. v-for="(item,key) in data"
  15. :key="key"
  16. @click="onClick(item)"
  17. :class="{active:selectedId == item.id}"
  18. >
  19. <slot :item="item">
  20. <template>
  21. <span>{{item.name}}</span>
  22. </template>
  23. </slot>
  24. </li>
  25. </ul>
  26. </div>
  27. </template>
  28. <script>
  29. export default {
  30. props: {
  31. data: Array,
  32. type: {
  33. type: String,
  34. default: "object"
  35. },
  36. selectedId: {
  37. type: String,
  38. default:''
  39. },
  40. bottomSpace: {
  41. type: Number,
  42. default: Infinity,
  43. },
  44. },
  45. data() {
  46. return {
  47. showPopup: false,
  48. needTranslateUp: false,
  49. };
  50. },
  51. computed: {
  52. current() {
  53. let tmp = this.data.find(item=>{
  54. return this.selectedId == item.id
  55. })
  56. return tmp
  57. }
  58. },
  59. methods: {
  60. switchShowHide() {
  61. if (this.showPopup === true) {
  62. this.hide()
  63. } else {
  64. this.showPopup = true;
  65. this.$nextTick(() => {
  66. const boundingRectBottom = this.$refs.ul.getBoundingClientRect().bottom
  67. if (boundingRectBottom > this.bottomSpace) {
  68. this.needTranslateUp = true
  69. } else {
  70. this.needTranslateUp = false
  71. }
  72. })
  73. }
  74. },
  75. hide() {
  76. this.showPopup = false;
  77. this.needTranslateUp = false
  78. },
  79. onClick(item) {
  80. if (this.selectedId == item.id) {
  81. return this.$emit("click", item);
  82. }
  83. this.$emit("change", item);
  84. }
  85. }
  86. };
  87. </script>
  88. <style lang="less" scoped>
  89. .com-combox {
  90. position: relative;
  91. border: 1px solid rgba(151, 151, 151, 0.2);
  92. padding: 0 16px;
  93. background: #1A1B1D;
  94. border-radius: 2px;
  95. height: 36px;
  96. cursor: pointer;
  97. &.show {
  98. ul {
  99. display: block;
  100. }
  101. a {
  102. transform: translateY(-50%) rotate(180deg);
  103. }
  104. }
  105. > .input {
  106. height: 100%;
  107. display: flex;
  108. align-items: center;
  109. .p {
  110. width: 100%;
  111. padding-right: 50px;
  112. overflow: hidden;
  113. white-space: nowrap;
  114. text-overflow: ellipsis;
  115. text-align: left;
  116. }
  117. }
  118. ul {
  119. display: none;
  120. padding: 0;
  121. position: absolute;
  122. left: -1px;
  123. right: -1px;
  124. background: #1A1B1D;
  125. border-radius: 0px 0px 2px 2px;
  126. border: 1px solid #404040;
  127. overflow-x: hidden;
  128. overflow-y: auto;
  129. z-index: 100;
  130. li {
  131. height: 36px;
  132. padding: 0 16px;
  133. display: flex;
  134. align-items: center;
  135. &.active,
  136. &:hover {
  137. background: #252526;
  138. color: @color;
  139. }
  140. }
  141. }
  142. a {
  143. position: absolute;
  144. right: 16px;
  145. top: 50%;
  146. font-size: 12px;
  147. transform: translateY(-50%);
  148. }
  149. }
  150. </style>