uni-combox.vue 5.9 KB

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