uni-combox.vue 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. /* border-bottom: solid 1px #DDDDDD; */
  134. }
  135. .uni-combox__label {
  136. font-size: 16px;
  137. line-height: 22px;
  138. padding-right: 10px;
  139. color: #999999;
  140. }
  141. .uni-combox__input-box {
  142. position: relative;
  143. /* #ifndef APP-NVUE */
  144. display: flex;
  145. /* #endif */
  146. flex: 1;
  147. flex-direction: row;
  148. align-items: center;
  149. }
  150. .uni-combox__input {
  151. flex: 1;
  152. font-size: 16px;
  153. height: 22px;
  154. line-height: 22px;
  155. }
  156. .uni-combox__input-arrow {
  157. padding: 10px;
  158. }
  159. .uni-combox__selector {
  160. box-sizing: border-box;
  161. position: absolute;
  162. top: 42px;
  163. left: 0;
  164. width: 100%;
  165. background-color: #FFFFFF;
  166. border-radius: 6px;
  167. box-shadow: #DDDDDD 4px 4px 8px, #DDDDDD -4px -4px 8px;
  168. z-index: 2;
  169. }
  170. .uni-combox__selector-scroll {
  171. max-height: 200px;
  172. box-sizing: border-box;
  173. }
  174. .uni-combox__selector::before {
  175. content: '';
  176. position: absolute;
  177. width: 0;
  178. height: 0;
  179. border-bottom: solid 6px #FFFFFF;
  180. border-right: solid 6px transparent;
  181. border-left: solid 6px transparent;
  182. left: 50%;
  183. top: -6px;
  184. margin-left: -6px;
  185. }
  186. .uni-combox__selector-empty,
  187. .uni-combox__selector-item {
  188. /* #ifdef APP-NVUE */
  189. display: flex;
  190. /* #endif */
  191. line-height: 36px;
  192. font-size: 14px;
  193. text-align: center;
  194. border-bottom: solid 1px #DDDDDD;
  195. margin: 0px 10px;
  196. }
  197. .uni-combox__selector-empty:last-child,
  198. .uni-combox__selector-item:last-child {
  199. border-bottom: none;
  200. }
  201. </style>