index.vue 8.2 KB

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