di-region-picker.vue 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <template>
  2. <view class="di-region-picker">
  3. <u-input ref='diRegion' v-model="tempVal" @click="show = true" disabled :select-open="show"
  4. type="select" :placeholder="placeholder" />
  5. <u-picker confirm-color="#18b566" v-model="show" :params="pickerParams" mode="region" @confirm="handlePicker" :default-region="defaultRegion"></u-picker>
  6. </view>
  7. </template>
  8. <script>
  9. /**
  10. * di-region-picker 区域选择器
  11. * @description 基于u-picker mode='region'模式
  12. * @property {String} value 可以使用v-model双向绑定,省市区请使用“-”分割,提交后自行处理
  13. * @property {String} placeholder 提示信息
  14. * @property {Boolean} city 是否选择市 默认true
  15. * @property {Boolean} area 是否选择区 默认true
  16. * @event {Function} confirm 点击确定按钮,传递出所选的完整的label-value值对象
  17. */
  18. export default {
  19. data() {
  20. return {
  21. show: false,
  22. tempVal: this.value
  23. }
  24. },
  25. watch: {
  26. value(val) {
  27. this.tempVal = val
  28. }
  29. },
  30. methods: {
  31. /**
  32. * 确认选择
  33. *
  34. * @param {Object} value
  35. */
  36. handlePicker(value) {
  37. const{province, city, area} = value
  38. const result = [province.label]
  39. city && result.push(city.label)
  40. area && result.push(area.label)
  41. this.tempVal = result.join('-')
  42. this.handleInputEvent(this.tempVal)
  43. this.$emit('confirm', value)
  44. },
  45. /**
  46. * 发送input消息
  47. * 过一个生命周期再发送事件给u-form-item,否则this.$emit('input')更新了父组件的值
  48. * 但是微信小程序上 尚未更新到u-form-item,导致获取的值为空
  49. */
  50. handleInputEvent(value) {
  51. this.$emit('input', value)
  52. this.$nextTick(function(){
  53. // 将当前的值发送到 u-form-item 进行校验
  54. this.$refs.diRegion.dispatch('u-form-item', 'on-form-change', value);
  55. })
  56. }
  57. },
  58. computed: {
  59. pickerParams(){
  60. return {
  61. province: true,
  62. city: this.city,
  63. area: this.area
  64. }
  65. },
  66. defaultRegion() {
  67. return this.value && typeof this.value === 'string' && this.value.split('-') || this.value || []
  68. }
  69. },
  70. props: {
  71. placeholder: {
  72. type: String,
  73. default: '请选择'
  74. },
  75. value: {
  76. type: String,
  77. require: true
  78. },
  79. city: {
  80. type: Boolean,
  81. default: true
  82. },
  83. area: {
  84. type: Boolean,
  85. default: true
  86. }
  87. }
  88. }
  89. </script>
  90. <style lang="scss">
  91. .di-region-picker {
  92. width: 100%;
  93. }
  94. </style>