cm-search.vue 4.3 KB

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