uni-right-combox.vue 6.2 KB

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