Combox.vue 3.1 KB

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