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" @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. export default {
  25. name: 'uniCombox',
  26. components: {
  27. uniIcons
  28. },
  29. props: {
  30. label: {
  31. type: String,
  32. default: ''
  33. },
  34. labelWidth: {
  35. type: String,
  36. default: 'auto'
  37. },
  38. placeholder: {
  39. type: String,
  40. default: ''
  41. },
  42. candidates: {
  43. type: Array,
  44. default () {
  45. return []
  46. }
  47. },
  48. emptyTips: {
  49. type: String,
  50. default: '无匹配项'
  51. },
  52. value: {
  53. type: String,
  54. default: ''
  55. }
  56. },
  57. data() {
  58. return {
  59. showSelector: false,
  60. inputVal: ''
  61. }
  62. },
  63. computed: {
  64. labelStyle() {
  65. if (this.labelWidth === 'auto') {
  66. return {}
  67. }
  68. return {
  69. width: this.labelWidth
  70. }
  71. },
  72. filterCandidates() {
  73. return this.candidates.filter((item) => {
  74. return item.indexOf(this.inputVal) > -1
  75. })
  76. },
  77. filterCandidatesLength() {
  78. return this.filterCandidates.length
  79. }
  80. },
  81. watch: {
  82. value: {
  83. handler(newVal) {
  84. this.inputVal = newVal
  85. },
  86. immediate: true
  87. }
  88. },
  89. methods: {
  90. toggleSelector() {
  91. this.showSelector = !this.showSelector
  92. },
  93. onFocus() {
  94. this.showSelector = true
  95. },
  96. onBlur() {
  97. setTimeout(() => {
  98. this.showSelector = false
  99. }, 50)
  100. },
  101. onSelectorClick(index) {
  102. this.inputVal = this.filterCandidates[index]
  103. this.showSelector = false
  104. this.$emit('input', this.inputVal)
  105. },
  106. onInput() {
  107. setTimeout(() => {
  108. this.$emit('input', this.inputVal)
  109. })
  110. }
  111. }
  112. }
  113. </script>
  114. <style scoped>
  115. .uni-combox {
  116. /* #ifndef APP-NVUE */
  117. display: flex;
  118. /* #endif */
  119. height: 40px;
  120. flex-direction: row;
  121. align-items: center;
  122. /* border-bottom: solid 1px #DDDDDD;
  123. */
  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>