mehaotian-search-revision.vue 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. <template>
  2. <view class="serach">
  3. <view class="content" :style="{'border-radius':radius+'px'}">
  4. <!-- HM修改 增加进入输入状态的点击范围 -->
  5. <view class="content-box" @click="getFocus">
  6. <text class="icon icon-serach"></text>
  7. <!-- HM修改 增加placeholder input confirm-type confirm-->
  8. <input ref='myInput' :placeholder="placeholder" placeholder-style='font-size:28rpx' @input="inputChange"
  9. confirm-type="search" @confirm="triggerConfirm" class="input" focus v-model="inputVal"
  10. @blur="blur" />
  11. <!-- <view v-if="!active && mode === 2" class="input sub" @click="getFocus">请输入搜索内容</view> -->
  12. <!-- HM修改 @click换成@click.stop阻止冒泡 -->
  13. <text v-if="isDelShow" class="icon icon-del" @click.stop="clear"></text>
  14. </view>
  15. <!-- <view v-show="(active&&show&&button === 'inside')||(isDelShow && button === 'inside')" class="serachBtn"
  16. @click="search">
  17. 搜索
  18. </view> -->
  19. </view>
  20. <view v-if="button === 'outside'" class="button" :class="{'active':show||active}" @click="search">
  21. <view class="button-item">{{!show?searchName:'搜索'}}</view>
  22. </view>
  23. </view>
  24. </template>
  25. <script>
  26. export default {
  27. props: {
  28. mode: {
  29. value: Number,
  30. default: 1
  31. },
  32. //HM修改 定义默认搜索关键词(水印文字)
  33. placeholder: {
  34. value: String,
  35. default: '请输入搜索内容'
  36. },
  37. oldsearch:{
  38. type: String,
  39. default: ''
  40. },
  41. value: {
  42. type: String,
  43. default: false
  44. },
  45. button: {
  46. value: String,
  47. default: 'outside'
  48. },
  49. show: {
  50. value: Boolean,
  51. default: true
  52. },
  53. radius: {
  54. value: String,
  55. default: 60
  56. }
  57. },
  58. data() {
  59. return {
  60. active: false,
  61. inputVal: uni.getStorageSync('jiaoyikeyword')?uni.getStorageSync('jiaoyikeyword'):'',
  62. searchName: '取消',
  63. isDelShow: false,
  64. isFocus: false,
  65. setfocus:false
  66. };
  67. },
  68. watch:{
  69. inputVal:{
  70. handler(oldValue, newValue) {
  71. console.log(oldValue, newValue,11111)
  72. },
  73. deep:true,
  74. immediate:true,
  75. }
  76. },
  77. methods: {
  78. //HM修改 触发组件confirm事件
  79. triggerConfirm() {
  80. this.$emit('confirm', false);
  81. },
  82. //HM修改 触发组件input事件
  83. inputChange(event) {
  84. var keyword = event.detail.value;
  85. this.$emit('input', keyword);
  86. if (this.inputVal) {
  87. console.log(33333)
  88. this.isDelShow = true;
  89. }
  90. },
  91. focus() {
  92. console.log(222222)
  93. this.active = true;
  94. //HM修改 增加获取焦点判断
  95. if (this.inputVal) {
  96. this.isDelShow = true;
  97. }
  98. },
  99. blur() {
  100. this.isFocus = false;
  101. if (!this.inputVal) {
  102. this.active = false;
  103. }
  104. },
  105. clear() {
  106. //HM修改 收起键盘
  107. // uni.hideKeyboard();
  108. this.isFocus = false;
  109. this.inputVal = '';
  110. this.active = false;
  111. //HM修改 清空内容时候触发组件input
  112. this.$emit('input', '');
  113. //this.$emit('search', '');//HM修改 清空内容时候不进行搜索
  114. },
  115. getFocus() {
  116. this.isFocus = true;
  117. },
  118. setFocus() {
  119. // uni.hideKeyboard()
  120. this.$nextTick(() => {
  121. // this.isFocus = true;
  122. // console.log(this.$refs)
  123. // this.$refs.myInput.focus();
  124. // // 加这一步是为了对Input没有成功聚焦情况的补救(渲染先后的关系)
  125. })
  126. },
  127. search() {
  128. // console.log(11111)
  129. //HM修改 增加点击取消时候退出输入状态,内容为空时,输入默认关键字
  130. if (!this.inputVal) {
  131. if (!this.show && this.searchName == '取消') {
  132. // uni.hideKeyboard();
  133. this.isFocus = false;
  134. this.active = false;
  135. return;
  136. }
  137. }
  138. console.log(this.inputVal);
  139. this.$emit('search', this.inputVal ? this.inputVal : this.placeholder);
  140. }
  141. },
  142. watch: {
  143. inputVal(newVal) {
  144. if (newVal) {
  145. this.searchName = '搜索';
  146. //this.isDelShow = true; //HM修改 直接点击页面预设关键字样式异常,注销
  147. } else {
  148. this.searchName = '取消';
  149. this.isDelShow = false;
  150. }
  151. },
  152. //HM修改 双向绑定
  153. value(val) {
  154. this.inputVal = val;
  155. },
  156. }
  157. };
  158. </script>
  159. <style lang="scss" scoped>
  160. .serach {
  161. display: flex;
  162. width: 100%;
  163. //border-bottom: 1px #f5f5f5 solid; //HM修改 去掉边框
  164. box-sizing: border-box;
  165. font-size: $uni-font-size-base;
  166. .content {
  167. display: flex;
  168. align-items: center;
  169. width: 100%;
  170. height: 60upx;
  171. //border: 1px #ccc solid; //HM修改 去掉边框
  172. background: #fff;
  173. overflow: hidden;
  174. transition: all 0.2s linear;
  175. border-radius: 30px;
  176. .content-box {
  177. background: #F5F6F9;
  178. width: 100%;
  179. display: flex;
  180. align-items: center;
  181. &.center {
  182. justify-content: center;
  183. }
  184. .icon {
  185. padding: 0 15upx;
  186. &.icon-del {
  187. font-size: 30upx;
  188. &:before {
  189. content: "\e644";
  190. }
  191. }
  192. &.icon-serach:before {
  193. content: "\e61c";
  194. }
  195. }
  196. .input {
  197. width: 100%;
  198. max-width: 100%;
  199. line-height: 60upx;
  200. height: 60upx;
  201. transition: all 0.2s linear;
  202. &.center {
  203. width: 200upx;
  204. }
  205. &.sub {
  206. // position: absolute;
  207. width: auto;
  208. color: grey;
  209. }
  210. }
  211. }
  212. .serachBtn {
  213. height: 100%;
  214. flex-shrink: 0;
  215. padding: 0 30upx;
  216. //HM修改 按钮背景色
  217. background: linear-gradient(to right, #ff9801, #ff570a);
  218. //background: $uni-color-success;
  219. line-height: 60upx;
  220. color: #fff;
  221. //border-left: 1px #ccc solid; //HM修改 去掉边框
  222. transition: all 0.3s;
  223. }
  224. }
  225. .button {
  226. display: flex;
  227. align-items: center;
  228. justify-content: center;
  229. position: relative;
  230. flex-shrink: 0;
  231. width: 0;
  232. transition: all 0.2s linear;
  233. white-space: nowrap;
  234. overflow: hidden;
  235. &.active {
  236. padding-left: 15upx;
  237. width: 100upx;
  238. }
  239. }
  240. }
  241. //HM修改 把字体改成本地加载
  242. @font-face {
  243. font-family: "iconfont";
  244. src: url('data:application/x-font-woff;charset=utf-8;base64,AAEAAAALAIAAAwAwR1NVQrD+s+0AAAE4AAAAQk9TLzI8fEg3AAABfAAAAFZjbWFws6gbWQAAAeQAAAGcZ2x5ZqgAaogAAAOMAAABMGhlYWQTyEk0AAAA4AAAADZoaGVhB90DhQAAALwAAAAkaG10eBAA//8AAAHUAAAAEGxvY2EA0gBOAAADgAAAAAptYXhwARIANgAAARgAAAAgbmFtZT5U/n0AAAS8AAACbXBvc3SanfjSAAAHLAAAAEUAAQAAA4D/gABcBAD//wAABAAAAQAAAAAAAAAAAAAAAAAAAAQAAQAAAAEAAL8Cm/NfDzz1AAsEAAAAAADYVQKbAAAAANhVApv///+ABAADgQAAAAgAAgAAAAAAAAABAAAABAAqAAQAAAAAAAIAAAAKAAoAAAD/AAAAAAAAAAEAAAAKAB4ALAABREZMVAAIAAQAAAAAAAAAAQAAAAFsaWdhAAgAAAABAAAAAQAEAAQAAAABAAgAAQAGAAAAAQAAAAAAAQQAAZAABQAIAokCzAAAAI8CiQLMAAAB6wAyAQgAAAIABQMAAAAAAAAAAAAAAAAAAAAAAAAAAAAAUGZFZABA5gbmRAOA/4AAXAOBAIAAAAABAAAAAAAABAAAAAQA//8EAAAABAAAAAAAAAUAAAADAAAALAAAAAQAAAFoAAEAAAAAAGIAAwABAAAALAADAAoAAAFoAAQANgAAAAgACAACAADmBuYc5kT//wAA5gbmHOZE//8AAAAAAAAAAQAIAAgACAAAAAIAAQADAAABBgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA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AQQJAAsAJgFpCkNyZWF0ZWQgYnkgaWNvbmZvbnQKaWNvbmZvbnRSZWd1bGFyaWNvbmZvbnRpY29uZm9udFZlcnNpb24gMS4waWNvbmZvbnRHZW5lcmF0ZWQgYnkgc3ZnMnR0ZiBmcm9tIEZvbnRlbGxvIHByb2plY3QuaHR0cDovL2ZvbnRlbGxvLmNvbQAKAEMAcgBlAGEAdABlAGQAIABiAHkAIABpAGMAbwBuAGYAbwBuAHQACgBpAGMAbwBuAGYAbwBuAHQAUgBlAGcAdQBsAGEAcgBpAGMAbwBuAGYAbwBuAHQAaQBjAG8AbgBmAG8AbgB0AFYAZQByAHMAaQBvAG4AIAAxAC4AMABpAGMAbwBuAGYAbwBuAHQARwBlAG4AZQByAGEAdABlAGQAIABiAHkAIABzAHYAZwAyAHQAdABmACAAZgByAG8AbQAgAEYAbwBuAHQAZQBsAGwAbwAgAHAAcgBvAGoAZQBjAHQALgBoAHQAdABwADoALwAvAGYAbwBuAHQAZQBsAGwAbwAuAGMAbwBtAAAAAAIAAAAAAAAACgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABAECAQMBBAEFAAZzb3VzdW8IamlhbnRvdTQHc2hhbmNodQAAAAAA');
  245. }
  246. .icon {
  247. font-family: iconfont;
  248. font-size: 32upx;
  249. font-style: normal;
  250. color: #999;
  251. }
  252. </style>