DateRangePicker.vue 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 type { 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(
  48. i18n.locale.value as supportedLocales,
  49. )
  50. const today = new Date()
  51. let style = ''
  52. if (props.maxHeight !== null) {
  53. style +=
  54. 'height: ' + props.maxHeight + 'px;max-height: ' + props.maxHeight + 'px;'
  55. }
  56. /**
  57. * Recalcule la position du panneau de sélection des dates si trop près du bord droit de l'écran
  58. * @param el
  59. */
  60. const dateRangePickerAltPosition = (el: HTMLElement) => {
  61. let xOffset = 0
  62. const fullWidth = 500
  63. const rightPadding = 30
  64. const rect = el.getBoundingClientRect()
  65. if (rect.left + fullWidth + rightPadding > window.innerWidth) {
  66. xOffset = window.innerWidth - (rect.left + fullWidth + rightPadding)
  67. }
  68. return {
  69. top: rect.bottom,
  70. left: rect.left + xOffset,
  71. }
  72. }
  73. </script>
  74. <style lang="scss">
  75. // @see https://vue3datepicker.com/customization/theming/
  76. // [!] Sass variables overriding does not work in scoped mode
  77. .dp__theme_light,
  78. .dp__theme_dark {
  79. --dp-background-color: #ffffff;
  80. --dp-text-color: #212121;
  81. --dp-hover-color: #f3f3f3;
  82. --dp-hover-text-color: #212121;
  83. --dp-hover-icon-color: #959595;
  84. --dp-primary-color: rgb(var(--v-theme-primary)) !important;
  85. --dp-primary-text-color: rgb(var(--v-theme-on-primary)) !important;
  86. --dp-secondary-color: rgb(var(--v-theme-neutral-strong)) !important;
  87. --dp-border-color: #ddd;
  88. --dp-menu-border-color: #ddd;
  89. --dp-border-color-hover: #aaaeb7;
  90. --dp-disabled-color: #f6f6f6;
  91. --dp-scroll-bar-background: #f3f3f3;
  92. --dp-scroll-bar-color: #959595;
  93. --dp-success-color: rgb(var(--v-theme-success)) !important;
  94. --dp-success-color-disabled: rgb(var(--v-theme-neutral-strong)) !important;
  95. --dp-icon-color: #959595;
  96. --dp-danger-color: #ff6f60;
  97. --dp-highlight-color: rgba(25, 118, 210, 0.1);
  98. }
  99. .date-range-picker {
  100. div {
  101. height: 100% !important;
  102. max-height: 100% !important;
  103. }
  104. .dp__input_wrap {
  105. height: 100% !important;
  106. max-height: 100% !important;
  107. }
  108. .date-range-picker-input {
  109. height: 100% !important;
  110. max-height: 100% !important;
  111. }
  112. }
  113. </style>