uni-combox.vue 4.9 KB

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