mehaotian-search-revision.vue 7.5 KB

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