nx-search.vue 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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. return []
  45. }
  46. },
  47. placeholder: {
  48. value: String,
  49. default: '请输入搜索内容'
  50. },
  51. value: {
  52. type: String,
  53. default: ''
  54. },
  55. button: {
  56. value: String,
  57. default: 'outside'
  58. },
  59. showSeachIcon: {
  60. value: Boolean,
  61. default: true
  62. },
  63. showSeachBth: {
  64. value: Boolean,
  65. default: true
  66. },
  67. radius: {
  68. value: String,
  69. default: 60
  70. }
  71. },
  72. data() {
  73. return {
  74. showSelectList: false,
  75. selectIndex: 0,
  76. isFocus: false,
  77. inputVal: '',
  78. searchName: '取消',
  79. isDelShow: false
  80. };
  81. },
  82. methods: {
  83. selectItem(index) {
  84. this.selectIndex = index
  85. this.showSelectList = !this.showSelectList;
  86. },
  87. selectClick() {
  88. this.showSelectList = !this.showSelectList;
  89. },
  90. triggerConfirm() {
  91. let searchQuery = {
  92. keyword: this.inputVal
  93. }
  94. if (this.selectList.length > 0) {
  95. searchQuery.selectIndex = this.selectIndex;
  96. }
  97. this.$emit('confirm', searchQuery);
  98. },
  99. inputChange(event) {
  100. const keyword = event.detail.value;
  101. this.$emit('input', keyword);
  102. if (this.inputVal) {
  103. this.isDelShow = true;
  104. }
  105. },
  106. focus() {
  107. if (this.inputVal) {
  108. this.isDelShow = true;
  109. }
  110. },
  111. blur() {
  112. this.isFocus = false;
  113. uni.hideKeyboard();
  114. },
  115. clear() {
  116. uni.hideKeyboard();
  117. this.isFocus = false;
  118. this.inputVal = '';
  119. this.$emit('clear', '');
  120. },
  121. getFocus() {
  122. this.isFocus = true;
  123. },
  124. search() {
  125. let searchQuery = {
  126. keyword: this.inputVal
  127. }
  128. if (this.selectList.length > 0) {
  129. searchQuery.selectIndex = this.selectIndex;
  130. }
  131. this.$emit('search', searchQuery);
  132. }
  133. },
  134. watch: {
  135. inputVal(newVal) {
  136. if (newVal) {
  137. this.searchName = '搜索';
  138. } else {
  139. this.searchName = '取消';
  140. this.isDelShow = false;
  141. }
  142. },
  143. value(val) {
  144. this.inputVal = val.trim();
  145. }
  146. }
  147. };
  148. </script>
  149. <style lang="scss" scoped>
  150. .serach {
  151. display: flex;
  152. width: 100%;
  153. box-sizing: border-box;
  154. font-size: $uni-font-size-base;
  155. .content {
  156. display: flex;
  157. align-items: center;
  158. width: 100%;
  159. height: 60upx;
  160. background: #fff;
  161. transition: all 0.2s linear;
  162. border-radius: 30px;
  163. border: 1rpx #5E00FF solid;
  164. .content-box {
  165. width: 100%;
  166. display: flex;
  167. align-items: center;
  168. .seach-select {
  169. min-width: 100rpx;
  170. margin-left: 10px;
  171. position: relative;
  172. max-width: 100rpx;
  173. .select-item-list {
  174. background-color: #FFFFFF;
  175. position: absolute;
  176. top: 75rpx;
  177. width: 150rpx;
  178. left: -20rpx;
  179. border-radius: 10rpx;
  180. z-index: 10;
  181. box-shadow: 0px 0px 5px #C9C9C9;
  182. .select-item {
  183. text-align: center;
  184. padding: 10rpx 0;
  185. }
  186. .item-border {
  187. border-top: 1rpx solid #EEEEEE;
  188. }
  189. }
  190. }
  191. .input {
  192. width: 100%;
  193. max-width: 100%;
  194. line-height: 60upx;
  195. height: 60upx;
  196. transition: all 0.2s linear;
  197. }
  198. }
  199. .serachBtn {
  200. height: 100%;
  201. flex-shrink: 0;
  202. padding: 0 30upx;
  203. line-height: 60upx;
  204. transition: all 0.3s;
  205. border-top-right-radius: 60px;
  206. border-bottom-right-radius: 60px;
  207. }
  208. }
  209. .button {
  210. display: flex;
  211. align-items: center;
  212. justify-content: center;
  213. position: relative;
  214. flex-shrink: 0;
  215. width: 0;
  216. transition: all 0.2s linear;
  217. white-space: nowrap;
  218. overflow: hidden;
  219. &.active {
  220. padding-left: 15upx;
  221. width: 100upx;
  222. }
  223. }
  224. .page-mask {
  225. position: fixed;
  226. top: 0;
  227. bottom: 0;
  228. right: 0;
  229. left: 0;
  230. z-index: 5;
  231. }
  232. }
  233. </style>