mehaotian-search-revision.vue 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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}">
  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"
  9. :class="{'center':!active && mode === 2}" :focus="isFocus" v-model="inputVal" @focus="focus" @blur="blur" />
  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: true
  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. console.log('blur');
  82. this.isFocus = false;
  83. if (!this.inputVal) {
  84. this.active = false;
  85. }
  86. },
  87. clear() {
  88. //HM修改 收起键盘
  89. uni.hideKeyboard();
  90. this.isFocus = false;
  91. this.inputVal = '';
  92. this.active = false;
  93. //HM修改 清空内容时候触发组件input
  94. this.$emit('input', '');
  95. },
  96. getFocus() {
  97. if (!this.isFocus) {
  98. this.isFocus = true;
  99. }
  100. },
  101. search() {
  102. //HM修改 增加点击取消时候退出输入状态,内容为空时,输入默认关键字
  103. if (!this.inputVal) {
  104. if (!this.show && this.searchName == '取消') {
  105. uni.hideKeyboard();
  106. this.isFocus = false;
  107. this.active = false;
  108. return;
  109. }
  110. }
  111. console.log(this.inputVal);
  112. // this.$emit('search', this.inputVal ? this.inputVal : this.placeholder);
  113. }
  114. },
  115. watch: {
  116. inputVal(newVal) {
  117. if (newVal) {
  118. this.searchName = '搜索';
  119. } else {
  120. this.searchName = '取消';
  121. this.isDelShow = false;
  122. }
  123. },
  124. //HM修改 双向绑定
  125. value(val) {
  126. this.inputVal = val;
  127. },
  128. }
  129. };
  130. </script>
  131. <style lang="scss">
  132. .serach {
  133. display: flex;
  134. width: 100%;
  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. background: #fff;
  143. overflow: hidden;
  144. transition: all 0.2s linear;
  145. border-radius: 30px;
  146. .content-box {
  147. width: 100%;
  148. display: flex;
  149. align-items: center;
  150. &.center {
  151. justify-content: center;
  152. }
  153. .icon {
  154. padding: 0 15upx;
  155. &.icon-del {
  156. font-size: 38upx;
  157. &:before {
  158. content: "\e644";
  159. }
  160. }
  161. &.icon-serach:before {
  162. content: "\e61c";
  163. }
  164. }
  165. .input {
  166. width: 100%;
  167. max-width: 100%;
  168. line-height: 60upx;
  169. height: 60upx;
  170. transition: all 0.2s linear;
  171. &.center {
  172. width: 600upx;
  173. }
  174. &.sub {
  175. width: auto;
  176. color: grey;
  177. }
  178. }
  179. }
  180. .serachBtn {
  181. height: 100%;
  182. flex-shrink: 0;
  183. padding: 0 30upx;
  184. background: linear-gradient(to right, #72c172, #3bb197);
  185. line-height: 60upx;
  186. color: #fff;
  187. transition: all 0.3s;
  188. }
  189. }
  190. .button {
  191. display: flex;
  192. align-items: center;
  193. justify-content: center;
  194. position: relative;
  195. flex-shrink: 0;
  196. width: 0;
  197. transition: all 0.2s linear;
  198. white-space: nowrap;
  199. overflow: hidden;
  200. &.active {
  201. padding-left: 15upx;
  202. width: 100upx;
  203. }
  204. }
  205. }
  206. //HM修改 把字体改成本地加载
  207. @font-face {
  208. font-family: "iconfont";
  209. src: url('data:application/x-font-woff;charset=utf-8;base64,AAEAAAALAIAAAwAwR1NVQrD+s+0AAAE4AAAAQk9TLzI8fEg3AAABfAAAAFZjbWFws6gbWQAAAeQAAAGcZ2x5ZqgAaogAAAOMAAABMGhlYWQTyEk0AAAA4AAAADZoaGVhB90DhQAAALwAAAAkaG10eBAA//8AAAHUAAAAEGxvY2EA0gBOAAADgAAAAAptYXhwARIANgAAARgAAAAgbmFtZT5U/n0AAAS8AAACbXBvc3SanfjSAAAHLAAAAEUAAQAAA4D/gABcBAD//wAABAAAAQAAAAAAAAAAAAAAAAAAAAQAAQAAAAEAAL8Cm/NfDzz1AAsEAAAAAADYVQKbAAAAANhVApv///+ABAADgQAAAAgAAgAAAAAAAAABAAAABAAqAAQAAAAAAAIAAAAKAAoAAAD/AAAAAAAAAAEAA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on {
  212. font-family: iconfont;
  213. font-size: 32upx;
  214. font-style: normal;
  215. color: #999;
  216. }
  217. </style>