uni-combox.vue 6.0 KB

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