Search.vue 5.2 KB

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