DateRangePicker.vue 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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(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 lang="scss">
  72. // @see https://vue3datepicker.com/customization/theming/
  73. // [!] Sass variables overriding does not work in scoped mode
  74. .dp__theme_light, .dp__theme_dark {
  75. --dp-background-color: #ffffff;
  76. --dp-text-color: #212121;
  77. --dp-hover-color: #f3f3f3;
  78. --dp-hover-text-color: #212121;
  79. --dp-hover-icon-color: #959595;
  80. --dp-primary-color: rgb(var(--v-theme-primary)) !important;
  81. --dp-primary-text-color: rgb(var(--v-theme-on-primary)) !important;
  82. --dp-secondary-color: rgb(var(--v-theme-neutral-strong)) !important;
  83. --dp-border-color: #ddd;
  84. --dp-menu-border-color: #ddd;
  85. --dp-border-color-hover: #aaaeb7;
  86. --dp-disabled-color: #f6f6f6;
  87. --dp-scroll-bar-background: #f3f3f3;
  88. --dp-scroll-bar-color: #959595;
  89. --dp-success-color: rgb(var(--v-theme-success)) !important;
  90. --dp-success-color-disabled: rgb(var(--v-theme-neutral-strong)) !important;
  91. --dp-icon-color: #959595;
  92. --dp-danger-color: #ff6f60;
  93. --dp-highlight-color: rgba(25, 118, 210, 0.1);
  94. }
  95. .date-range-picker {
  96. div[role="textbox"] {
  97. height: 100% !important;
  98. max-height: 100% !important;
  99. }
  100. .dp__input_wrap {
  101. height: 100% !important;
  102. max-height: 100% !important;
  103. }
  104. .date-range-picker-input {
  105. height: 100% !important;
  106. max-height: 100% !important;
  107. }
  108. }
  109. </style>