uni-id-combox.vue 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. <template>
  2. <view class="uni-id-combox">
  3. <!--<view v-if="label" class="uni-id-combox__label" :style="labelStyle">
  4. <text>{{label}}</text>
  5. </view>-->
  6. <view class="uni-id-combox__input-box">
  7. <label class="uni-id-combox__input" @click="toggleSelector">{{inputVal}}</label>
  8. <!-- <input class="uni-id-combox__input" type="text" disabled="true" :placeholder="placeholder" v-model="inputVal" @input="onInput"/>-->
  9. <!-- @focus="onFocus" @blur="onBlur" />-->
  10. <label class="iconfont icon-triangle-down " style="color: grey;" @click="toggleSelector"></label>
  11. <!-- <uni-icons class="uni-id-combox__input-arrow" type="arrowdown" size="14" @click="toggleSelector"></uni-icons>-->
  12. <view class="uni-id-combox__selector" v-if="showSelector">
  13. <scroll-view scroll-y="true" class="uni-id-combox__selector-scroll">
  14. <view class="uni-id-combox__selector-empty" v-if="filterCandidatesLength === 0">
  15. <label>{{emptyTips}}</label>
  16. </view>
  17. <!-- <view class="uni-id-combox__selector-item" v-for="(item,index) in filterCandidates" :key="index" @click="onSelectorClick(index)">-->
  18. <view class="uni-id-combox__selector-item" v-for="(item,index) in candidates" :key="index" @click="onSelectorClick(index)">
  19. <label>{{item.value}}</label>
  20. <!-- <text>{{item.value}}</text>-->
  21. </view>
  22. </scroll-view>
  23. </view>
  24. </view>
  25. </view>
  26. </template>
  27. <script>
  28. /**
  29. * Combox 组合输入框
  30. * @description 组合输入框一般用于既可以输入也可以选择的场景
  31. * @tutorial https://ext.dcloud.net.cn/plugin?id=1261
  32. * @property {String} label 左侧文字
  33. * @property {String} labelWidth 左侧内容宽度
  34. * @property {String} placeholder 输入框占位符
  35. * @property {Array} candidates 候选项列表
  36. * @property {String} emptyTips 筛选结果为空时显示的文字
  37. * @property {String} value 组合框的值
  38. */
  39. export default {
  40. name: 'uniIdCombox',
  41. emits:['input','update:modelValue'],
  42. props: {
  43. label: {
  44. type: String,
  45. default: ''
  46. },
  47. labelWidth: {
  48. type: String,
  49. default: 'auto'
  50. },
  51. placeholder: {
  52. type: String,
  53. default: ''
  54. },
  55. candidates: {
  56. type: Array,
  57. default () {
  58. return []
  59. }
  60. },
  61. emptyTips: {
  62. type: String,
  63. default: '无匹配项'
  64. },
  65. // #ifndef VUE3
  66. value: {
  67. type: [String, Number],
  68. default: ''
  69. },
  70. // #endif
  71. // #ifdef VUE3
  72. modelValue: {
  73. type: [String, Number],
  74. default: ''
  75. },
  76. // #endif
  77. },
  78. data() {
  79. return {
  80. showSelector: false,
  81. inputVal: '',
  82. id:'',
  83. }
  84. },
  85. computed: {
  86. labelStyle() {
  87. if (this.labelWidth === 'auto') {
  88. return {}
  89. }
  90. return {
  91. width: this.labelWidth
  92. }
  93. },
  94. filterCandidates() {
  95. return this.candidates.filter((item) => {
  96. let ret = item.value.toString().indexOf(this.inputVal) > -1;
  97. //console.log('value:' +item.value + ' ret:' + ret);
  98. return ret
  99. })
  100. },
  101. filterCandidatesLength() {
  102. return this.filterCandidates.length
  103. }
  104. },
  105. watch: {
  106. // #ifndef VUE3
  107. value: {
  108. handler(newVal) {
  109. this.inputVal = newVal
  110. },
  111. immediate: true
  112. },
  113. // #endif
  114. // #ifdef VUE3
  115. modelValue: {
  116. handler(newVal) {
  117. this.inputVal = newVal
  118. },
  119. immediate: true
  120. },
  121. // #endif
  122. },
  123. methods: {
  124. toggleSelector() {
  125. this.showSelector = !this.showSelector
  126. },
  127. onFocus() {
  128. this.showSelector = true
  129. },
  130. onBlur() {
  131. setTimeout(() => {
  132. this.showSelector = false
  133. }, 153)
  134. },
  135. onSelectorClick(index) {
  136. this.inputVal = this.candidates[index].value;
  137. this.id = this.candidates[index].id;
  138. //this.filterCandidates[index].value
  139. this.showSelector = false
  140. this.$emit('input', this.inputVal)
  141. this.$emit('update:modelValue', {id:this.id,value:this.inputVal})
  142. },
  143. onInput() {
  144. setTimeout(() => {
  145. this.$emit('input', this.inputVal)
  146. this.$emit('update:modelValue', this.inputVal)
  147. })
  148. }
  149. }
  150. }
  151. </script>
  152. <style lang="scss" scoped>
  153. @import url("/static/font/iconfont.css");
  154. .uni-id-combox {
  155. ///* #ifndef APP-NVUE */
  156. display: flex;
  157. ///* #endif */
  158. height: 80rpx;
  159. flex-direction: row;
  160. align-items: center;
  161. // border-bottom: solid 1px #DDDDDD;
  162. }
  163. .uni-id-combox__label {
  164. font-size: 32rpx;
  165. line-height: 44rpx;
  166. padding-right: 20rpx;
  167. color: #999999;
  168. }
  169. .uni-id-combox__input-box {
  170. position: relative;
  171. ///* #ifndef APP-NVUE */
  172. display: flex;
  173. ////* #endif */
  174. //flex: 1;
  175. flex-direction: row;
  176. align-items: center;
  177. justify-content: center;
  178. width:100%
  179. //background-color: #007AFF;
  180. }
  181. .uni-id-combox__input {
  182. //flex: 1;
  183. font-size: 32rpx;
  184. height: 44rpx;
  185. line-height: 44rpx;
  186. //justify-content: flex-end;
  187. }
  188. .uni-id-combox__input-arrow {
  189. padding: 10rpx;
  190. }
  191. .uni-id-combox__selector {
  192. ///* #ifndef APP-NVUE */
  193. box-sizing: border-box;
  194. ///* #endif */
  195. position: absolute;
  196. top: 42rpx;
  197. left: 0;
  198. width: 100%;
  199. background-color: #FFFFFF;
  200. border-radius: 6rpx;
  201. box-shadow: #DDDDDD 4rpx 4rpx 8rpx, #DDDDDD -4rpx -4rpx 8rpx;
  202. z-index: 2;
  203. }
  204. .uni-id-combox__selector-scroll {
  205. ///* #ifndef APP-NVUE */
  206. max-height: 800rpx;
  207. box-sizing: border-box;
  208. ///* #endif */
  209. }
  210. .uni-id-combox__selector::before {
  211. ///* #ifndef APP-NVUE */
  212. content: '';
  213. ///* #endif */
  214. position: absolute;
  215. width: 0;
  216. height: 0;
  217. border-bottom: solid 6rpx #FFFFFF;
  218. border-right: solid 6rpx transparent;
  219. border-left: solid 6rpx transparent;
  220. left: 50%;
  221. top: -6rpx;
  222. margin-left: -6rpx;
  223. }
  224. .uni-id-combox__selector-empty,
  225. .uni-id-combox__selector-item {
  226. ///* #ifndef APP-NVUE */
  227. display: flex;
  228. cursor: pointer;
  229. ///* #endif */
  230. line-height: 72rpx;
  231. font-size: 28rpx;
  232. text-align: center;
  233. border-bottom: solid 1rpx #DDDDDD;
  234. margin: 0px 20rpx;
  235. justify-content: center;
  236. }
  237. .uni-id-combox__selector-empty:last-child,
  238. .uni-id-combox__selector-item:last-child {
  239. ///* #ifndef APP-NVUE */
  240. border-bottom: none;
  241. ///* #endif */
  242. }
  243. </style>