cm-search.vue 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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: '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. watch: {
  74. inputValue(nVal) {
  75. this.$emit('input', nVal)
  76. }
  77. },
  78. data() {
  79. return {
  80. inputValue: ''
  81. }
  82. },
  83. created() {
  84. this.inputValue = this.value
  85. },
  86. methods: {
  87. clearValue() {
  88. this.inputValue = ''
  89. this.$emit('clear')
  90. },
  91. handleClick() {
  92. if (!this.disabled) return
  93. this.$emit('goSearch')
  94. },
  95. handleSearch(keyword) {
  96. if (this.disabled) return
  97. this.inputValue = keyword
  98. this.$emit('search')
  99. }
  100. }
  101. }
  102. </script>
  103. <style lang="scss" scoped>
  104. .cm-search {
  105. position: relative;
  106. .hot-keyword {
  107. position: relative;
  108. z-index: 999;
  109. width: 100%;
  110. background: #fff;
  111. box-sizing: border-box;
  112. padding: 0 24rpx 32rpx;
  113. .title {
  114. position: relative;
  115. font-size: 26rpx;
  116. padding: 24rpx 0;
  117. color: #333;
  118. font-weight: bold;
  119. .clear {
  120. position: absolute;
  121. font-weight: normal;
  122. right: 0;
  123. top: 50%;
  124. transform: translateY(-50%);
  125. }
  126. }
  127. .keyword {
  128. display: inline-block;
  129. margin-right: 18rpx;
  130. font-size: 24rpx;
  131. line-height: 40rpx;
  132. padding: 0 16rpx;
  133. border-radius: 20rpx;
  134. background: #eee;
  135. color: #333;
  136. }
  137. }
  138. .mask {
  139. position: fixed;
  140. width: 100vw;
  141. height: 100vh;
  142. top: 0;
  143. left: 0;
  144. z-index: 998;
  145. background: #f7f7f7;
  146. }
  147. .search-control {
  148. position: relative;
  149. z-index: 999;
  150. border-bottom: 1rpx solid #f7f7f7;
  151. display: flex;
  152. justify-content: space-between;
  153. align-items: center;
  154. height: 90rpx;
  155. background: #fff;
  156. box-sizing: border-box;
  157. padding: 0 24rpx;
  158. .search-input {
  159. flex: 1;
  160. height: 60rpx;
  161. line-height: 60rpx;
  162. display: flex;
  163. align-items: center;
  164. position: relative;
  165. padding-left: 74rpx;
  166. font-size: 26rpx;
  167. border-radius: 30rpx;
  168. background: #f7f7f7;
  169. input {
  170. width: 100%;
  171. }
  172. .search-icon,
  173. .clearable {
  174. position: absolute;
  175. top: 50%;
  176. transform: translateY(-50%);
  177. z-index: 9;
  178. }
  179. .search-icon {
  180. left: 24rpx;
  181. }
  182. .clearable {
  183. right: 24rpx;
  184. color: #ccc;
  185. }
  186. }
  187. .search-btn {
  188. text-align: center;
  189. width: 60rpx;
  190. height: 60rpx;
  191. line-height: 60rpx;
  192. margin-left: 16rpx;
  193. font-size: 26rpx;
  194. }
  195. }
  196. }
  197. </style>