nx-search.vue 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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. defaultSelectIndex: {
  81. value: String,
  82. default: 0
  83. },
  84. },
  85. data() {
  86. return {
  87. showSelectList: false,
  88. selectIndex: 0,
  89. isFocus: this.setIsFocus,
  90. inputVal: '',
  91. searchName: '取消',
  92. isDelShow: false
  93. };
  94. },
  95. created() {
  96. //this.isFocus=this.setIsFocus;
  97. this.selectIndex=this.defaultSelectIndex
  98. },
  99. methods: {
  100. selectItem(index) {
  101. this.selectIndex = index
  102. this.showSelectList = !this.showSelectList;
  103. this.$emit('selectItem', index);
  104. },
  105. selectClick() {
  106. if(this.selectList.length>1){
  107. this.showSelectList = !this.showSelectList;
  108. }
  109. },
  110. triggerConfirm() {
  111. let searchQuery = {
  112. keyword: this.inputVal
  113. }
  114. if (this.selectList.length > 0) {
  115. searchQuery.selectIndex = this.selectIndex;
  116. }
  117. this.$emit('confirm', searchQuery);
  118. },
  119. inputChange(event) {
  120. const keyword = event.detail.value;
  121. this.$emit('input', keyword);
  122. if (this.inputVal) {
  123. this.isDelShow = true;
  124. }
  125. },
  126. focus() {
  127. if (this.inputVal) {
  128. this.isDelShow = true;
  129. }
  130. this.$emit('focus');
  131. },
  132. blur() {
  133. this.isFocus = false;
  134. uni.hideKeyboard();
  135. },
  136. clear() {
  137. uni.hideKeyboard();
  138. this.isFocus = false;
  139. this.inputVal = '';
  140. this.$emit('input', '');
  141. },
  142. getFocus() {
  143. this.isFocus = true;
  144. },
  145. search() {
  146. let searchQuery = {
  147. keyword: this.inputVal
  148. }
  149. if (this.selectList.length > 0) {
  150. searchQuery.selectIndex = this.selectIndex;
  151. }
  152. this.$emit('search', searchQuery);
  153. }
  154. },
  155. watch: {
  156. defaultSelectIndex(val){
  157. this.selectIndex=this.defaultSelectIndex
  158. },
  159. inputVal(newVal) {
  160. if (newVal) {
  161. this.searchName = '搜索';
  162. } else {
  163. this.searchName = '取消';
  164. this.isDelShow = false;
  165. }
  166. },
  167. value(val) {
  168. this.inputVal = val.trim();
  169. }
  170. }
  171. };
  172. </script>
  173. <style lang="scss" scoped>
  174. img{
  175. width: 24px;
  176. height: 24px;
  177. vertical-align: middle;
  178. }
  179. .serach {
  180. display: flex;
  181. width: 100%;
  182. box-sizing: border-box;
  183. font-size: $uni-font-size-base;
  184. .content {
  185. display: flex;
  186. align-items: center;
  187. width: 100%;
  188. height: 72rpx;
  189. background-color: rgba(243, 246, 249, 1);
  190. transition: all 0.2s linear;
  191. border-radius: 30px;
  192. .content-box {
  193. width: 100%;
  194. display: flex;
  195. align-items: center;
  196. .cuIcon-search{
  197. width: 24px;
  198. height: 24px;
  199. img{
  200. width: 24px;
  201. height: 24px;
  202. vertical-align: middle;
  203. }
  204. }
  205. .seach-select {
  206. min-width: 160rpx;
  207. margin-left: 10px;
  208. position: relative;
  209. color: #101010;
  210. .select-item-list {
  211. background-color: #FFFFFF;
  212. position: absolute;
  213. top: 75rpx;
  214. width: 150rpx;
  215. left: -20rpx;
  216. border-radius: 10rpx;
  217. z-index: 10;
  218. box-shadow: 0px 0px 5px #C9C9C9;
  219. .select-item {
  220. text-align: center;
  221. padding: 10rpx 0;
  222. }
  223. .item-border {
  224. border-top: 1rpx solid #EEEEEE;
  225. }
  226. }
  227. }
  228. .input {
  229. width: 100%;
  230. max-width: 100%;
  231. line-height: 60upx;
  232. height: 60upx;
  233. transition: all 0.2s linear;
  234. }
  235. }
  236. .serachBtn {
  237. height: 100%;
  238. flex-shrink: 0;
  239. padding: 0 30upx;
  240. line-height: 72rpx;
  241. transition: all 0.3s;
  242. border-top-right-radius: 60px;
  243. border-bottom-right-radius: 60px;
  244. }
  245. }
  246. .button {
  247. display: flex;
  248. align-items: center;
  249. justify-content: center;
  250. position: relative;
  251. flex-shrink: 0;
  252. width: 0;
  253. transition: all 0.2s linear;
  254. white-space: nowrap;
  255. overflow: hidden;
  256. &.active {
  257. padding-left: 15upx;
  258. width: 100upx;
  259. }
  260. }
  261. .page-mask {
  262. position: fixed;
  263. top: 0;
  264. bottom: 0;
  265. right: 0;
  266. left: 0;
  267. z-index: 5;
  268. }
  269. }
  270. </style>