VueMonthlyPickerExample.vue 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <template>
  2. <div style="margin-top: 20px;">
  3. <h4>正常使用</h4>
  4. <month-picker v-model="selectedMonth" :placeholder="placeholder" @selected="monthSelected" />
  5. <p>当前选择:{{ selectedMonth }}</p>
  6. <h4>限制范围(前后六个月)</h4>
  7. <a-switch v-model:checked="isLimitRange" @change="rangeChange" />
  8. <month-picker
  9. v-model="selectedMonth" :placeholder="placeholder" :disabled-date="disabledDate"
  10. @selected="monthSelected"
  11. />
  12. <h4>禁用</h4>
  13. <month-picker v-model="selectedMonth" :placeholder="placeholder" :disabled="disabled" @selected="monthSelected" />
  14. </div>
  15. </template>
  16. <script setup>
  17. import { ref } from 'vue';
  18. import dayjs from 'dayjs';
  19. import MonthPicker from '@/vue-monthly-picker/src/VueMonthlyPicker.vue';
  20. const selectedMonth = ref('2025-06');
  21. const isLimitRange = ref(false);
  22. const min = ref(null);
  23. const max = ref(null);
  24. const placeholder = ref('请选择月份');
  25. const disabled = ref(true);
  26. // 月份变更
  27. const monthSelected = value => {
  28. console.log('月份变更:', value);
  29. };
  30. // 限制日期
  31. const disabledDate = current => {
  32. if (!current) return false;
  33. const date = dayjs(current);
  34. if (!min.value || !max.value) return false;
  35. return date < min.value.startOf('month') ||
  36. date > max.value.endOf('month');
  37. };
  38. // 限制范围
  39. const rangeChange = checked => {
  40. if (checked) {
  41. min.value = dayjs().subtract(6, 'month');
  42. max.value = dayjs().add(6, 'month');
  43. } else {
  44. min.value = null;
  45. max.value = null;
  46. }
  47. };
  48. </script>