nx-search.vue 8.3 KB

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