index.vue 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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: '#daf3e6',
  104. },
  105. // 被选中的字体颜色
  106. selectedColor: {
  107. type: String,
  108. default: '#00B760',
  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. this.$emit('onBlur')
  186. }, 153)
  187. },
  188. onSelectorClick(index) {
  189. this.dictVal = this.filterCandidates[index][`${this.valueKey}`]
  190. //this.dictVal 的赋值一定要在this.inputVal前执行,
  191. //因为this.filterCandidates会监听this.inputVal的变化被重新赋值
  192. //这样在选择列表中非第一个选项会报错
  193. this.inputVal = this.filterCandidates[index][`${this.labelKey}`]
  194. this.showSelector = false
  195. this.$emit('input', this.dictVal)
  196. this.$emit('update:modelValue', this.dictVal)
  197. },
  198. onInput() {
  199. this.filterCandidates = this.candidates.filter(item => {
  200. return item[`${this.labelKey}`].toString().indexOf(this.inputVal) > -1
  201. })
  202. setTimeout(() => {
  203. if (!this.inputVal) this.dictVal = this.inputVal
  204. this.$emit('input', this.inputVal)
  205. this.$emit('update:modelValue', this.inputVal)
  206. })
  207. },
  208. setLabel() {
  209. this.$nextTick(()=>{
  210. if (this.candidates?.length > 0 && this.dictVal) {
  211. const obj = this.candidates.find(item => item[`${this.valueKey}`] === this.dictVal)
  212. if (obj) {
  213. this.inputVal = obj[`${this.labelKey}`]
  214. }
  215. }
  216. })
  217. },
  218. getOnInputValue() {
  219. return this.inputVal
  220. },
  221. getValue() {
  222. if (this.inputVal === this.dictVal) return { [this.itemTextName]: this.inputVal, [this.itemValueName]: null }
  223. else return { [this.itemTextName]: null, [this.itemValueName]: this.dictVal }
  224. }
  225. }
  226. }
  227. </script>
  228. <style lang="scss">
  229. .uni-combox {
  230. position: relative;
  231. display: flex;
  232. flex-direction: row;
  233. align-items: center;
  234. min-height: 70rpx;
  235. // width: 100%;
  236. padding: 0 15rpx 0 20rpx;
  237. font-size: 28rpx;
  238. border: 1px solid #e5e5e5;
  239. border-radius: 8rpx;
  240. // background-color: #fff;
  241. }
  242. .uni-combox__label {
  243. padding-right: 10rpx;
  244. color: #999999;
  245. font-size: 32rpx;
  246. line-height: 44rpx;
  247. }
  248. .uni-combox__input-box {
  249. position: relative;
  250. display: flex;
  251. flex: 1;
  252. flex-direction: row;
  253. align-items: center;
  254. }
  255. .uni-combox__input {
  256. flex: 1;
  257. height: 44rpx;
  258. font-size: 28rpx;
  259. line-height: 44rpx;
  260. }
  261. .uni-combox__input-plac {
  262. color: #999;
  263. font-size: 12px;
  264. }
  265. .uni-combox__selector {
  266. position: absolute;
  267. top: calc(100% + 12px);
  268. left: 0;
  269. z-index: 999;
  270. box-sizing: border-box;
  271. width: 100%;
  272. padding: 8rpx 0;
  273. background-color: #FFFFFF;
  274. border: 1px solid #ebeef5;
  275. border-radius: 12rpx;
  276. box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  277. }
  278. .uni-combox__selector-scroll {
  279. max-height: 300rpx;
  280. box-sizing: border-box;
  281. }
  282. .uni-combox__selector-empty,
  283. .uni-combox__selector-item {
  284. display: flex;
  285. padding: 0px 0px;
  286. // padding: 0px 10px;
  287. font-size: 14px;
  288. line-height: 70rpx;
  289. text-indent: 1rem;
  290. text-align: center;
  291. cursor: pointer;
  292. }
  293. .uni-combox__selector-item:hover {
  294. background-color: #e5e5e5;
  295. }
  296. .uni-combox__selector-empty:last-child,
  297. .uni-combox__selector-item:last-child {
  298. border-bottom: none;
  299. }
  300. // picker 弹出层通用的指示小三角
  301. .uni-popper__arrow,
  302. .uni-popper__arrow::after {
  303. position: absolute;
  304. display: block;
  305. width: 0;
  306. height: 0;
  307. border-color: transparent;
  308. border-style: solid;
  309. border-width: 12rpx;
  310. }
  311. .uni-popper__arrow {
  312. top: -12rpx;
  313. left: 10%;
  314. margin-right: 3px;
  315. border-top-width: 0;
  316. border-bottom-color: #ebeef5;
  317. filter: drop-shadow(0 2px 12px rgba(0, 0, 0, 0.03));
  318. }
  319. .uni-popper__arrow::after {
  320. content: " ";
  321. top: 2rpx;
  322. margin-left: -12rpx;
  323. border-top-width: 0;
  324. border-bottom-color: #fff;
  325. }
  326. .uni-combox__no-border {
  327. border: none;
  328. }
  329. </style>