di-calendar-picker.vue 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <template>
  2. <view class="di-calendar-picker">
  3. <u-input ref='diCalendar' v-model="tempVal" @click="show = true" disabled :select-open="show"
  4. type="select" :placeholder="placeholder" />
  5. <u-calendar v-model="show" :mode="mode" @change="handleSelect" btn-type="success"
  6. :active-bg-color="activeBgcolor" :range-color="rangeColor" :range-bg-color="rangeBgcolor" safe-area-inset-bottom z-index="99999"/>
  7. </view>
  8. </template>
  9. <script>
  10. /**
  11. * di-calendar-picker 时间选择器
  12. * @description yyyy-MM-dd格式的选择
  13. * @property {String} value 可以使用v-model双向绑定
  14. * @property {String} placeholder 提示信息
  15. * @property {String} active-bg-color 激活的背景色
  16. * @property {String} range-bg-color 激活范围的背景色
  17. * @property {String} range-color 激活范围的字体颜色
  18. * @property {String} mode = [date|range] 模式选择,"date"-日期模式(默认),"range"-选择日期范围
  19. * @event {Function} confirm 点击确定按钮,传递出所选的完整的时间对象
  20. */
  21. export default {
  22. data() {
  23. return {
  24. show: false,
  25. tempVal: this.value
  26. }
  27. },
  28. watch: {
  29. value(val) {
  30. this.tempVal = val
  31. }
  32. },
  33. methods: {
  34. /**
  35. * 确认选择
  36. *
  37. * @param {Object} value
  38. */
  39. handleSelect(value) {
  40. let result = ''
  41. if(this.mode === 'range') {
  42. const { startDate, endDate} = value
  43. result = [startDate, endDate].join('~')
  44. } else {
  45. result = value.result
  46. }
  47. this.tempVal = result
  48. this.handleInputEvent(result)
  49. this.$emit('confirm', value)
  50. },
  51. /**
  52. * 发送input消息
  53. * 过一个生命周期再发送事件给u-form-item,否则this.$emit('input')更新了父组件的值
  54. * 但是微信小程序上 尚未更新到u-form-item,导致获取的值为空
  55. */
  56. handleInputEvent(value) {
  57. this.$emit('input', value)
  58. this.$nextTick(function(){
  59. // 将当前的值发送到 u-form-item 进行校验
  60. this.$refs.diCalendar.dispatch('u-form-item', 'on-form-change', value);
  61. })
  62. }
  63. },
  64. props: {
  65. placeholder: {
  66. type: String,
  67. default: '请选择日期'
  68. },
  69. value: {
  70. type: String,
  71. require: true
  72. },
  73. mode: {
  74. type: String,
  75. default: 'date'
  76. },
  77. activeBgcolor: {
  78. type: String,
  79. default: '#19be6b'
  80. },
  81. rangeBgcolor: {
  82. type: String,
  83. default: '#dbf1e1'
  84. },
  85. rangeColor: {
  86. type: String,
  87. default: '#18b566'
  88. }
  89. }
  90. }
  91. </script>
  92. <style lang="scss">
  93. .di-calendar-picker {
  94. width: 100%;
  95. }
  96. </style>