cm-search.vue 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <template>
  2. <view class="cm-search">
  3. <view class="search-control" @click.stop="handleClick">
  4. <view class="search-input">
  5. <text class="search-icon iconfont icon-sousuo"></text>
  6. <input
  7. type="text"
  8. confirm-type="search"
  9. :placeholder="placeholder"
  10. :value="value"
  11. @input="e => $emit('input', e.target.value)"
  12. @focus="$emit('focus')"
  13. @confirm="handleSearch"
  14. />
  15. <text v-if="clearable && value" class="clearable iconfont icon-quxiao" @click="clearValue"></text>
  16. </view>
  17. <view class="search-btn" @click="handleSearch(value)">搜索</view>
  18. </view>
  19. <template v-if="keywordVisible && keywordList.length > 0">
  20. <view class="hot-keyword">
  21. <view class="title">
  22. <text>搜索历史</text>
  23. <text class="clear iconfont icon-shanchu" @click="$emit('remove')"></text>
  24. </view>
  25. <view class="keyword-list">
  26. <view
  27. class="keyword"
  28. v-for="(keyword, index) in keywordList"
  29. v-text="keyword"
  30. :key="index"
  31. @click="handleSearch(keyword)"
  32. ></view>
  33. </view>
  34. </view>
  35. <view class="mask"></view>
  36. </template>
  37. </view>
  38. </template>
  39. <script>
  40. export default {
  41. name: 'cm-search',
  42. model: {
  43. prop: 'value',
  44. event: 'input'
  45. },
  46. props: {
  47. value: {
  48. typs: String,
  49. default: ''
  50. },
  51. // 占位字符
  52. placeholder: {
  53. type: String,
  54. default: '请输入搜索关键字'
  55. },
  56. clearable: {
  57. type: Boolean,
  58. default: true
  59. },
  60. disabled: {
  61. type: Boolean,
  62. default: false
  63. },
  64. keywordList: {
  65. type: Array,
  66. default: () => []
  67. },
  68. keywordVisible: {
  69. type: Boolean,
  70. default: true
  71. }
  72. },
  73. methods: {
  74. // 清空
  75. clearValue() {
  76. this.$emit('input', '')
  77. this.$emit('clear')
  78. },
  79. // 搜索
  80. handleClick() {
  81. if (!this.disabled) return
  82. this.$emit('goSearch')
  83. },
  84. // 关键词搜索
  85. handleSearch(keyword) {
  86. if (this.disabled) return
  87. this.$emit('input', keyword)
  88. this.$emit('search')
  89. }
  90. }
  91. }
  92. </script>
  93. <style lang="scss" scoped>
  94. .cm-search {
  95. position: relative;
  96. .hot-keyword {
  97. position: relative;
  98. z-index: 999;
  99. width: 100%;
  100. background: #fff;
  101. box-sizing: border-box;
  102. padding: 0 24rpx 32rpx;
  103. .title {
  104. position: relative;
  105. font-size: 26rpx;
  106. padding: 24rpx 0;
  107. color: #333;
  108. font-weight: bold;
  109. .clear {
  110. position: absolute;
  111. font-weight: normal;
  112. right: 0;
  113. top: 50%;
  114. transform: translateY(-50%);
  115. }
  116. }
  117. .keyword {
  118. display: inline-block;
  119. margin-right: 18rpx;
  120. font-size: 24rpx;
  121. line-height: 40rpx;
  122. padding: 0 16rpx;
  123. border-radius: 20rpx;
  124. background: #eee;
  125. color: #333;
  126. }
  127. }
  128. .mask {
  129. position: fixed;
  130. width: 100vw;
  131. height: 100vh;
  132. top: 0;
  133. left: 0;
  134. z-index: 998;
  135. background: #f7f7f7;
  136. }
  137. .search-control {
  138. position: relative;
  139. z-index: 999;
  140. border-bottom: 1rpx solid #f7f7f7;
  141. display: flex;
  142. justify-content: space-between;
  143. align-items: center;
  144. height: 90rpx;
  145. background: #fff;
  146. box-sizing: border-box;
  147. padding: 0 24rpx;
  148. .search-input {
  149. flex: 1;
  150. height: 60rpx;
  151. line-height: 60rpx;
  152. display: flex;
  153. align-items: center;
  154. position: relative;
  155. padding-left: 74rpx;
  156. font-size: 26rpx;
  157. border-radius: 30rpx;
  158. background: #f7f7f7;
  159. input {
  160. width: 100%;
  161. }
  162. .search-icon,
  163. .clearable {
  164. position: absolute;
  165. top: 50%;
  166. transform: translateY(-50%);
  167. z-index: 9;
  168. }
  169. .search-icon {
  170. left: 24rpx;
  171. }
  172. .clearable {
  173. right: 24rpx;
  174. color: #ccc;
  175. }
  176. }
  177. .search-btn {
  178. text-align: center;
  179. width: 60rpx;
  180. height: 60rpx;
  181. line-height: 60rpx;
  182. margin-left: 16rpx;
  183. font-size: 26rpx;
  184. }
  185. }
  186. }
  187. </style>