Combox.vue 2.5 KB

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