uni-events-helper-wx
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

256 lines
8.1 KiB

3 years ago
  1. <template>
  2. <view class="serach">
  3. <view class="content" :style="{ 'border-radius': radius + 'px' }">
  4. <view class="content-box">
  5. <!-- 下拉选择框 -->
  6. <view class="seach-select" v-if="selectList.length>0">
  7. <!-- 选中值 -->
  8. <view class="select-value" @click="selectClick">
  9. {{selectList[selectIndex].name}}
  10. <text class="cuIcon-triangledownfill" style=""></text>
  11. </view>
  12. <!-- 选择列表 -->
  13. <view class="select-item-list" :style="{'display':(showSelectList)?'block':'none'}">
  14. <text class="cuIcon-triangleupfill"
  15. style="position: absolute;top: -18px;left: 60rpx;font-size: 30rpx;color: #FFFFFF;"></text>
  16. <view :class="['select-item',(index>0)?'item-border':'']" v-for="(data,index) in selectList"
  17. :key="index" @click="selectItem(index)">{{data.name}}</view>
  18. </view>
  19. </view>
  20. <text class="cuIcon-search" v-if="showSeachIcon" style="margin: 0 10rpx;"></text>
  21. <input :placeholder="placeholder" @input="inputChange" confirm-type="search" @confirm="triggerConfirm"
  22. class="input" :focus="isFocus" v-model="inputVal" @focus="focus" @blur="blur" />
  23. <text v-if="isDelShow" class="cuIcon-roundclose" @tap.stop="clear"></text>
  24. </view>
  25. <view v-if="
  26. (showSeachBth && button === 'inside') ||
  27. (isDelShow && button === 'inside')
  28. " class="serachBtn" style="background-color: #C0191F;color: #fff;" @tap="search">
  29. 搜索
  30. </view>
  31. </view>
  32. <view v-if="button === 'outside'" class="button" :class="{ active: showSeachBth }" @tap="search">
  33. <view class="button-item">{{ !showSeachBth ? searchName : '搜索' }}</view>
  34. </view>
  35. <view v-show="showSelectList" @click="selectClick" class="page-mask"></view>
  36. </view>
  37. </template>
  38. <script>
  39. export default {
  40. props: {
  41. selectList: {
  42. type: Array,
  43. default: [
  44. // {
  45. // id: 1,
  46. // name: '产品'
  47. // },
  48. // {
  49. // id: 2,
  50. // name: '内容'
  51. // }
  52. ]
  53. },
  54. placeholder: {
  55. value: String,
  56. default: '请输入搜索内容'
  57. },
  58. value: {
  59. type: String,
  60. default: ''
  61. },
  62. button: {
  63. value: String,
  64. default: 'outside'
  65. },
  66. showSeachIcon: {
  67. value: Boolean,
  68. default: true
  69. },
  70. showSeachBth: {
  71. value: Boolean,
  72. default: true
  73. },
  74. radius: {
  75. value: String,
  76. default: 60
  77. }
  78. },
  79. data() {
  80. return {
  81. showSelectList: false,
  82. selectIndex: 0,
  83. isFocus: true,
  84. inputVal: '',
  85. searchName: '取消',
  86. isDelShow: false
  87. };
  88. },
  89. methods: {
  90. selectItem(index) {
  91. this.selectIndex = index
  92. this.showSelectList = !this.showSelectList;
  93. },
  94. selectClick() {
  95. this.showSelectList = !this.showSelectList;
  96. },
  97. triggerConfirm() {
  98. let searchQuery = {
  99. keyword: this.inputVal
  100. }
  101. if (this.selectList.length > 0) {
  102. searchQuery.selectIndex = this.selectIndex;
  103. }
  104. this.$emit('confirm', searchQuery);
  105. },
  106. inputChange(event) {
  107. const keyword = event.detail.value;
  108. this.$emit('input', keyword);
  109. if (this.inputVal) {
  110. this.isDelShow = true;
  111. }
  112. },
  113. focus() {
  114. if (this.inputVal) {
  115. this.isDelShow = true;
  116. }
  117. },
  118. blur() {
  119. this.isFocus = false;
  120. uni.hideKeyboard();
  121. },
  122. clear() {
  123. uni.hideKeyboard();
  124. this.isFocus = false;
  125. this.inputVal = '';
  126. this.$emit('input', '');
  127. },
  128. getFocus() {
  129. this.isFocus = true;
  130. },
  131. search() {
  132. let searchQuery = {
  133. keyword: this.inputVal
  134. }
  135. if (this.selectList.length > 0) {
  136. searchQuery.selectIndex = this.selectIndex;
  137. }
  138. this.$emit('search', searchQuery);
  139. }
  140. },
  141. watch: {
  142. inputVal(newVal) {
  143. if (newVal) {
  144. this.searchName = '搜索';
  145. } else {
  146. this.searchName = '取消';
  147. this.isDelShow = false;
  148. }
  149. },
  150. value(val) {
  151. this.inputVal = val.trim();
  152. }
  153. }
  154. };
  155. </script>
  156. <style lang="scss" scoped>
  157. .serach {
  158. display: flex;
  159. width: 100%;
  160. box-sizing: border-box;
  161. font-size: $uni-font-size-base;
  162. .content {
  163. display: flex;
  164. align-items: center;
  165. width: 100%;
  166. height: 60upx;
  167. background: #fff;
  168. transition: all 0.2s linear;
  169. border-radius: 30px;
  170. .content-box {
  171. width: 100%;
  172. display: flex;
  173. align-items: center;
  174. .seach-select {
  175. min-width: 100rpx;
  176. margin-left: 10px;
  177. position: relative;
  178. max-width: 100rpx;
  179. .select-item-list {
  180. background-color: #FFFFFF;
  181. position: absolute;
  182. top: 75rpx;
  183. width: 150rpx;
  184. left: -20rpx;
  185. border-radius: 10rpx;
  186. z-index: 10;
  187. box-shadow: 0px 0px 5px #C9C9C9;
  188. .select-item {
  189. text-align: center;
  190. padding: 10rpx 0;
  191. }
  192. .item-border {
  193. border-top: 1rpx solid #EEEEEE;
  194. }
  195. }
  196. }
  197. .input {
  198. width: 100%;
  199. max-width: 100%;
  200. line-height: 60upx;
  201. height: 60upx;
  202. transition: all 0.2s linear;
  203. }
  204. }
  205. .serachBtn {
  206. height: 100%;
  207. flex-shrink: 0;
  208. padding: 0 30upx;
  209. line-height: 60upx;
  210. transition: all 0.3s;
  211. border-top-right-radius: 60px;
  212. border-bottom-right-radius: 60px;
  213. }
  214. }
  215. .button {
  216. display: flex;
  217. align-items: center;
  218. justify-content: center;
  219. position: relative;
  220. flex-shrink: 0;
  221. width: 0;
  222. transition: all 0.2s linear;
  223. white-space: nowrap;
  224. overflow: hidden;
  225. &.active {
  226. padding-left: 15upx;
  227. width: 100upx;
  228. }
  229. }
  230. .page-mask {
  231. position: fixed;
  232. top: 0;
  233. bottom: 0;
  234. right: 0;
  235. left: 0;
  236. z-index: 5;
  237. }
  238. }
  239. </style>