Search.vue 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. console.log('focus')
  87. this.associate.show = true
  88. },
  89. onBlur () {
  90. console.log('blur')
  91. this.associate.show = this.associate.focus
  92. },
  93. onSelectChange (count) {
  94. console.log('selectchange')
  95. let keywords = this.similarKeywords.data
  96. if (keywords && keywords.length) {
  97. let index = this.associate.activeIndex
  98. if (index === null) {
  99. index = -1
  100. }
  101. index += count
  102. if (index >= keywords.length) {
  103. index = 0
  104. } else if (index < 0) {
  105. index = keywords.length - 1
  106. }
  107. this.associate.activeIndex = index
  108. this.keyword = keywords[index]
  109. }
  110. },
  111. onChange () {
  112. console.log('change')
  113. this.associate.activeIndex = null
  114. if (!this.keyword) {
  115. this.associate.show = false
  116. this.$store.dispatch('resetSearchKeywords')
  117. } else {
  118. this.searchKeywords()
  119. }
  120. if (this.click_flag) {
  121. this.associate.show = false
  122. this.click_flag = false
  123. }
  124. },
  125. searchKeywords () {
  126. console.log('searchkeywords')
  127. this.associate.show = true
  128. this.$store.dispatch('searchKeywords', { keyword: this.keyword })
  129. },
  130. onSearch () {
  131. console.log('search')
  132. if (this.keyword) {
  133. this.associate.show = false
  134. this.$store.dispatch('resetSearchKeywords')
  135. this.$router.push({path: '/search?w=' + encodeURIComponent(this.keyword)})
  136. }
  137. },
  138. onAssociateClick (word) {
  139. console.log('assclick')
  140. this.click_flag = true
  141. this.keyword = word
  142. this.onSearch()
  143. }
  144. },
  145. created () {
  146. this.$store.dispatch('resetSearchKeywords')
  147. }
  148. }
  149. </script>
  150. <style lang="scss" scoped>
  151. @import '~assets/scss/variables';
  152. .form-control{
  153. border-radius: 0;
  154. }
  155. .search-box {
  156. width: 470px;
  157. height: 40px;
  158. position: relative;
  159. .search-input, .search-btn {
  160. height: 40px;
  161. border-width: 2px;
  162. }
  163. .search-btn {
  164. font-size: 16px;
  165. width: 78px;
  166. border-radius: 0;
  167. }
  168. .search-hot ul li a{
  169. color: #838383;
  170. }
  171. .search-hot {
  172. .item {
  173. display: inline-block;
  174. font-size: $font-size-small;
  175. margin-right: $pad;
  176. &.item-first {
  177. color: $red;
  178. }
  179. }
  180. }
  181. .association {
  182. position: absolute;
  183. left: 0;
  184. top: 100%;
  185. right: 79px;
  186. background: $white;
  187. border: $border;
  188. border-top-width: 0;
  189. z-index: 21;
  190. .item {
  191. padding: 0 15px;
  192. line-height: 30px;
  193. cursor: pointer;
  194. &.active {
  195. background-color: $dark-bg;
  196. }
  197. &:hover {
  198. background-color: $grey-bg;
  199. }
  200. }
  201. }
  202. }
  203. </style>