search.vue 2.9 KB

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