Search.vue 4.9 KB

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