DateRangePicker.vue 2.3 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"
  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 { fr } from 'date-fns/locale';
  27. import DateUtils, {supportedLocales} from "~/services/utils/dateUtils";
  28. const props = defineProps({
  29. modelValue: {
  30. type: Array,
  31. required: true
  32. },
  33. maxHeight: {
  34. type: Number,
  35. required: false,
  36. default: null
  37. }
  38. })
  39. const emit = defineEmits(['update:modelValue'])
  40. const autoApply = false
  41. const updateDateTimeRange = (value: [string, string]) => {
  42. emit('update:modelValue', value)
  43. }
  44. const i18n = useI18n()
  45. const fnsLocale = DateUtils.getFnsLocale(i18n.locale.value as supportedLocales)
  46. const dateFormatPattern = DateUtils.getShortFormatPattern(i18n.locale.value as supportedLocales)
  47. const today = new Date()
  48. let style = '';
  49. if (props.maxHeight !== null) {
  50. style += 'height: ' + props.maxHeight + 'px;max-height: ' + props.maxHeight + 'px;'
  51. }
  52. /**
  53. * Recalcule la position du panneau de sélection des dates si trop près du bord droit de l'écran
  54. * @param el
  55. */
  56. const dateRangePickerAltPosition = (el: HTMLElement) => {
  57. let xOffset = 0
  58. const fullWidth = 500
  59. const rightPadding = 30
  60. const rect = el.getBoundingClientRect()
  61. if ((rect.left + fullWidth + rightPadding) > window.innerWidth) {
  62. xOffset = window.innerWidth - (rect.left + fullWidth + rightPadding)
  63. }
  64. return {
  65. top: rect.bottom,
  66. left: rect.left + xOffset
  67. }
  68. }
  69. </script>
  70. <style scoped lang="scss">
  71. :deep(div[role="textbox"]) {
  72. height: 100% !important;
  73. max-height: 100% !important;
  74. }
  75. :deep(.dp__input_wrap) {
  76. height: 100% !important;
  77. max-height: 100% !important;
  78. }
  79. :deep(.date-range-picker-input) {
  80. height: 100% !important;
  81. max-height: 100% !important;
  82. }
  83. </style>