uni-combox.vue 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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"
  9. @blur="onBlur" />
  10. <uni-icons :type="showSelector? 'top' : 'bottom'" size="14" color="#999" @click="toggleSelector">
  11. </uni-icons>
  12. </view>
  13. <view class="uni-combox__selector" v-if="showSelector">
  14. <view class="uni-popper__arrow"></view>
  15. <scroll-view scroll-y="true" class="uni-combox__selector-scroll">
  16. <view class="uni-combox__selector-empty" v-if="filterCandidatesLength === 0">
  17. <text>{{emptyTips}}</text>
  18. </view>
  19. <view class="uni-combox__selector-item" v-for="(item,index) in filterCandidates" :key="index"
  20. @click="onSelectorClick(index)">
  21. <text>{{item}}</text>
  22. </view>
  23. </scroll-view>
  24. </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. }
  87. },
  88. computed: {
  89. labelStyle() {
  90. if (this.labelWidth === 'auto') {
  91. return ""
  92. }
  93. return `width: ${this.labelWidth}`
  94. },
  95. filterCandidates() {
  96. return this.candidates.filter((item) => {
  97. return item.toString().indexOf(this.inputVal) > -1
  98. })
  99. },
  100. filterCandidatesLength() {
  101. return this.filterCandidates.length
  102. }
  103. },
  104. watch: {
  105. // #ifndef VUE3
  106. value: {
  107. handler(newVal) {
  108. this.inputVal = newVal
  109. },
  110. immediate: true
  111. },
  112. // #endif
  113. // #ifdef VUE3
  114. modelValue: {
  115. handler(newVal) {
  116. this.inputVal = newVal
  117. },
  118. immediate: true
  119. },
  120. // #endif
  121. },
  122. methods: {
  123. toggleSelector() {
  124. this.showSelector = !this.showSelector
  125. },
  126. onFocus() {
  127. this.showSelector = true
  128. },
  129. onBlur() {
  130. setTimeout(() => {
  131. this.showSelector = false
  132. }, 153)
  133. },
  134. onSelectorClick(index) {
  135. this.inputVal = this.filterCandidates[index]
  136. this.showSelector = false
  137. this.$emit('input', this.inputVal)
  138. this.$emit('update:modelValue', this.inputVal)
  139. },
  140. onInput() {
  141. setTimeout(() => {
  142. this.$emit('input', this.inputVal)
  143. this.$emit('update:modelValue', this.inputVal)
  144. })
  145. }
  146. }
  147. }
  148. </script>
  149. <style lang="scss" scoped>
  150. .uni-combox {
  151. font-size: 14px;
  152. border: 1px solid #DCDFE6;
  153. border-radius: 4px;
  154. padding: 6px 10px;
  155. position: relative;
  156. /* #ifndef APP-NVUE */
  157. display: flex;
  158. /* #endif */
  159. // height: 40px;
  160. flex-direction: row;
  161. align-items: center;
  162. // border-bottom: solid 1px #DDDDDD;
  163. }
  164. .uni-combox__label {
  165. font-size: 16px;
  166. line-height: 22px;
  167. padding-right: 10px;
  168. color: #999999;
  169. }
  170. .uni-combox__input-box {
  171. position: relative;
  172. /* #ifndef APP-NVUE */
  173. display: flex;
  174. /* #endif */
  175. flex: 1;
  176. flex-direction: row;
  177. align-items: center;
  178. }
  179. .uni-combox__input {
  180. flex: 1;
  181. font-size: 14px;
  182. height: 22px;
  183. line-height: 22px;
  184. }
  185. .uni-combox__input-plac {
  186. font-size: 14px;
  187. color: #999;
  188. }
  189. .uni-combox__selector {
  190. /* #ifndef APP-NVUE */
  191. box-sizing: border-box;
  192. /* #endif */
  193. position: absolute;
  194. top: calc(100% + 12px);
  195. left: 0;
  196. width: 100%;
  197. background-color: #FFFFFF;
  198. border: 1px solid #EBEEF5;
  199. border-radius: 6px;
  200. box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  201. z-index: 2;
  202. padding: 4px 0;
  203. }
  204. .uni-combox__selector-scroll {
  205. /* #ifndef APP-NVUE */
  206. max-height: 200px;
  207. box-sizing: border-box;
  208. /* #endif */
  209. }
  210. .uni-combox__selector-empty,
  211. .uni-combox__selector-item {
  212. /* #ifndef APP-NVUE */
  213. display: flex;
  214. cursor: pointer;
  215. /* #endif */
  216. line-height: 36px;
  217. font-size: 14px;
  218. text-align: center;
  219. // border-bottom: solid 1px #DDDDDD;
  220. padding: 0px 10px;
  221. }
  222. .uni-combox__selector-item:hover {
  223. background-color: #f9f9f9;
  224. }
  225. .uni-combox__selector-empty:last-child,
  226. .uni-combox__selector-item:last-child {
  227. /* #ifndef APP-NVUE */
  228. border-bottom: none;
  229. /* #endif */
  230. }
  231. // picker 弹出层通用的指示小三角
  232. .uni-popper__arrow,
  233. .uni-popper__arrow::after {
  234. position: absolute;
  235. display: block;
  236. width: 0;
  237. height: 0;
  238. border-color: transparent;
  239. border-style: solid;
  240. border-width: 6px;
  241. }
  242. .uni-popper__arrow {
  243. filter: drop-shadow(0 2px 12px rgba(0, 0, 0, 0.03));
  244. top: -6px;
  245. left: 10%;
  246. margin-right: 3px;
  247. border-top-width: 0;
  248. border-bottom-color: #EBEEF5;
  249. }
  250. .uni-popper__arrow::after {
  251. content: " ";
  252. top: 1px;
  253. margin-left: -6px;
  254. border-top-width: 0;
  255. border-bottom-color: #fff;
  256. }
  257. .uni-combox__no-border {
  258. border: none;
  259. }
  260. </style>