nx-search.vue 8.0 KB

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