app-date-picker.vue 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <template>
  2. <view>
  3. <picker :mode="mode" :value="date" :start="startDate2" :end="endDate2" @change="bindDateChange">
  4. <slot>
  5. <view class="uni-input">
  6. <span v-if="placeholder && !date">{{placeholder}}</span>
  7. <span v-else>{{date}}</span>
  8. <span></span>
  9. </view>
  10. </slot>
  11. </picker>
  12. </view>
  13. </template>
  14. <script>
  15. export default {
  16. name: 'app-date-picker',
  17. props: {
  18. mode: {
  19. type: String,
  20. default: 'date'
  21. },
  22. placeholder: {
  23. type: String,
  24. default: null
  25. },
  26. dateVal: {
  27. type: String,
  28. default: ''
  29. },
  30. startDate: {
  31. type: String,
  32. default: ''
  33. },
  34. endDate: {
  35. type: String,
  36. default: ''
  37. }
  38. },
  39. data() {
  40. return {
  41. date: null
  42. }
  43. },
  44. watch: {
  45. dateVal(val) {
  46. this.date = val
  47. }
  48. },
  49. computed: {
  50. startDate2() {
  51. console.log(this.getDate('start'))
  52. return this.getDate('start')
  53. },
  54. endDate2() {
  55. return this.getDate('end')
  56. }
  57. },
  58. methods: {
  59. bindDateChange: function(e) {
  60. let val = e.target.value
  61. this.date = val
  62. console.log(val)
  63. this.$emit('update:dateVal', val)
  64. this.$emit('change', val)
  65. },
  66. getDate(type) {
  67. const date = new Date()
  68. let year = date.getFullYear()
  69. let month = date.getMonth() + 1
  70. let day = date.getDate()
  71. if (type === 'start') {
  72. year = this.startDate
  73. } else if (type === 'end') {
  74. year = this.endDate
  75. }
  76. month = month > 9 ? month : '0' + month
  77. day = day > 9 ? day : '0' + day
  78. return `${year}-${month}-${day}`
  79. }
  80. }
  81. }
  82. </script>
  83. <style scoped>
  84. </style>