uni-combox.vue 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. <template>
  2. <view class="uni-combox" :class="border ? '' : 'uni-combox__no-border'">
  3. <view v-if="label" class="uni-combox__label" :style="labelStyle">
  4. <text>{{label}}</text>
  5. </view>
  6. <view class="uni-combox__input-box">
  7. <input class="uni-combox__input" type="text" :placeholder="placeholder"
  8. placeholder-class="uni-combox__input-plac" v-model="inputVal" @input="onInput" @focus="onFocus" @blur="onBlur" />
  9. <uni-icons :type="showSelector? 'top' : 'bottom'" size="14" color="#999" @click="toggleSelector">
  10. </uni-icons>
  11. </view>
  12. <view class="uni-combox__selector" v-if="showSelector">
  13. <view class="uni-popper__arrow"></view>
  14. <scroll-view scroll-y="true" class="uni-combox__selector-scroll" @scroll="onScroll">
  15. <view class="uni-combox__selector-empty" v-if="filterCandidatesLength === 0">
  16. <text>{{emptyTips}}</text>
  17. </view>
  18. <view class="uni-combox__selector-item" v-for="(item,index) in filterCandidates" :key="index" @click="onSelectorClick(index)">
  19. <text>{{item}}</text>
  20. </view>
  21. </scroll-view>
  22. </view>
  23. <!-- 新增蒙层,点击蒙层时关闭选项显示 -->
  24. <view class="uni-combox__mask" v-show="showSelector" @click="showSelector = false"></view>
  25. </view>
  26. </template>
  27. <script>
  28. /**
  29. * Combox 组合输入框
  30. * @description 组合输入框一般用于既可以输入也可以选择的场景
  31. * @tutorial https://ext.dcloud.net.cn/plugin?id=1261
  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. */
  39. export default {
  40. name: 'uniCombox',
  41. emits: ['input', 'update:modelValue'],
  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. placeholder: {
  56. type: String,
  57. default: ''
  58. },
  59. candidates: {
  60. type: Array,
  61. default () {
  62. return []
  63. }
  64. },
  65. emptyTips: {
  66. type: String,
  67. default: '无匹配项'
  68. },
  69. // #ifndef VUE3
  70. value: {
  71. type: [String, Number],
  72. default: ''
  73. },
  74. // #endif
  75. // #ifdef VUE3
  76. modelValue: {
  77. type: [String, Number],
  78. default: ''
  79. },
  80. // #endif
  81. },
  82. data() {
  83. return {
  84. showSelector: false,
  85. inputVal: '',
  86. blurTimer:null,
  87. }
  88. },
  89. computed: {
  90. labelStyle() {
  91. if (this.labelWidth === 'auto') {
  92. return ""
  93. }
  94. return `width: ${this.labelWidth}`
  95. },
  96. filterCandidates() {
  97. if (this.inputVal !== 0 && !this.inputVal) {
  98. return this.candidates
  99. }
  100. return this.candidates.filter((item) => {
  101. return item.toString().indexOf(this.inputVal) > -1
  102. })
  103. },
  104. filterCandidatesLength() {
  105. return this.filterCandidates.length
  106. }
  107. },
  108. watch: {
  109. // #ifndef VUE3
  110. value: {
  111. handler(newVal) {
  112. this.inputVal = newVal
  113. },
  114. immediate: true
  115. },
  116. // #endif
  117. // #ifdef VUE3
  118. modelValue: {
  119. handler(newVal) {
  120. this.inputVal = newVal
  121. },
  122. immediate: true
  123. },
  124. // #endif
  125. },
  126. methods: {
  127. toggleSelector() {
  128. this.showSelector = !this.showSelector
  129. },
  130. onFocus() {
  131. this.showSelector = true
  132. },
  133. onBlur() {
  134. this.blurTimer = setTimeout(() => {
  135. this.showSelector = false
  136. }, 153)
  137. },
  138. onScroll(){ // 滚动时将blur的定时器关掉
  139. if(this.blurTimer) {
  140. clearTimeout(this.blurTimer)
  141. this.blurTimer = null
  142. }
  143. },
  144. onSelectorClick(index) {
  145. this.inputVal = this.filterCandidates[index]
  146. this.showSelector = false
  147. this.$emit('input', this.inputVal)
  148. this.$emit('update:modelValue', this.inputVal)
  149. },
  150. onInput() {
  151. setTimeout(() => {
  152. this.$emit('input', this.inputVal)
  153. this.$emit('update:modelValue', this.inputVal)
  154. })
  155. }
  156. }
  157. }
  158. </script>
  159. <style lang="scss">
  160. .uni-combox {
  161. font-size: 14px;
  162. border: 1px solid #DCDFE6;
  163. border-radius: 4px;
  164. padding: 6px 10px;
  165. position: relative;
  166. /* #ifndef APP-NVUE */
  167. display: flex;
  168. /* #endif */
  169. // height: 40px;
  170. flex-direction: row;
  171. align-items: center;
  172. // border-bottom: solid 1px #DDDDDD;
  173. }
  174. .uni-combox__label {
  175. font-size: 16px;
  176. line-height: 22px;
  177. padding-right: 10px;
  178. color: #999999;
  179. }
  180. .uni-combox__input-box {
  181. position: relative;
  182. /* #ifndef APP-NVUE */
  183. display: flex;
  184. /* #endif */
  185. flex: 1;
  186. flex-direction: row;
  187. align-items: center;
  188. }
  189. .uni-combox__input {
  190. flex: 1;
  191. font-size: 14px;
  192. height: 22px;
  193. line-height: 22px;
  194. }
  195. .uni-combox__input-plac {
  196. font-size: 14px;
  197. color: #999;
  198. }
  199. .uni-combox__selector {
  200. /* #ifndef APP-NVUE */
  201. box-sizing: border-box;
  202. /* #endif */
  203. position: absolute;
  204. top: calc(100% + 12px);
  205. left: 0;
  206. width: 100%;
  207. background-color: #FFFFFF;
  208. border: 1px solid #EBEEF5;
  209. border-radius: 6px;
  210. box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  211. z-index: 3;
  212. padding: 4px 0;
  213. }
  214. .uni-combox__selector-scroll {
  215. /* #ifndef APP-NVUE */
  216. max-height: 200px;
  217. box-sizing: border-box;
  218. /* #endif */
  219. }
  220. .uni-combox__selector-empty,
  221. .uni-combox__selector-item {
  222. /* #ifndef APP-NVUE */
  223. display: flex;
  224. cursor: pointer;
  225. /* #endif */
  226. line-height: 36px;
  227. font-size: 14px;
  228. text-align: center;
  229. // border-bottom: solid 1px #DDDDDD;
  230. padding: 0px 10px;
  231. }
  232. .uni-combox__selector-item:hover {
  233. background-color: #f9f9f9;
  234. }
  235. .uni-combox__selector-empty:last-child,
  236. .uni-combox__selector-item:last-child {
  237. /* #ifndef APP-NVUE */
  238. border-bottom: none;
  239. /* #endif */
  240. }
  241. // picker 弹出层通用的指示小三角
  242. .uni-popper__arrow,
  243. .uni-popper__arrow::after {
  244. position: absolute;
  245. display: block;
  246. width: 0;
  247. height: 0;
  248. border-color: transparent;
  249. border-style: solid;
  250. border-width: 6px;
  251. }
  252. .uni-popper__arrow {
  253. filter: drop-shadow(0 2px 12px rgba(0, 0, 0, 0.03));
  254. top: -6px;
  255. left: 10%;
  256. margin-right: 3px;
  257. border-top-width: 0;
  258. border-bottom-color: #EBEEF5;
  259. }
  260. .uni-popper__arrow::after {
  261. content: " ";
  262. top: 1px;
  263. margin-left: -6px;
  264. border-top-width: 0;
  265. border-bottom-color: #fff;
  266. }
  267. .uni-combox__no-border {
  268. border: none;
  269. }
  270. .uni-combox__mask {
  271. width:100%;
  272. height:100%;
  273. position: fixed;
  274. top: 0;
  275. left: 0;
  276. z-index: 1;
  277. }
  278. </style>