cm-search.vue 3.5 KB

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