BaseDatePicker.vue 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <script setup>
  2. import { ref, computed } from 'vue';
  3. import { c, NPopover } from 'naive-ui';
  4. import VueDatePicker from '@vuepic/vue-datepicker';
  5. import dayjs from 'dayjs';
  6. const dateValue = ref(null);
  7. const popoverTriggerRef = ref(null);
  8. const modelValue = defineModel('value');
  9. const modelFormatValue = defineModel('formatValue');
  10. import { addMonths, getMonth, getYear, subMonths } from 'date-fns';
  11. const props = defineProps({
  12. isNoInp: {
  13. type: Boolean,
  14. default: false
  15. },
  16. dark: {
  17. type: Boolean,
  18. default: false
  19. },
  20. disabledMonth: {
  21. type: Array,
  22. default:() => []
  23. }
  24. })
  25. const onSelectMonth = (time) => {
  26. const month = time.month < 9 ? `0${time.month + 1}` : time.month + 1;
  27. const timeStr = `${time.year}-${month}`;
  28. const date = dayjs(timeStr, 'YYYY-MM');
  29. const timestamp = date.valueOf();
  30. dateValue.value = time;
  31. modelValue.value = timestamp;
  32. modelFormatValue.value = timeStr;
  33. setTimeout(() => popoverTriggerRef.value.setShow(false), 200);
  34. }
  35. // const allowedDates = (val) => {
  36. // console.log(val);
  37. // return [ new Date()]
  38. // }
  39. const allowedDates = computed(() => {
  40. console.log(new Date());
  41. const a = dayjs('2024-4', 'YYYY-M').format()
  42. console.log(a);
  43. return [
  44. a
  45. ];
  46. });
  47. // const allowedDates = (date) => {
  48. // // 获取当前日期的月份
  49. // const month = date.getMonth(); // 月份是从 0 开始的,0 代表一月
  50. // // 禁用特定月份,例如 3 月和 4 月
  51. // return ![3, 4].includes(month);
  52. // };
  53. const filters = computed(() => {
  54. const currentDate = new Date()
  55. return {
  56. months: Array.from(Array(3).keys())
  57. .map((item) => {
  58. console.log( getMonth(addMonths(currentDate, item + 1)) );
  59. return getMonth(addMonths(currentDate, item + 1))
  60. })
  61. }
  62. })
  63. const disabledDates = computed(() => {
  64. const today = new Date();
  65. const tomorrow = new Date(today)
  66. tomorrow.setDate(tomorrow.getDate() + 1)
  67. const afterTomorrow = new Date(tomorrow);
  68. afterTomorrow.setDate(tomorrow.getDate() + 1);
  69. console.log( tomorrow );
  70. const date = dayjs('2024-06', 'YYYY-MM');
  71. return [tomorrow, date]
  72. })
  73. </script>
  74. <template>
  75. <NPopover trigger="click" raw :animated="false" ref="popoverTriggerRef">
  76. <template #trigger>
  77. <slot></slot>
  78. </template>
  79. <div>
  80. <VueDatePicker month-picker inline locale="zh" v-model="dateValue"
  81. ref="datePickerRef" :dark="dark" :allowed-dates="allowedDates">
  82. <template #action-row></template>
  83. </VueDatePicker>
  84. </div>
  85. </NPopover>
  86. </template>
  87. <style lang="scss" scoped>
  88. :deep(.dp__overlay_cell_active) {
  89. background-color: #2454FF;
  90. }
  91. </style>