wk-field-position.vue 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <template>
  2. <view class="wk-field wk-field-position">
  3. <view v-if="_label" class="wk-field__label">
  4. <view v-if="field.isNull === 1" class="line" />
  5. {{ _label }}
  6. </view>
  7. <view class="wk-field__body" @click.stop="handleChoose">
  8. <view class="wk-field__body-core">
  9. {{ areaValueStr }}
  10. </view>
  11. <text class="wk wk-arrow-right icon-right" />
  12. </view>
  13. <view
  14. v-if="field.precisions === 1"
  15. class="wk-field__body detail-addr">
  16. <textarea
  17. ref="core"
  18. v-model="detailAddr"
  19. :maxlength="500"
  20. :disabled="field.disabled"
  21. type="text"
  22. placeholder="详细地址"
  23. placeholder-class="wk-placeholder placeholder"
  24. class="wk-field__body-core"
  25. @input="changeText" />
  26. </view>
  27. <uni-popup ref="popup" type="picker">
  28. <wk-area-picker
  29. v-model="areaValue"
  30. :precisions="areaPrecisions"
  31. @select="handleSelect" />
  32. </uni-popup>
  33. </view>
  34. </template>
  35. <script>
  36. import mixins from './mixins'
  37. export default {
  38. name: 'WkFieldPosition',
  39. mixins: [mixins],
  40. data() {
  41. return {
  42. areaValue: [],
  43. detailAddr: ''
  44. }
  45. },
  46. computed: {
  47. areaValueStr() {
  48. return this.areaValue.map(o => o.name).join('')
  49. },
  50. // 省市区精度
  51. areaPrecisions() {
  52. if (this.field.precisions === 3) return 2 // 省市
  53. if (this.field.precisions === 4) return 1 // 省
  54. return 3 // 省市区
  55. }
  56. },
  57. watch: {
  58. formValue: {
  59. handler() {
  60. // this.checkedStatus = Boolean(Number(this.formValue))
  61. if (this.$isEmpty(this.formValue)) {
  62. this.areaValue = []
  63. this.detailAddr = ''
  64. return
  65. }
  66. this.areaValue = this.formValue.filter(o => o.id !== 4)
  67. const findRes = this.formValue.find(o => o.id === 4)
  68. if (findRes) {
  69. this.detailAddr = findRes.name
  70. }
  71. },
  72. immediate: true,
  73. deep: true
  74. }
  75. },
  76. methods: {
  77. handleChoose() {
  78. if (this.field.disabled) {
  79. if (this.config && this.config.disabledMsg) {
  80. this.$toast(this.config.disabledMsg)
  81. }
  82. return
  83. }
  84. this.$refs.popup.open()
  85. },
  86. handleSelect(data, next) {
  87. this.formValue = data
  88. this.emitValue()
  89. next()
  90. },
  91. changeText() {
  92. const regStr = /[\ud800-\udbff]|[\udc00-\udfff]/g;
  93. let _str = this.detailAddr
  94. if (regStr.test(_str)) {
  95. _str = _str.replace(regStr, '');
  96. }
  97. this.$nextTick(() => {
  98. this.detailAddr = _str
  99. this.emitValue()
  100. })
  101. },
  102. emitValue() {
  103. if (this.field.precisions === 1) {
  104. const findRes = this.formValue.find(o => o.id === 4)
  105. if (findRes) {
  106. findRes.name = this.detailAddr
  107. } else {
  108. this.formValue.push({
  109. code: '',
  110. name: this.detailAddr,
  111. id: 4
  112. })
  113. }
  114. }
  115. this.$emit('input', this.formValue)
  116. this.$emit('change', {
  117. index: this.index,
  118. field: this.field,
  119. value: this.formValue
  120. })
  121. }
  122. }
  123. }
  124. </script>
  125. <style scoped lang="scss">
  126. @import './wkField.scss';
  127. .detail-addr {
  128. margin-top: 15rpx;
  129. .wk-field__body-core {
  130. height: 150rpx;
  131. }
  132. }
  133. </style>