selectSearch.vue 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. <template>
  2. <view class="superwei-combox" :class="border ? '' : 'superwei-combox__no-border'">
  3. <view v-if="label" class="superwei-combox__label" :style="labelStyle">
  4. <text>{{label}}</text>
  5. </view>
  6. <view class="superwei-combox__input-box">
  7. <input class="superwei-combox__input" type="text" style="text-align: center;font-size: 18px;"
  8. :placeholder="placeholder" placeholder-class="superwei-combox__input-plac" v-model="inputVal"
  9. @input="onInput" @focus="onFocus" @blur="onBlur" />
  10. <uni-icons :type="showSelector? 'top' : 'bottom'" size="14" color="#999" @click="toggleSelector">
  11. </uni-icons>
  12. </view>
  13. <view class="superwei-combox__selector" v-if="showSelector">
  14. <view class="uni-popper__arrow"></view>
  15. <scroll-view scroll-y="true" class="superwei-combox__selector-scroll">
  16. <view class="superwei-combox__selector-empty" v-if="filterCandidatesLength === 0">
  17. <text>{{emptyTips}}</text>
  18. </view>
  19. <view class="superwei-combox__selector-item" v-for="(item,index) in filterCandidates" :key="index"
  20. @click="onSelectorClick(index)">
  21. <text
  22. :style="(isJSON?item[keyName]?item[keyName]==inputVal:false:item==inputVal)?'font-weight: bold;background-color: '+selectedBackground+';color: '+selectedColor:''">{{isJSON?item[keyName]?item[keyName]:'字段'+keyName+'不存在':item}}</text>
  23. </view>
  24. </scroll-view>
  25. </view>
  26. </view>
  27. </template>
  28. <script>
  29. /**
  30. * Combox 组合输入框
  31. * @description 组合输入框一般用于既可以输入也可以选择的场景
  32. * @property {String} label 左侧文字
  33. * @property {String} labelWidth 左侧内容宽度
  34. * @property {String} placeholder 输入框占位符
  35. * @property {Array} candidates 候选项列表
  36. * @property {String} emptyTips 筛选结果为空时显示的文字
  37. * @property {String} value 组合框的值
  38. * @property {String} selectedBackground 选中项背景颜色
  39. * @property {String} selectedColor 选中项文字颜色
  40. * @property {Boolean} isJSON 是否是json数组
  41. * @property {String} keyName json数组显示的字段值
  42. */
  43. export default {
  44. name: 'superweiCombox',
  45. emits: ['input', 'update:modelValue', 'select'],
  46. props: {
  47. isJSON: {
  48. type: Boolean,
  49. default: false
  50. },
  51. keyName: {
  52. type: String,
  53. default: ''
  54. },
  55. selectedBackground: {
  56. type: String,
  57. default: '#f5f7fa'
  58. },
  59. selectedColor: {
  60. type: String,
  61. default: '#409eff'
  62. },
  63. border: {
  64. type: Boolean,
  65. default: true
  66. },
  67. label: {
  68. type: String,
  69. default: ''
  70. },
  71. labelWidth: {
  72. type: String,
  73. default: 'auto'
  74. },
  75. placeholder: {
  76. type: String,
  77. default: ''
  78. },
  79. candidates: {
  80. type: Array,
  81. default () {
  82. return []
  83. }
  84. },
  85. emptyTips: {
  86. type: String,
  87. default: '无匹配项'
  88. },
  89. // #ifndef VUE3
  90. value: {
  91. type: [String, Number],
  92. default: ''
  93. },
  94. // #endif
  95. // #ifdef VUE3
  96. modelValue: {
  97. type: [String, Number],
  98. default: ''
  99. },
  100. // #endif
  101. },
  102. data() {
  103. return {
  104. isInput: false,
  105. showSelector: false,
  106. inputVal: ''
  107. }
  108. },
  109. computed: {
  110. labelStyle() {
  111. if (this.labelWidth === 'auto') {
  112. return ""
  113. }
  114. return `width: ${this.labelWidth}`
  115. },
  116. filterCandidates() {
  117. if (this.isInput) {
  118. if (this.isJSON) {
  119. return this.candidates.filter((item) => {
  120. return item[this.keyName].toString().indexOf(this.inputVal) > -1
  121. })
  122. } else {
  123. return this.candidates.filter((item) => {
  124. return item.toString().indexOf(this.inputVal) > -1
  125. })
  126. }
  127. } else {
  128. return this.candidates
  129. }
  130. },
  131. filterCandidatesLength() {
  132. return this.filterCandidates.length
  133. }
  134. },
  135. watch: {
  136. // #ifndef VUE3
  137. value: {
  138. handler(newVal) {
  139. this.inputVal = newVal
  140. this.isInput = true
  141. },
  142. immediate: true
  143. },
  144. // #endif
  145. // #ifdef VUE3
  146. modelValue: {
  147. handler(newVal) {
  148. this.inputVal = newVal
  149. this.isInput = true
  150. },
  151. immediate: true
  152. },
  153. // #endif
  154. },
  155. methods: {
  156. toggleSelector() {
  157. this.showSelector = !this.showSelector
  158. this.isInput = false
  159. },
  160. onFocus() {
  161. // console.log(this.candidates, this.filterCandidates, this.inputVal);
  162. this.showSelector = true
  163. this.isInput = true
  164. },
  165. onBlur() {
  166. setTimeout(() => {
  167. const fdIndex = this.candidates.findIndex(item => item.name == this.inputVal);
  168. if (fdIndex == -1) {
  169. this.inputVal = "";
  170. this.$emit('input', this.inputVal)
  171. this.$emit('update:modelValue', this.inputVal)
  172. this.$emit('select', this.inputVal)
  173. }
  174. this.showSelector = false
  175. this.isInput = false
  176. }, 153)
  177. },
  178. onSelectorClick(index) {
  179. let item = this.filterCandidates[index]
  180. if (this.isJSON) {
  181. this.inputVal = item[this.keyName]
  182. } else {
  183. this.inputVal = item
  184. }
  185. this.showSelector = false
  186. this.$emit('input', this.inputVal)
  187. this.$emit('update:modelValue', this.inputVal)
  188. this.$emit('select', item)
  189. },
  190. onInput() {
  191. setTimeout(() => {
  192. this.$emit('input', this.inputVal)
  193. this.$emit('update:modelValue', this.inputVal)
  194. })
  195. }
  196. }
  197. }
  198. </script>
  199. <style lang="scss" scoped>
  200. @media only screen and (max-width: 999px) {
  201. /* 针对手机: */
  202. .superwei-combox {
  203. font-size: 14px;
  204. border: 0px solid #12b7f5;
  205. border-radius: 4px;
  206. padding: 6px 10px;
  207. position: relative;
  208. /* #ifndef APP-NVUE */
  209. display: flex;
  210. /* #endif */
  211. // height: 40px;
  212. flex-direction: row;
  213. align-items: center;
  214. // border-bottom: solid 1px #DDDDDD;
  215. }
  216. }
  217. @media only screen and (min-width: 1000px) {
  218. /* 针对手机: */
  219. .superwei-combox {
  220. font-size: 14px;
  221. border: 2px solid #12b7f5;
  222. border-radius: 4px;
  223. padding: 6px 10px;
  224. position: relative;
  225. /* #ifndef APP-NVUE */
  226. display: flex;
  227. /* #endif */
  228. // height: 40px;
  229. flex-direction: row;
  230. align-items: center;
  231. // border-bottom: solid 1px #DDDDDD;
  232. }
  233. }
  234. .superwei-combox__label {
  235. font-size: 16px;
  236. line-height: 22px;
  237. padding-right: 10px;
  238. color: #999999;
  239. }
  240. .superwei-combox__input-box {
  241. position: relative;
  242. /* #ifndef APP-NVUE */
  243. display: flex;
  244. /* #endif */
  245. flex: 1;
  246. flex-direction: row;
  247. align-items: center;
  248. }
  249. .superwei-combox__input {
  250. flex: 1;
  251. font-size: 14px;
  252. height: 32px;
  253. line-height: 32px;
  254. }
  255. .superwei-combox__input-plac {
  256. font-size: 14px;
  257. color: #999; //placeholder-style="color:#FFFFFF"
  258. }
  259. .superwei-combox__selector {
  260. /* #ifndef APP-NVUE */
  261. box-sizing: border-box;
  262. /* #endif */
  263. position: absolute;
  264. top: calc(100% + 12px);
  265. left: 0;
  266. width: 100%;
  267. background-color: #FFFFFF;
  268. border: 1px solid #EBEEF5;
  269. border-radius: 6px;
  270. box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  271. // z-index: 2;
  272. padding: 4px 0;
  273. height: 190px;
  274. z-index: 999;
  275. }
  276. .superwei-combox__selector-scroll {
  277. /* #ifndef APP-NVUE */
  278. max-height: 180px;
  279. box-sizing: border-box;
  280. /* #endif */
  281. }
  282. .superwei-combox__selector-empty,
  283. .superwei-combox__selector-item {
  284. /* #ifndef APP-NVUE */
  285. display: flex;
  286. cursor: pointer;
  287. /* #endif */
  288. line-height: 36px;
  289. font-size: 14px;
  290. text-align: center;
  291. // border-bottom: solid 1px #DDDDDD;
  292. padding: 0px 0px;
  293. }
  294. .superwei-combox__selector-empty text,
  295. .superwei-combox__selector-item text {
  296. width: 100%;
  297. }
  298. .superwei-combox__selector-item:hover {
  299. background-color: #f9f9f9;
  300. }
  301. .superwei-combox__selector-empty:last-child,
  302. .superwei-combox__selector-item:last-child {
  303. /* #ifndef APP-NVUE */
  304. border-bottom: none;
  305. /* #endif */
  306. }
  307. // picker 弹出层通用的指示小三角
  308. .uni-popper__arrow,
  309. .uni-popper__arrow::after {
  310. position: absolute;
  311. display: block;
  312. width: 0;
  313. height: 0;
  314. border-color: transparent;
  315. border-style: solid;
  316. border-width: 6px;
  317. }
  318. .uni-popper__arrow {
  319. filter: drop-shadow(0 2px 12px rgba(0, 0, 0, 0.03));
  320. top: -6px;
  321. left: 80%;
  322. margin-right: 3px;
  323. border-top-width: 0;
  324. border-bottom-color: #EBEEF5;
  325. }
  326. .uni-popper__arrow::after {
  327. content: " ";
  328. top: 1px;
  329. margin-left: -6px;
  330. border-top-width: 0;
  331. border-bottom-color: #fff;
  332. }
  333. .superwei-combox__no-border {
  334. border-width: 0;
  335. border-style: none;
  336. border-color: transparent;
  337. }
  338. </style>