DateRangePicker.vue 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <template>
  2. <!-- @see https://vue3datepicker.com/props/modes/#multi-calendars -->
  3. <VueDatePicker
  4. :model-value="modelValue"
  5. range
  6. multi-calendars
  7. :auto-apply="autoApply"
  8. :locale="i18n.locale.value"
  9. :format-locale="fnsLocale"
  10. :format="dateFormatPattern"
  11. :start-date="today"
  12. :teleport="true"
  13. :alt-position="dateRangePickerAltPosition"
  14. :enable-time-picker="false"
  15. close-on-scroll
  16. text-input
  17. :select-text="$t('select')"
  18. :cancel-text="$t('cancel')"
  19. input-class-name="date-range-picker-input"
  20. @update:model-value="updateDateTimeRange"
  21. class="date-range-picker"
  22. :style="style"
  23. />
  24. </template>
  25. <script setup lang="ts">
  26. import DateUtils, {supportedLocales} from "~/services/utils/dateUtils";
  27. import {PropType} from "@vue/runtime-core";
  28. const props = defineProps({
  29. modelValue: {
  30. type: Array as PropType<Array<Date> | null>,
  31. required: false,
  32. default: null
  33. },
  34. maxHeight: {
  35. type: Number,
  36. required: false,
  37. default: null
  38. }
  39. })
  40. const emit = defineEmits(['update:modelValue'])
  41. const autoApply = false
  42. const updateDateTimeRange = (value: [string, string]) => {
  43. emit('update:modelValue', value)
  44. }
  45. const i18n = useI18n()
  46. const fnsLocale = DateUtils.getFnsLocale(i18n.locale.value as supportedLocales)
  47. const dateFormatPattern = DateUtils.getShortFormatPattern(i18n.locale.value as supportedLocales)
  48. const today = new Date()
  49. let style = '';
  50. if (props.maxHeight !== null) {
  51. style += 'height: ' + props.maxHeight + 'px;max-height: ' + props.maxHeight + 'px;'
  52. }
  53. /**
  54. * Recalcule la position du panneau de sélection des dates si trop près du bord droit de l'écran
  55. * @param el
  56. */
  57. const dateRangePickerAltPosition = (el: HTMLElement) => {
  58. let xOffset = 0
  59. const fullWidth = 500
  60. const rightPadding = 30
  61. const rect = el.getBoundingClientRect()
  62. if ((rect.left + fullWidth + rightPadding) > window.innerWidth) {
  63. xOffset = window.innerWidth - (rect.left + fullWidth + rightPadding)
  64. }
  65. return {
  66. top: rect.bottom,
  67. left: rect.left + xOffset
  68. }
  69. }
  70. </script>
  71. <style scoped lang="scss">
  72. :deep(div[role="textbox"]) {
  73. height: 100% !important;
  74. max-height: 100% !important;
  75. }
  76. :deep(.dp__input_wrap) {
  77. height: 100% !important;
  78. max-height: 100% !important;
  79. }
  80. :deep(.date-range-picker-input) {
  81. height: 100% !important;
  82. max-height: 100% !important;
  83. }
  84. </style>