SelectList.vue 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. <template>
  2. <div class="control" @click="onInputerClick">
  3. <div class="component select" v-click-outside="onOutside">
  4. <div class="place" :class="{ placeholder: !modelValue.length }">
  5. <ul v-if="modelValue.length">
  6. <li v-for="item in modelValue" @click.stop>
  7. <span>{{ item.text || $t('tag.unkownUser') }}</span
  8. ><i class="iconfont icon-close" @click="onselecterChange(item)"></i>
  9. </li>
  10. </ul>
  11. <div v-else>{{ placeholder }}</div>
  12. </div>
  13. <div class="icon" :class="{ up: selecterShow }">
  14. <i class="iconfont icon-arrows_down"></i>
  15. </div>
  16. <div class="panel" v-show="selecterShow">
  17. <ul>
  18. <li v-for="item in data" @click.stop="onselecterChange(item)">
  19. <div>
  20. <span class="checkbox" :class="{ checked: modelValue.includes(item) }"></span><span class="name">{{ item.text || $t('tag.unkownUser') }}</span>
  21. </div>
  22. </li>
  23. </ul>
  24. </div>
  25. </div>
  26. </div>
  27. </template>
  28. <script setup>
  29. import { ref } from 'vue'
  30. const props = defineProps({
  31. data: {
  32. type: Array,
  33. default: [],
  34. },
  35. modelValue: {
  36. type: Array,
  37. require: true,
  38. },
  39. placeholder: {
  40. type: String,
  41. require: false,
  42. default: '请输入',
  43. },
  44. })
  45. const emits = defineEmits(['change'])
  46. const selecterShow = ref(false)
  47. const onInputerClick = () => {
  48. selecterShow.value = !selecterShow.value
  49. }
  50. const onselecterChange = data => {
  51. let index = props.modelValue.findIndex(item => item.value == data.value)
  52. if (index == -1) {
  53. props.modelValue.push(data)
  54. } else {
  55. props.modelValue.splice(index, 1)
  56. }
  57. emits('change', props.modelValue)
  58. }
  59. const onOutside = () => {
  60. selecterShow.value = false
  61. }
  62. </script>
  63. <style lang="scss" scoped>
  64. ul,
  65. li {
  66. list-style: none;
  67. margin: 0;
  68. padding: 0;
  69. }
  70. .control {
  71. background: rgba(255, 255, 255, 0.1);
  72. border-radius: 4px 4px 4px 4px;
  73. border: 1px solid rgba(255, 255, 255, 0.2);
  74. .component {
  75. position: relative;
  76. width: 100%;
  77. display: flex;
  78. align-items: center;
  79. }
  80. .select {
  81. .place {
  82. display: flex;
  83. width: 100%;
  84. min-height: 66px;
  85. color: #fff;
  86. align-items: flex-start;
  87. &.placeholder {
  88. color: #757575;
  89. align-items: center;
  90. justify-content: center;
  91. }
  92. ul {
  93. margin-bottom: 6px;
  94. }
  95. li {
  96. display: inline-block;
  97. background-color: #767473;
  98. border-radius: 4px;
  99. padding: 4px 6px;
  100. margin-top: 6px;
  101. margin-left: 6px;
  102. i {
  103. font-size: 12px;
  104. margin-left: 7px;
  105. cursor: pointer;
  106. }
  107. }
  108. }
  109. .panel {
  110. position: absolute;
  111. left: -1px;
  112. right: -1px;
  113. top: calc(100% + 4px);
  114. background: rgba(27, 27, 28, 0.9);
  115. // background: rgb(57, 57, 60, 0.9);
  116. box-shadow: 0px 4px 4px 0px rgba(0, 0, 0, 0.25);
  117. border-radius: 4px 4px 4px 4px;
  118. border: 1px solid #000000;
  119. max-height: 250px;
  120. overflow: hidden;
  121. overflow-y: auto;
  122. z-index: 1000;
  123. li {
  124. cursor: pointer;
  125. height: 34px;
  126. display: flex;
  127. align-items: center;
  128. &:hover {
  129. background: rgba(255, 255, 255, 0.1);
  130. }
  131. > div {
  132. display: flex;
  133. align-items: center;
  134. color: rgba(255, 255, 255, 0.6);
  135. width: 100%;
  136. .name {
  137. width: 80%;
  138. overflow: hidden;
  139. text-overflow: ellipsis;
  140. white-space: nowrap;
  141. }
  142. }
  143. }
  144. }
  145. .icon {
  146. cursor: pointer;
  147. margin-right: 10px;
  148. &.up {
  149. transform: rotate(180deg);
  150. }
  151. i {
  152. font-size: 12px;
  153. color: #999;
  154. }
  155. }
  156. }
  157. }
  158. .checkbox {
  159. position: relative;
  160. width: 16px;
  161. height: 16px;
  162. margin-right: 5px;
  163. margin-left: 10px;
  164. &::before {
  165. content: '';
  166. border: 1px solid #666;
  167. border-radius: 2px;
  168. width: 14px;
  169. height: 14px;
  170. position: absolute;
  171. left: 0px;
  172. top: 0;
  173. display: inline-block;
  174. }
  175. &.checked {
  176. &::before {
  177. border: 1px solid #0076f6;
  178. background-color: #0076f6;
  179. }
  180. &::after {
  181. left: 3px;
  182. top: 7px;
  183. position: absolute;
  184. display: table;
  185. border: 2px solid #fff;
  186. border-top: 0;
  187. border-left: 0;
  188. transform: rotate(45deg) translate(-50%, -50%);
  189. opacity: 1;
  190. transition: all 0.2s cubic-bezier(0.12, 0.4, 0.29, 1.46) 0.1s;
  191. width: 6px;
  192. height: 8px;
  193. content: ' ';
  194. }
  195. }
  196. }
  197. .checkbox-label {
  198. display: inline-block;
  199. vertical-align: 3px;
  200. }
  201. </style>