rf-search.vue 5.0 KB

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