MapSearchBox.vue 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. <template>
  2. <div class="map-search-box" @click.stop>
  3. <div class="search-input-wrap" :class="{ 'is-open': open && hasResults }">
  4. <i class="search-icon"></i>
  5. <input
  6. ref="input"
  7. v-model="query"
  8. :placeholder="placeholder"
  9. class="search-input"
  10. @focus="open = true"
  11. @keydown.down.prevent="moveSelection(1)"
  12. @keydown.up.prevent="moveSelection(-1)"
  13. @keydown.enter.prevent="selectActive"
  14. @keydown.esc="handleEsc"
  15. @compositionstart="composing = true"
  16. @compositionend="onCompositionEnd"
  17. />
  18. <i v-if="query" class="clear-icon" @click="clear" title="清空">✕</i>
  19. </div>
  20. <div v-if="open && query && !composing" class="search-dropdown">
  21. <div v-if="filtered.length === 0" class="dropdown-empty">无匹配路口</div>
  22. <div
  23. v-for="(item, idx) in filtered"
  24. :key="item[idField]"
  25. class="dropdown-item"
  26. :class="{ 'is-active': idx === activeIdx }"
  27. @mouseenter="activeIdx = idx"
  28. @mousedown.prevent="selectItem(item)"
  29. >
  30. <div class="item-name">{{ item[nameField] }}</div>
  31. <div class="item-id">{{ item[idField] }}</div>
  32. </div>
  33. </div>
  34. </div>
  35. </template>
  36. <script>
  37. export default {
  38. name: 'MapSearchBox',
  39. props: {
  40. dataSource: { type: Array, required: true },
  41. placeholder: { type: String, default: '搜路口' },
  42. maxResults: { type: Number, default: 10 },
  43. searchFields: { type: Array, default: () => ['路口名称', '路口编号'] },
  44. nameField: { type: String, default: '路口名称' },
  45. idField: { type: String, default: '路口编号' },
  46. },
  47. data() {
  48. return {
  49. query: '',
  50. open: false,
  51. activeIdx: 0,
  52. composing: false,
  53. };
  54. },
  55. computed: {
  56. filtered() {
  57. const q = this.query.trim().toLowerCase();
  58. if (!q) return [];
  59. const out = [];
  60. const fields = this.searchFields;
  61. for (const item of this.dataSource) {
  62. for (const f of fields) {
  63. const v = item[f];
  64. if (v != null && String(v).toLowerCase().includes(q)) {
  65. out.push(item);
  66. break;
  67. }
  68. }
  69. if (out.length >= this.maxResults) break;
  70. }
  71. return out;
  72. },
  73. hasResults() {
  74. return this.query && !this.composing;
  75. },
  76. },
  77. watch: {
  78. filtered() {
  79. this.activeIdx = 0;
  80. },
  81. },
  82. mounted() {
  83. this._outsideHandler = (e) => {
  84. if (!this.$el.contains(e.target)) this.open = false;
  85. };
  86. document.addEventListener('click', this._outsideHandler);
  87. },
  88. beforeDestroy() {
  89. if (this._outsideHandler) {
  90. document.removeEventListener('click', this._outsideHandler);
  91. this._outsideHandler = null;
  92. }
  93. },
  94. methods: {
  95. moveSelection(delta) {
  96. const len = this.filtered.length;
  97. if (len === 0) return;
  98. this.activeIdx = (this.activeIdx + delta + len) % len;
  99. },
  100. selectActive() {
  101. const item = this.filtered[this.activeIdx];
  102. if (item) this.selectItem(item);
  103. },
  104. selectItem(item) {
  105. this.$emit('select', item);
  106. this.open = false;
  107. this.query = '';
  108. this.$refs.input && this.$refs.input.blur();
  109. },
  110. handleEsc() {
  111. if (this.open) {
  112. this.open = false;
  113. } else {
  114. this.clear();
  115. }
  116. },
  117. clear() {
  118. this.query = '';
  119. this.activeIdx = 0;
  120. this.$refs.input && this.$refs.input.focus();
  121. },
  122. onCompositionEnd() {
  123. this.composing = false;
  124. },
  125. },
  126. };
  127. </script>
  128. <style scoped>
  129. .map-search-box {
  130. width: 300px;
  131. font-family: inherit;
  132. }
  133. .search-input-wrap {
  134. position: relative;
  135. display: flex;
  136. align-items: center;
  137. height: 38px;
  138. background: rgba(5, 22, 45, 0.9);
  139. border: 1px solid #1e4d8e;
  140. border-radius: 7px;
  141. backdrop-filter: blur(8px);
  142. box-shadow: 0 4px 20px rgba(0, 0, 0, 0.4);
  143. transition: border-color 0.15s;
  144. }
  145. .search-input-wrap:focus-within,
  146. .search-input-wrap.is-open {
  147. border-color: #3a7fd1;
  148. }
  149. .search-icon {
  150. width: 14px;
  151. height: 14px;
  152. margin: 0 8px 0 12px;
  153. border: 1.5px solid rgba(255, 255, 255, 0.5);
  154. border-radius: 50%;
  155. position: relative;
  156. flex-shrink: 0;
  157. }
  158. .search-icon::after {
  159. content: '';
  160. position: absolute;
  161. width: 1.5px;
  162. height: 6px;
  163. background: rgba(255, 255, 255, 0.5);
  164. right: -3px;
  165. bottom: -3px;
  166. transform: rotate(-45deg);
  167. }
  168. .search-input {
  169. flex: 1;
  170. height: 100%;
  171. line-height: 38px;
  172. background: transparent;
  173. border: none;
  174. outline: none;
  175. color: #fff;
  176. font-size: 13px;
  177. padding: 0;
  178. min-width: 0;
  179. }
  180. .search-input::placeholder {
  181. color: rgba(255, 255, 255, 0.4);
  182. }
  183. .clear-icon {
  184. width: 18px;
  185. height: 18px;
  186. line-height: 18px;
  187. text-align: center;
  188. margin: 0 8px;
  189. color: rgba(255, 255, 255, 0.5);
  190. cursor: pointer;
  191. font-size: 12px;
  192. font-style: normal;
  193. border-radius: 50%;
  194. flex-shrink: 0;
  195. user-select: none;
  196. }
  197. .clear-icon:hover {
  198. color: #fff;
  199. background: rgba(255, 255, 255, 0.1);
  200. }
  201. .search-dropdown {
  202. margin-top: 6px;
  203. max-height: 320px;
  204. overflow-y: auto;
  205. background: rgba(5, 22, 45, 0.92);
  206. border: 1px solid #1e4d8e;
  207. border-radius: 7px;
  208. backdrop-filter: blur(8px);
  209. box-shadow: 0 4px 20px rgba(0, 0, 0, 0.4);
  210. }
  211. .dropdown-empty {
  212. padding: 14px 12px;
  213. text-align: center;
  214. color: rgba(255, 255, 255, 0.4);
  215. font-size: 12px;
  216. }
  217. .dropdown-item {
  218. padding: 8px 12px;
  219. cursor: pointer;
  220. border-bottom: 1px solid rgba(30, 77, 142, 0.25);
  221. transition: background 0.12s;
  222. }
  223. .dropdown-item:last-child {
  224. border-bottom: none;
  225. }
  226. .dropdown-item.is-active,
  227. .dropdown-item:hover {
  228. background: rgba(30, 77, 142, 0.4);
  229. }
  230. .item-name {
  231. color: #fff;
  232. font-size: 13px;
  233. line-height: 1.4;
  234. white-space: nowrap;
  235. overflow: hidden;
  236. text-overflow: ellipsis;
  237. }
  238. .item-id {
  239. margin-top: 2px;
  240. color: rgba(120, 200, 255, 0.6);
  241. font-size: 11px;
  242. font-family: 'Consolas', monospace;
  243. }
  244. .search-dropdown::-webkit-scrollbar {
  245. width: 6px;
  246. }
  247. .search-dropdown::-webkit-scrollbar-thumb {
  248. background: rgba(30, 77, 142, 0.6);
  249. border-radius: 3px;
  250. }
  251. .search-dropdown::-webkit-scrollbar-track {
  252. background: transparent;
  253. }
  254. </style>