uni-combox.vue 3.9 KB

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