search.vue 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <template>
  2. <div class="search-box">
  3. <input
  4. @focus="onfocus"
  5. @keyup="choseList"
  6. v-model="searchVal"
  7. class="search"
  8. type="text"
  9. placeholder="搜索组件..."
  10. >
  11. <!-- <transition name="fade"> -->
  12. <ul class="search-list" v-if="searchList.length>0">
  13. <li
  14. :class="searchCurName==item.name?'cur':''"
  15. @click="checklist(item)"
  16. v-for="(item,index) in searchList"
  17. :key="index"
  18. >
  19. <router-link :to="{name:item.name}">
  20. {{item.name}}
  21. <span>{{item.chnName}}</span>
  22. </router-link>
  23. </li>
  24. </ul>
  25. <!-- </transition> -->
  26. </div>
  27. </template>
  28. <script>
  29. import { packages } from "../../src/config.json";
  30. export default {
  31. data() {
  32. return {
  33. packages,
  34. searchVal: "",
  35. searchList: [],
  36. searchCurName: "",
  37. searchIndex: 0
  38. };
  39. },
  40. watch: {
  41. searchVal(sVal) {
  42. if (sVal) {
  43. this.searchList = this.packages.filter(item => {
  44. if (item.showDemo === false) return false;
  45. const rx = new RegExp(sVal, "gi");
  46. return rx.test(item.name + " " + item.chnName + "" + item.desc);
  47. });
  48. } else {
  49. this.searchCurName = "";
  50. this.searchIndex = 0;
  51. this.searchList = [];
  52. }
  53. }
  54. },
  55. mounted() {
  56. document.documentElement.addEventListener("click", this.closelist);
  57. },
  58. methods: {
  59. closelist() {
  60. this.searchVal = "";
  61. this.searchCurName = "";
  62. this.searchIndex = 0;
  63. },
  64. onfocus(e) {
  65. e.target.select();
  66. },
  67. checklist() {
  68. this.searchVal = "";
  69. this.searchCurName = "";
  70. this.searchIndex = 0;
  71. },
  72. choseList(e) {
  73. let searchIndex = this.searchIndex;
  74. if (e.keyCode == 40) {
  75. searchIndex++;
  76. }
  77. if (e.keyCode == 38) {
  78. searchIndex--;
  79. }
  80. if (searchIndex < 0) {
  81. searchIndex = 0;
  82. }
  83. let searchList = this.searchList;
  84. if (searchList.length > 0) {
  85. let chnName = searchList[searchIndex] && searchList[searchIndex].name;
  86. if (chnName) {
  87. this.searchCurName = chnName;
  88. this.searchIndex = searchIndex;
  89. if (e.keyCode == 13) {
  90. this.$router.push({
  91. path: "/" + searchList[searchIndex].name
  92. });
  93. this.searchCurName = "";
  94. this.searchIndex = 0;
  95. this.searchVal = "";
  96. }
  97. }
  98. }
  99. }
  100. }
  101. };
  102. </script>
  103. <style lang="scss" scoped>
  104. .search-box {
  105. height: 22px;
  106. min-width: 300px;
  107. position: relative;
  108. input {
  109. width: 100%;
  110. }
  111. }
  112. .search {
  113. height: 22px;
  114. font-size: 14px;
  115. color: #666;
  116. border: none;
  117. background: url(./asset/css/i/sreach.png) no-repeat left center;
  118. padding-left: 45px;
  119. &:focus {
  120. outline: none;
  121. }
  122. }
  123. .search-list {
  124. background: #fff;
  125. position: absolute;
  126. width: 300px;
  127. list-style: none;
  128. border: 1px solid #f2f2f2;
  129. z-index: 99999;
  130. top: 27px;
  131. padding: 0;
  132. li {
  133. height: 40px;
  134. line-height: 40px;
  135. font-size: 12px;
  136. a {
  137. display: inline-block;
  138. box-sizing: border-box;
  139. width: 100%;
  140. padding-left: 40px;
  141. text-decoration: none;
  142. color: #666;
  143. }
  144. &:hover {
  145. background: #6096ff;
  146. color: #fff;
  147. a {
  148. color: #fff;
  149. }
  150. }
  151. }
  152. .cur {
  153. background: #6096ff;
  154. color: #fff;
  155. a {
  156. color: #fff;
  157. }
  158. }
  159. }
  160. </style>