uni-combox.vue 4.4 KB

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