m-search-revision.vue 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. <template>
  2. <view class="serach">
  3. <view class="left-box" @tap="onClickLeft">
  4. <uni-icons style="line-height:70rpx" :color="color" 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. <!-- <view v-if="!active && mode === 2" class="input sub" @click="getFocus">请输入搜索内容</view> -->
  14. <!-- HM修改 @click换成@click.stop阻止冒泡 -->
  15. <text v-if="isDelShow" class="icon icon-del" @click.stop="clear"></text>
  16. </view>
  17. <view v-show="(active && show && button === 'inside') || (isDelShow && button === 'inside')" class="serachBtn" @click="search">搜索</view>
  18. </view>
  19. <view v-if="button === 'outside'" class="button" :class="{ active: show || active }">
  20. <!-- @click="search" -->
  21. <view v-if="isShowSeachGoods !=true" class="button-item">
  22. <div @click="out()">取消</div>
  23. </view>
  24. <view v-else class="button-item">
  25. <!-- {{ !show ? searchName : '搜索' }} -->
  26. <u-icon name="grid-fill" size="50" @click="handelListClass()" v-if="!isListClass"></u-icon>
  27. <u-icon v-else @click="handelListClass()" name="list-dot" size="50"></u-icon>
  28. </view>
  29. </view>
  30. </view>
  31. </template>
  32. <script>
  33. import uniStatusBar from "../uni-status-bar/uni-status-bar.vue";
  34. import uniIcons from "../uni-icons/uni-icons.vue";
  35. export default {
  36. watch: {},
  37. mounted() {},
  38. components: {
  39. uniStatusBar,
  40. uniIcons,
  41. },
  42. props: {
  43. mode: {
  44. value: Number,
  45. default: 1,
  46. },
  47. //HM修改 定义默认搜索关键词(水印文字)
  48. placeholder: {
  49. value: String,
  50. default: "请输入搜索内容",
  51. },
  52. value: {
  53. type: String,
  54. default: "",
  55. },
  56. button: {
  57. value: String,
  58. default: "outside",
  59. },
  60. show: {
  61. value: Boolean,
  62. default: true,
  63. },
  64. leftText: {
  65. type: String,
  66. default: "",
  67. },
  68. color: {
  69. type: String,
  70. },
  71. statusBar: {
  72. type: [Boolean, String],
  73. default: false,
  74. },
  75. rightText: {
  76. type: String,
  77. default: "",
  78. },
  79. leftIcon: {
  80. type: String,
  81. default: "",
  82. },
  83. rightIcon: {
  84. type: String,
  85. default: "",
  86. },
  87. radius: {
  88. value: String,
  89. default: 60,
  90. },
  91. isFocusVal: {
  92. value: Boolean,
  93. default: true,
  94. },
  95. },
  96. data() {
  97. return {
  98. isShowSeachGoods: false,
  99. // 点击左侧按钮
  100. nums: 0,
  101. iconParams: false,
  102. active: false,
  103. inputVal: "",
  104. searchName: "取消",
  105. isDelShow: false,
  106. isFocus: false,
  107. isListClass: true,
  108. };
  109. },
  110. mounted() {
  111. this.isFocus = this.isFocusVal;
  112. },
  113. methods: {
  114. ddType() {
  115. this.isShowSeachGoods = true;
  116. },
  117. out() {
  118. uni.reLaunch({
  119. url: "/pages/tabbar/home/index"
  120. });
  121. },
  122. // 切换排列顺序
  123. handelListClass() {
  124. this.isListClass = !this.isListClass;
  125. this.$emit("SwitchType");
  126. },
  127. //HM修改 触发组件confirm事件
  128. triggerConfirm() {
  129. this.nums++;
  130. this.$emit("confirm", false);
  131. uni.hideKeyboard();
  132. this.isShowSeachGoods = true;
  133. },
  134. //HM修改 触发组件input事件
  135. inputChange(event) {
  136. this.nums++;
  137. var keyword = event.detail.value;
  138. this.$emit("input", keyword);
  139. if (this.inputVal) {
  140. this.isDelShow = true;
  141. }
  142. },
  143. focus() {
  144. this.active = true;
  145. //HM修改 增加获取焦点判断
  146. if (this.inputVal) {
  147. this.isDelShow = true;
  148. }
  149. },
  150. blur() {
  151. this.isFocus = false;
  152. if (!this.inputVal) {
  153. this.active = false;
  154. }
  155. },
  156. clear() {
  157. //HM修改 收起键盘
  158. uni.hideKeyboard();
  159. this.isFocus = false;
  160. this.inputVal = "";
  161. this.active = false;
  162. //HM修改 清空内容时候触发组件input
  163. this.$emit("input", "");
  164. //this.$emit('search', '');//HM修改 清空内容时候不进行搜索
  165. },
  166. getFocus() {
  167. if (!this.isFocus) {
  168. this.isFocus = true;
  169. }
  170. },
  171. onClickLeft() {
  172. uni.navigateBack();
  173. },
  174. search() {
  175. this.nums++;
  176. //HM修改 增加点击取消时候退出输入状态,内容为空时,输入默认关键字
  177. if (!this.inputVal) {
  178. if (!this.show && this.searchName == "取消") {
  179. uni.hideKeyboard();
  180. this.isFocus = false;
  181. this.active = false;
  182. return;
  183. }
  184. }
  185. this.$emit("search", this.inputVal ? this.inputVal : this.placeholder);
  186. },
  187. },
  188. watch: {
  189. inputVal(newVal) {
  190. if (newVal) {
  191. this.searchName = "搜索";
  192. //this.isDelShow = true; //HM修改 直接点击页面预设关键字样式异常,注销
  193. } else {
  194. this.searchName = "取消";
  195. this.isDelShow = false;
  196. }
  197. },
  198. //HM修改 双向绑定
  199. value(val) {
  200. this.inputVal = val;
  201. },
  202. },
  203. };
  204. </script>
  205. <style lang="scss">
  206. .serach {
  207. display: flex;
  208. width: 100%;
  209. //border-bottom: 1px #f5f5f5 solid; //HM修改 去掉边框
  210. box-sizing: border-box;
  211. font-size: $uni-font-size-base;
  212. .left-box {
  213. width: 15%;
  214. /* #ifndef APP-NVUE */
  215. text-align: center;
  216. // display: flex;
  217. // /* #endif */
  218. }
  219. .content {
  220. display: flex;
  221. align-items: center;
  222. width: 100%;
  223. height: 70rpx;
  224. color: #999;
  225. background: #eee;
  226. overflow: hidden;
  227. transition: all 0.2s linear;
  228. // border-radius: 30px;
  229. .content-box {
  230. width: 100%;
  231. display: flex;
  232. align-items: center;
  233. &.center {
  234. justify-content: center;
  235. }
  236. .icon {
  237. padding: 0 15rpx;
  238. &.icon-del {
  239. font-size: 38rpx;
  240. &:before {
  241. content: "\e644";
  242. }
  243. }
  244. &.icon-serach:before {
  245. content: "\e61c";
  246. }
  247. }
  248. .input {
  249. width: 100%;
  250. max-width: 100%;
  251. line-height: 60rpx;
  252. height: 60rpx;
  253. transition: all 0.2s linear;
  254. &.center {
  255. width: 200rpx;
  256. }
  257. &.sub {
  258. // position: absolute;
  259. width: auto;
  260. color: grey;
  261. }
  262. }
  263. }
  264. .serachBtn {
  265. height: 100%;
  266. flex-shrink: 0;
  267. padding: 0 30rpx;
  268. //HM修改 按钮背景色
  269. background: linear-gradient(to right, grey, grey);
  270. //background: $uni-color-success;
  271. line-height: 60rpx;
  272. color: #eee;
  273. //border-left: 1px #ccc solid; //HM修改 去掉边框
  274. transition: all 0.3s;
  275. }
  276. }
  277. .button {
  278. display: flex;
  279. align-items: center;
  280. justify-content: center;
  281. position: relative;
  282. flex-shrink: 0;
  283. width: 0;
  284. transition: all 0.2s linear;
  285. white-space: nowrap;
  286. overflow: hidden;
  287. &.active {
  288. padding-left: 15rpx;
  289. width: 100rpx;
  290. }
  291. }
  292. }
  293. .icon {
  294. font-family: iconfont;
  295. font-size: 32rpx;
  296. font-style: normal;
  297. color: #999;
  298. }
  299. .placeholder-color {
  300. color: #999;
  301. opacity: 0.4;
  302. }
  303. </style>