Nenhuma Descrição

bjx-inputs.vue 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <template>
  2. <view class="bjx-inputs" :style="getStyle">
  3. <view class="bjx-background" @tap="isShow=false" v-show="isShow"></view>
  4. <view class="bjx-con">
  5. <slot name='left'></slot>
  6. <input :disabled="theDisabled" :placeholder="placeholder" v-model="theValue" @input="theInput" @tap='isShow=data.length' @focus="theFocus" @blur="theBlur" autocomplete="off" />
  7. <slot name='right'></slot>
  8. </view>
  9. <view class="bjx-select" v-show="show">
  10. <view class="data">
  11. <view class="select-item" :class="'item-'+overflow" v-for="(item, index) in data" :key="index" @tap="selectItem(item,index)">
  12. <view class="item-text" :class="{active: getItem(item) == theValue}">{{getItem(item)}}</view>
  13. </view>
  14. </view>
  15. <view class="item-close" @tap="isShow=false">收起</view>
  16. </view>
  17. </view>
  18. </template>
  19. <script>
  20. let fontUnit = 'upx'
  21. // #ifdef MP-WEIXIN
  22. fontUnit = 'rpx'
  23. // #endif
  24. export default {
  25. name: 'BjxInputs',
  26. props: {
  27. placeholder: {
  28. type: String,
  29. default: ''
  30. },
  31. value: {
  32. type: String,
  33. default: ''
  34. },
  35. itemKey: {
  36. type: String,
  37. default: ''
  38. },
  39. width: {
  40. type: String,
  41. default: '600'
  42. },
  43. disabled: {
  44. type: Boolean,
  45. default: false
  46. },
  47. timeDelay: {
  48. type: Number,
  49. default: 60
  50. },
  51. theStyle: {
  52. type: String,
  53. default: ''
  54. },
  55. overflow: {
  56. type: String,
  57. default: 'auto'
  58. },
  59. },
  60. data() {
  61. return {
  62. timer: null,
  63. data: [],
  64. isShow: false,
  65. theValue: this.value,
  66. theDisabled: this.disabled,
  67. }
  68. },
  69. watch: {
  70. value(val){
  71. this.theValue = val
  72. }
  73. },
  74. computed: {
  75. show(){
  76. return this.isShow && this.data.length
  77. },
  78. getStyle(){
  79. let style = this.theStyle.replace(/upx/g,fontUnit)
  80. let width = 'width:' + (!isNaN(Number(this.width)) ? this.width + fontUnit : this.width) + ';'
  81. return style + width
  82. },
  83. },
  84. methods: {
  85. showItem(){
  86. this.isShow = this.data.length > 0
  87. },
  88. theFocus(e){
  89. this.$emit('focus',e)
  90. },
  91. theBlur(e){
  92. this.$emit('blur',e)
  93. },
  94. theInput(e) {
  95. clearTimeout(this.timer)
  96. this.timer = setTimeout(()=>{
  97. this.$emit('list',{value: e.detail.value,callback: this.setData})
  98. },this.timeDelay)
  99. },
  100. getItem(item) {
  101. return this.itemKey && typeof item == 'object' ? item[this.itemKey] : item
  102. },
  103. selectItem(item) {
  104. this.isShow = false
  105. this.theValue = this.itemKey ? item[this.itemKey] : item
  106. this.$emit('select',item)
  107. },
  108. setData(data) {
  109. if(data) {
  110. this.isShow = true
  111. this.data = data
  112. }
  113. },
  114. setValue(value){
  115. this.theValue = value
  116. },
  117. setDisabled(value){
  118. this.theDisabled = value
  119. },
  120. },
  121. }
  122. </script>
  123. <style lang="scss" scoped>
  124. .bjx-inputs{
  125. max-width: 100%;
  126. position: relative;
  127. .bjx-background{position: fixed;top:0;left:0;width: 750upx;height: 100%;}
  128. .bjx-con{display: flex;align-items: center;justify-content: center;
  129. input{flex: 1;margin: 0 6upx;}
  130. }
  131. .bjx-select {
  132. border: 1px solid #f3f3f4;
  133. position: absolute;
  134. z-index: 999;
  135. background: #fff;
  136. width: 100%;
  137. .data{
  138. max-height: 600upx;
  139. padding: 10upx;
  140. overflow: auto;
  141. .select-item {width: 100%;
  142. .item-text{padding: 10upx 0;}
  143. .active{font-weight: bold;}
  144. }
  145. .item-auto{overflow: auto;
  146. .item-text{width: max-content;}
  147. }
  148. .item-hide .item-text{
  149. overflow: hidden;
  150. text-overflow: ellipsis;
  151. white-space: normal;
  152. }
  153. }
  154. .item-close {
  155. padding: 20upx;
  156. text-align: center;
  157. font-size: 32upx;
  158. border-top: 1px solid #f3f3f4;
  159. color:#8F8F94;
  160. }
  161. }
  162. }
  163. </style>