You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

date-picker.vue 2.0 KiB

4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <template>
  2. <l-label :class="className" :style="style" :arrow="!disabled" :required="required" :title="title">
  3. <!-- #ifndef MP-DINGTALK -->
  4. <picker @change="change" :fields="fields" :disabled="disabled" :value="value" :start="start" :end="end" mode="date">
  5. <view class="picker">{{ value || displayPlaceholder }}</view>
  6. </picker>
  7. <!-- #endif -->
  8. <!-- #ifdef MP-DINGTALK -->
  9. <text @click="click">{{ value || displayPlaceholder }}</text>
  10. <!-- #endif -->
  11. </l-label>
  12. </template>
  13. <script>
  14. export default {
  15. name: 'l-date-picker',
  16. props: {
  17. title: {},
  18. start: { default: '1900-01-01' },
  19. end: { default: '2100-01-01' },
  20. fields: { default: 'day' },
  21. disabled: {},
  22. placeholder: {},
  23. required: {},
  24. value: {}
  25. },
  26. created() {
  27. console.log(this.end)
  28. },
  29. methods: {
  30. click(e) {
  31. if (this.disabled) {
  32. return
  33. }
  34. this.$emit('click')
  35. // #ifdef MP-DINGTALK
  36. const getCurrent = () => {
  37. const now = new Date()
  38. const year = now.getFullYear().toString()
  39. const month = (now.getMonth() + 1).toString().padStart(2, '0')
  40. const date = now
  41. .getDate()
  42. .toString()
  43. .padStart(2, '0')
  44. return `${year}-${month}-${date}`
  45. }
  46. const currentDate = this.value ? this.value : getCurrent()
  47. this.$emit('open')
  48. dd.datePicker({
  49. currentDate,
  50. format: 'yyyy-MM-dd',
  51. success: ({ date }) => {
  52. if (date) {
  53. this.$emit('input', date)
  54. this.$emit('change', date)
  55. }
  56. },
  57. complete: () => {
  58. this.$emit('close')
  59. }
  60. })
  61. // #endif
  62. },
  63. change(e) {
  64. this.$emit('input', e.detail.value)
  65. this.$emit('change', e.detail.value)
  66. }
  67. },
  68. computed: {
  69. displayPlaceholder() {
  70. if (this.disabled) {
  71. return ''
  72. }
  73. if (this.placeholder) {
  74. return this.placeholder
  75. }
  76. return this.title ? `请选择${this.title}` : '请选择日期…'
  77. }
  78. }
  79. }
  80. </script>