Search.vue 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <template>
  2. <div class="search-box">
  3. <div class="input-group">
  4. <input v-model="keyword" type="text" class="search-input form-control input-primary"
  5. placeholder="型号/物料名称/品牌"
  6. @focus.stop.prevent="onFocus()"
  7. @blur.stop.prevent="onBlur()"
  8. @keyup.40="onSelectChange(1)"
  9. @keyup.38="onSelectChange(-1)"
  10. @keyup.13="onSearch()"/>
  11. <span class="input-group-btn" @click="onSearch()">
  12. <button class="btn btn-primary search-btn" type="button">搜&nbsp;索</button>
  13. </span>
  14. </div>
  15. <ul class="association" v-show="showAssociate">
  16. <li v-for="(k, index) in similarKeywords.data" class="item"
  17. :class="{'active': index==associate.activeIndex}"
  18. @click.stop.prevent="onAssociateClick(k)">{{ k }}
  19. </li>
  20. </ul>
  21. <div class="search-hot">
  22. <ul class="list-untyled">
  23. <li class="item item-first">热门搜索</li>
  24. <li class="item" v-for="w in hotwords">
  25. <nuxt-link :to="w.url" target="_blank">{{ w.name }}</nuxt-link>
  26. </li>
  27. </ul>
  28. </div>
  29. </div>
  30. </template>
  31. <script>
  32. export default {
  33. name: 'search-box',
  34. data () {
  35. return {
  36. keyword: '',
  37. associate: {
  38. show: false,
  39. activeIndex: null
  40. }
  41. }
  42. },
  43. computed: {
  44. similarKeywords () {
  45. return this.$store.state.search.keywords
  46. },
  47. showAssociate () {
  48. return this.associate.show && this.similarKeywords.data && this.similarKeywords.data.length
  49. }
  50. },
  51. props: {
  52. hotwords: {
  53. type: Array,
  54. default () {
  55. return [{
  56. name: 'SCT2080KEC',
  57. url: '/product/component/1100400300009990'
  58. }, {
  59. name: '电池组',
  60. url: '/product/kinds/346'
  61. }, {
  62. name: 'Vishay',
  63. url: '/product/brand/30327265e42a871be050007f01003d96'
  64. }, {
  65. name: 'Panasonic Battery',
  66. url: '/product/brand/30327265e4e7871be050007f01003d96'
  67. }]
  68. }
  69. }
  70. },
  71. watch: {
  72. 'keyword': {
  73. handler (val, oldVal) {
  74. let keywords = this.similarKeywords.data
  75. if (!keywords || !keywords.length || this.associate.activeIndex === null || val !== keywords[this.associate.activeIndex]) {
  76. this.onChange()
  77. }
  78. }
  79. }
  80. },
  81. methods: {
  82. onFocus () {
  83. this.associate.show = true
  84. },
  85. onBlur () {
  86. this.associate.show = false
  87. },
  88. onSelectChange (count) {
  89. let keywords = this.similarKeywords.data
  90. if (keywords && keywords.length) {
  91. let index = this.associate.activeIndex
  92. if (index === null) {
  93. index = -1
  94. }
  95. index += count
  96. if (index >= keywords.length) {
  97. index = 0
  98. } else if (index < 0) {
  99. index = keywords.length - 1
  100. }
  101. this.associate.activeIndex = index
  102. this.keyword = keywords[index]
  103. }
  104. },
  105. onChange () {
  106. this.associate.activeIndex = null
  107. if (!this.keyword) {
  108. this.associate.show = false
  109. this.$store.dispatch('resetSearchKeywords')
  110. } else {
  111. this.searchKeywords()
  112. }
  113. },
  114. searchKeywords () {
  115. this.associate.show = true
  116. this.$store.dispatch('searchKeywords', { keyword: this.keyword })
  117. },
  118. onSearch () {
  119. if (this.keyword) {
  120. this.associate.show = false
  121. this.$store.dispatch('resetSearchKeywords')
  122. this.$router.push({path: '/search?w=' + encodeURIComponent(this.keyword)})
  123. }
  124. },
  125. onAssociateClick (word) {
  126. this.keyword = word
  127. this.onSearch()
  128. }
  129. }
  130. }
  131. </script>
  132. <style lang="scss" scoped>
  133. @import '~assets/scss/variables';
  134. .search-box {
  135. width: 470px;
  136. height: 40px;
  137. position: relative;
  138. .search-input, .search-btn {
  139. height: 40px;
  140. border-width: 2px;
  141. }
  142. .search-btn {
  143. font-size: 16px;
  144. width: 78px;
  145. }
  146. .search-hot {
  147. .item {
  148. display: inline-block;
  149. font-size: $font-size-small;
  150. margin-right: $pad;
  151. &.item-first {
  152. color: $red;
  153. }
  154. }
  155. }
  156. .association {
  157. position: absolute;
  158. left: 0;
  159. top: 100%;
  160. right: 79px;
  161. background: $white;
  162. border: $border;
  163. border-top-width: 0;
  164. z-index: 21;
  165. .item {
  166. padding: 0 15px;
  167. line-height: 30px;
  168. cursor: pointer;
  169. &.active {
  170. background-color: $dark-bg;
  171. }
  172. &:hover {
  173. background-color: $grey-bg;
  174. }
  175. }
  176. }
  177. }
  178. </style>