DateTimePicker.vue 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <template>
  2. <v-row>
  3. <v-col cols="12" md="6">
  4. <v-text-field
  5. :model-value="dateModel ? date.format(dateModel, 'fullDateWithWeekday') : undefined"
  6. :label="$t('choose_day')"
  7. prepend-icon="fas fa-calendar"
  8. :rules="rules"
  9. :error="error || !!fieldViolations"
  10. :error-messages="
  11. errorMessage || (fieldViolations ? $t(fieldViolations) : '')
  12. "
  13. readonly
  14. >
  15. <v-menu
  16. v-model="showMenuDate"
  17. :close-on-content-click="false"
  18. activator="parent"
  19. min-width="0"
  20. >
  21. <v-date-picker
  22. :model-value="dateModel"
  23. @update:model-value="onUpdateDate($event)"
  24. />
  25. </v-menu>
  26. </v-text-field>
  27. </v-col>
  28. <v-col cols="12" md="6">
  29. <v-text-field
  30. :model-value="time"
  31. :label="$t('choose_hour')"
  32. prepend-icon="fas fa-clock"
  33. :rules="rules"
  34. :error="error || !!fieldViolations"
  35. :error-messages="
  36. errorMessage || (fieldViolations ? $t(fieldViolations) : '')
  37. "
  38. readonly
  39. >
  40. <v-menu
  41. v-model="showMenuTime"
  42. :close-on-content-click="false"
  43. activator="parent"
  44. min-width="0"
  45. >
  46. <v-time-picker
  47. :model-value="time"
  48. format="24hr"
  49. @update:model-value="onUpdateTime($event)"
  50. scrollable
  51. />
  52. </v-menu>
  53. </v-text-field>
  54. </v-col>
  55. </v-row>
  56. </template>
  57. <script setup lang="ts">
  58. import type { PropType, Ref } from 'vue'
  59. import { ref } from 'vue'
  60. import { useFieldViolation } from '~/composables/form/useFieldViolation'
  61. import { useDate } from 'vuetify'
  62. import DateUtils from "~/services/utils/dateUtils";
  63. const props = defineProps({
  64. /**
  65. * v-model
  66. */
  67. modelValue: {
  68. type: String as PropType<Date | string | null>,
  69. required: false,
  70. default: null,
  71. },
  72. /**
  73. * Nom de la propriété d'une entité lorsque l'input concerne cette propriété
  74. * - Utilisé par la validation
  75. * - Laisser null si le champ ne s'applique pas à une entité
  76. */
  77. field: {
  78. type: String,
  79. required: false,
  80. default: null,
  81. },
  82. /**
  83. * Label du champ
  84. * Si non défini, c'est le nom de propriété qui est utilisé
  85. */
  86. label: {
  87. type: String,
  88. required: false,
  89. default: null,
  90. },
  91. /**
  92. * Définit si le champ est en lecture seule
  93. */
  94. readonly: {
  95. type: Boolean,
  96. required: false,
  97. },
  98. /**
  99. * Règles de validation
  100. * @see https://vuetify.cn/en/components/forms/#validation-with-submit-clear
  101. */
  102. rules: {
  103. type: Array,
  104. required: false,
  105. default: () => [],
  106. },
  107. /**
  108. * Le champ est-il actuellement en état d'erreur
  109. */
  110. error: {
  111. type: Boolean,
  112. required: false,
  113. },
  114. /**
  115. * Si le champ est en état d'erreur, quel est le message d'erreur?
  116. */
  117. errorMessage: {
  118. type: String,
  119. required: false,
  120. default: null,
  121. }
  122. })
  123. const showMenuTime = ref(false)
  124. const showMenuDate = ref(false)
  125. const date = useDate()
  126. const { fieldViolations, updateViolationState } = useFieldViolation(props.field)
  127. const dateModel = computed(()=> props.modelValue ? new Date(props.modelValue) : undefined)
  128. const time = computed(()=> props.modelValue ? date.format(new Date(props.modelValue),'fullTime24h') : undefined)
  129. const emit = defineEmits(['update:model-value'])
  130. const onUpdateDate = (event: string) => {
  131. updateViolationState()
  132. let date = DateUtils.combineDateAndTime(event, time.value)
  133. emit('update:model-value', date.toISOString())
  134. }
  135. const onUpdateTime = (event: string) => {
  136. updateViolationState()
  137. let date = DateUtils.combineDateAndTime(dateModel.value, event)
  138. emit('update:model-value', date.toISOString())
  139. }
  140. onUnmounted(() => {
  141. updateViolationState()
  142. })
  143. </script>
  144. <style scoped lang="scss">
  145. </style>