DatePicker.vue 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <!--
  2. Sélecteur de dates avec Vuetify
  3. @see https://vuetifyjs.com/en/components/date-pickers/
  4. -->
  5. <template>
  6. <v-layout row wrap>
  7. <!--
  8. TODO: remplacer par <v-date-input> quand celui ci ne sera plus expérimental
  9. (@see https://vuetifyjs.com/en/components/date-inputs)
  10. -->
  11. <v-menu
  12. v-model="menu"
  13. :close-on-content-click="false"
  14. :nudge-right="40"
  15. lazy
  16. transition="scale-transition"
  17. offset-y
  18. :max-width="290"
  19. :min-width="290"
  20. :position-x="positionX"
  21. :position-y="positionY"
  22. >
  23. <template #activator="{ props: attrs }">
  24. <v-text-field
  25. v-model="displayDate"
  26. :label="label"
  27. :readonly="true"
  28. v-bind="attrs"
  29. prepend-inner-icon="far fa-calendar"
  30. variant="outlined"
  31. density="compact"
  32. />
  33. </template>
  34. <v-date-picker
  35. :model-value="modelValue"
  36. :locale="i18n.locale.value"
  37. no-title
  38. scrollable
  39. @update:model-value="updateDate"
  40. />
  41. </v-menu>
  42. </v-layout>
  43. </template>
  44. <script setup lang="ts">
  45. import { ref, computed, nextTick, watch, type PropType } from 'vue'
  46. import { useI18n } from 'vue-i18n'
  47. const props = defineProps({
  48. modelValue: Date,
  49. label: {
  50. type: String,
  51. default: '',
  52. },
  53. format: {
  54. type: String,
  55. default: null,
  56. },
  57. /**
  58. * Position du date-picker
  59. * @see https://vuetifyjs.com/en/api/v-menu/#props-position
  60. */
  61. position: {
  62. type: String as PropType<'left' | 'center' | 'right'>,
  63. default: 'center',
  64. },
  65. })
  66. const emit = defineEmits(['update:modelValue'])
  67. const i18n = useI18n()
  68. const menu = ref(false)
  69. const positionX = ref(0)
  70. const positionY = ref(0)
  71. const displayDate = computed({
  72. get: () => {
  73. if (!props.modelValue) return ''
  74. if (props.format) {
  75. return new Intl.DateTimeFormat(i18n.locale.value, {
  76. year: 'numeric',
  77. month: '2-digit',
  78. day: '2-digit',
  79. }).format(props.modelValue)
  80. }
  81. return props.modelValue.toLocaleDateString(i18n.locale.value)
  82. },
  83. set: () => {},
  84. })
  85. function updateDate(value: Date) {
  86. emit('update:modelValue', value)
  87. menu.value = false
  88. }
  89. function updatePosition() {
  90. nextTick(() => {
  91. const activator = document.querySelector('.v-menu__activator')
  92. if (activator) {
  93. const rect = activator.getBoundingClientRect()
  94. positionX.value = rect.left
  95. positionY.value = rect.bottom
  96. }
  97. })
  98. }
  99. watch(menu, (val) => {
  100. if (val) updatePosition()
  101. })
  102. </script>
  103. <style scoped>
  104. .v-menu__content {
  105. position: absolute !important;
  106. }
  107. </style>