uni-combox.vue 4.5 KB

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