uni-combox.vue 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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" @focus="onFocus" @blur="onBlur" />
  8. <uni-icons class="uni-combox__input-arrow" type="arrowdown" size="14" @click="toggleSelector"></uni-icons>
  9. <view class="uni-combox__selector" v-if="showSelector">
  10. <scroll-view scroll-y="true" class="uni-combox__selector-scroll">
  11. <view class="uni-combox__selector-empty" v-if="filterCandidatesLength === 0">
  12. <text>{{emptyTips}}</text>
  13. </view>
  14. <view class="uni-combox__selector-item" v-for="(item,index) in filterCandidates" :key="index" @click="onSelectorClick(index)">
  15. <text>{{item}}</text>
  16. </view>
  17. </scroll-view>
  18. </view>
  19. </view>
  20. </view>
  21. </template>
  22. <script>
  23. import uniIcons from '../uni-icons/uni-icons.vue'
  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. components: {
  38. uniIcons
  39. },
  40. props: {
  41. label: {
  42. type: String,
  43. default: ''
  44. },
  45. labelWidth: {
  46. type: String,
  47. default: 'auto'
  48. },
  49. placeholder: {
  50. type: String,
  51. default: ''
  52. },
  53. candidates: {
  54. type: Array,
  55. default () {
  56. return []
  57. }
  58. },
  59. emptyTips: {
  60. type: String,
  61. default: '无匹配项'
  62. },
  63. value: {
  64. type: String,
  65. default: ''
  66. }
  67. },
  68. data() {
  69. return {
  70. showSelector: false,
  71. inputVal: ''
  72. }
  73. },
  74. computed: {
  75. labelStyle() {
  76. if (this.labelWidth === 'auto') {
  77. return {}
  78. }
  79. return {
  80. width: this.labelWidth
  81. }
  82. },
  83. filterCandidates() {
  84. return this.candidates.filter((item) => {
  85. return item.indexOf(this.inputVal) > -1
  86. })
  87. },
  88. filterCandidatesLength() {
  89. return this.filterCandidates.length
  90. }
  91. },
  92. watch: {
  93. value: {
  94. handler(newVal) {
  95. this.inputVal = newVal
  96. },
  97. immediate: true
  98. }
  99. },
  100. methods: {
  101. toggleSelector() {
  102. this.showSelector = !this.showSelector
  103. },
  104. onFocus() {
  105. this.showSelector = true
  106. },
  107. onBlur() {
  108. setTimeout(() => {
  109. this.showSelector = false
  110. }, 50)
  111. },
  112. onSelectorClick(index) {
  113. this.inputVal = this.filterCandidates[index]
  114. this.showSelector = false
  115. this.$emit('input', this.inputVal)
  116. },
  117. onInput() {
  118. setTimeout(() => {
  119. this.$emit('input', this.inputVal)
  120. })
  121. }
  122. }
  123. }
  124. </script>
  125. <style scoped>
  126. .uni-combox {
  127. /* #ifndef APP-NVUE */
  128. display: flex;
  129. /* #endif */
  130. height: 40px;
  131. flex-direction: row;
  132. align-items: center;
  133. }
  134. .uni-combox__label {
  135. font-size: 16px;
  136. line-height: 22px;
  137. padding-right: 10px;
  138. color: #999999;
  139. }
  140. .uni-combox__input-box {
  141. position: relative;
  142. /* #ifndef APP-NVUE */
  143. display: flex;
  144. /* #endif */
  145. flex: 1;
  146. flex-direction: row;
  147. align-items: center;
  148. }
  149. .uni-combox__input {
  150. flex: 1;
  151. font-size: 16px;
  152. height: 22px;
  153. line-height: 22px;
  154. }
  155. .uni-combox__input-arrow {
  156. padding: 10px;
  157. }
  158. .uni-combox__selector {
  159. box-sizing: border-box;
  160. position: absolute;
  161. top: 42px;
  162. left: 0;
  163. width: 100%;
  164. background-color: #FFFFFF;
  165. border-radius: 6px;
  166. box-shadow: #DDDDDD 4px 4px 8px, #DDDDDD -4px -4px 8px;
  167. z-index: 2;
  168. }
  169. .uni-combox__selector-scroll {
  170. max-height: 200px;
  171. box-sizing: border-box;
  172. }
  173. .uni-combox__selector::before {
  174. content: '';
  175. position: absolute;
  176. width: 0;
  177. height: 0;
  178. border-bottom: solid 6px #FFFFFF;
  179. border-right: solid 6px transparent;
  180. border-left: solid 6px transparent;
  181. left: 50%;
  182. top: -6px;
  183. margin-left: -6px;
  184. }
  185. .uni-combox__selector-empty,
  186. .uni-combox__selector-item {
  187. /* #ifdef APP-NVUE */
  188. display: flex;
  189. /* #endif */
  190. line-height: 36px;
  191. font-size: 14px;
  192. text-align: center;
  193. border-bottom: solid 1px #DDDDDD;
  194. margin: 0px 10px;
  195. }
  196. .uni-combox__selector-empty:last-child,
  197. .uni-combox__selector-item:last-child {
  198. border-bottom: none;
  199. }
  200. </style>