cm-search.vue 4.4 KB

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