m-search-revision.vue 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. <template>
  2. <view class="serach">
  3. <view class="left-box" @tap="onClickLeft">
  4. <uni-icons style="line-height:70rpx" type="back" size="24" />
  5. </view>
  6. <view class="content" :style="{ 'border-radius': radius + 'px' }">
  7. <!-- HM修改 增加进入输入状态的点击范围 -->
  8. <view class="content-box" :class="{ center: mode === 2 }">
  9. <u-icon name="search" size="32" style="padding:0 15rpx;"></u-icon>
  10. <!-- HM修改 增加placeholder input confirm-type confirm-->
  11. <input style="width:100%; " :placeholder="placeholder" placeholder-class="placeholder-color" @input="inputChange" confirm-type="search" @confirm="triggerConfirm" class="input"
  12. :class="{ center: !active && mode === 2 }" :focus="isFocus" v-model="inputVal" @focus="focus" @blur="blur" />
  13. <u-icon name="close" v-if="isDelShow" style="padding:0 30rpx;" @click="clear"></u-icon>
  14. </view>
  15. <view v-show="(active && show && button === 'inside') || (isDelShow && button === 'inside')" class="serachBtn" @click="search">搜索</view>
  16. </view>
  17. <view v-if="button === 'outside'" class="button" :class="{ active: show || active }">
  18. <view v-if="isShowSeachGoods !=true" class="button-item">
  19. <div @click="out()">取消</div>
  20. </view>
  21. <view v-else class="button-item">
  22. <u-icon name="grid-fill" size="50" @click="handelListClass()" v-if="!switchLayout"></u-icon>
  23. <u-icon v-else @click="handelListClass()" name="list-dot" size="50"></u-icon>
  24. </view>
  25. </view>
  26. </view>
  27. </template>
  28. <script>
  29. import uniStatusBar from "../uni-status-bar/uni-status-bar.vue";
  30. import uniIcons from "../uni-icons/uni-icons.vue";
  31. export default {
  32. components: {
  33. uniStatusBar,
  34. uniIcons,
  35. },
  36. props: {
  37. mode: {
  38. value: Number,
  39. default: 1,
  40. },
  41. //HM修改 定义默认搜索关键词(水印文字)
  42. placeholder: {
  43. value: String,
  44. default: "请输入搜索内容",
  45. },
  46. value: {
  47. type: String,
  48. default: "",
  49. },
  50. button: {
  51. value: String,
  52. default: "outside",
  53. },
  54. //
  55. show: {
  56. value: Boolean,
  57. default: true,
  58. },
  59. // 默认半径为60
  60. radius: {
  61. value: String,
  62. default: 60,
  63. },
  64. // 是否获取焦点
  65. isFocusVal: {
  66. value: Boolean,
  67. default: true,
  68. },
  69. },
  70. data() {
  71. return {
  72. isShowSeachGoods: false, //是否显示查询的商品
  73. active: false, //是否选中
  74. inputVal: "", //Input中内容
  75. isDelShow: false, //是否显示右侧删除icon
  76. isFocus: false, //是否获取焦点
  77. switchLayout: true, //切换当前商品的布局,默认为两列
  78. };
  79. },
  80. mounted() {
  81. this.isFocus = this.isFocusVal;
  82. },
  83. methods: {
  84. //
  85. out() {
  86. uni.reLaunch({
  87. url: "/pages/tabbar/home/index",
  88. });
  89. },
  90. // 切换排列顺序
  91. handelListClass() {
  92. this.switchLayout = !this.switchLayout;
  93. this.$emit("SwitchType");
  94. },
  95. //HM修改 触发组件confirm事件
  96. triggerConfirm() {
  97. this.$emit("confirm", false);
  98. uni.hideKeyboard();
  99. },
  100. //HM修改 触发组件input事件
  101. inputChange(event) {
  102. var keyword = event.detail.value;
  103. this.$emit("input", keyword);
  104. if (this.inputVal) {
  105. this.isDelShow = true;
  106. }
  107. },
  108. focus() {
  109. this.active = true;
  110. //HM修改 增加获取焦点判断
  111. if (this.inputVal) {
  112. this.isDelShow = true;
  113. }
  114. },
  115. blur() {
  116. this.isFocus = false;
  117. if (!this.inputVal) {
  118. this.active = false;
  119. }
  120. },
  121. clear() {
  122. //HM修改 收起键盘
  123. uni.hideKeyboard();
  124. this.isFocus = false;
  125. this.inputVal = "";
  126. this.active = false;
  127. //HM修改 清空内容时候触发组件input
  128. this.$emit("input", "");
  129. //this.$emit('search', '');//HM修改 清空内容时候不进行搜索
  130. },
  131. /**
  132. * 回退到上一级
  133. */
  134. onClickLeft() {
  135. uni.navigateBack();
  136. },
  137. /**
  138. * 内容为空时,输入默认关键字
  139. */
  140. search() {
  141. if (!this.inputVal) {
  142. if (!this.show && this.searchName == "取消") {
  143. uni.hideKeyboard();
  144. this.isFocus = false;
  145. this.active = false;
  146. return;
  147. }
  148. }
  149. this.$emit("search", this.inputVal ? this.inputVal : this.placeholder);
  150. },
  151. },
  152. watch: {
  153. /**
  154. * 监听当前是否有值 是否显示清除图标
  155. */
  156. inputVal(newVal) {
  157. newVal ? (this.isDelShow = true) : (this.isDelShow = false);
  158. },
  159. },
  160. };
  161. </script>
  162. <style lang="scss" scoped>
  163. .serach {
  164. display: flex;
  165. width: 100%;
  166. //border-bottom: 1px #f5f5f5 solid; //HM修改 去掉边框
  167. box-sizing: border-box;
  168. font-size: $uni-font-size-base;
  169. .left-box {
  170. width: 15%;
  171. /* #ifndef APP-NVUE */
  172. text-align: center;
  173. // display: flex;
  174. // /* #endif */
  175. }
  176. .content {
  177. display: flex;
  178. align-items: center;
  179. width: 100%;
  180. height: 70rpx;
  181. color: #999;
  182. background: #eee;
  183. overflow: hidden;
  184. transition: all 0.2s linear;
  185. .content-box {
  186. width: 100%;
  187. display: flex;
  188. align-items: center;
  189. &.center {
  190. justify-content: center;
  191. }
  192. .icon {
  193. padding: 0 15rpx;
  194. &.icon-serach:before {
  195. content: "\e61c";
  196. }
  197. }
  198. .input {
  199. width: 100%;
  200. max-width: 100%;
  201. line-height: 60rpx;
  202. height: 60rpx;
  203. transition: all 0.2s linear;
  204. &.center {
  205. width: 200rpx;
  206. }
  207. &.sub {
  208. // position: absolute;
  209. width: auto;
  210. color: grey;
  211. }
  212. }
  213. }
  214. .serachBtn {
  215. height: 100%;
  216. flex-shrink: 0;
  217. padding: 0 30rpx;
  218. //HM修改 按钮背景色
  219. background: linear-gradient(to right, grey, grey);
  220. //background: $uni-color-success;
  221. line-height: 60rpx;
  222. color: #eee;
  223. //border-left: 1px #ccc solid; //HM修改 去掉边框
  224. transition: all 0.3s;
  225. }
  226. }
  227. .button {
  228. display: flex;
  229. align-items: center;
  230. justify-content: center;
  231. position: relative;
  232. flex-shrink: 0;
  233. width: 0;
  234. transition: all 0.2s linear;
  235. white-space: nowrap;
  236. overflow: hidden;
  237. &.active {
  238. padding-left: 15rpx;
  239. width: 100rpx;
  240. }
  241. }
  242. }
  243. .icon {
  244. font-family: iconfont;
  245. font-size: 32rpx;
  246. font-style: normal;
  247. color: #999;
  248. }
  249. .placeholder-color {
  250. color: #999;
  251. opacity: 0.4;
  252. }
  253. </style>