DatePicker.vue 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <!--
  2. Sélecteur de dates
  3. @see https://vuetifyjs.com/en/components/date-pickers/
  4. -->
  5. <template>
  6. <main>
  7. <v-menu
  8. v-model="dateOpen"
  9. :close-on-content-click="false"
  10. :nudge-right="40"
  11. transition="scale-transition"
  12. offset-y
  13. min-width="auto"
  14. >
  15. <template #activator="{ on, attrs }">
  16. <v-text-field
  17. v-model="datesFormatted"
  18. autocomplete="off"
  19. :label="$t(fieldLabel)"
  20. prepend-icon="mdi-calendar"
  21. :disabled="readonly"
  22. v-bind="attrs"
  23. :dense="dense"
  24. :single-line="singleLine"
  25. v-on="on"
  26. :error="error || !!violation"
  27. :error-messages="errorMessage || violation ? $t(violation) : ''"
  28. />
  29. </template>
  30. <v-date-picker
  31. v-model="datesParsed"
  32. :range="range"
  33. color="ot-green lighten-1"
  34. @input="dateOpen = range && datesParsed.length < 2"
  35. />
  36. </v-menu>
  37. </main>
  38. </template>
  39. <script setup lang="ts">
  40. import {useNuxtApp} from "#app";
  41. import {useFieldViolation} from "~/composables/form/useFieldViolation";
  42. import {computed, ComputedRef, Ref} from "@vue/reactivity";
  43. import {useDateUtils} from "~/composables/utils/useDateUtils";
  44. import {onUnmounted, watch, WatchStopHandle} from "@vue/runtime-core";
  45. const props = defineProps({
  46. field: {
  47. type: String,
  48. required: false,
  49. default: null
  50. },
  51. label: {
  52. type: String,
  53. required: false,
  54. default: null
  55. },
  56. data: {
  57. type: [String, Array],
  58. required: false,
  59. default: null
  60. },
  61. readonly: {
  62. type: Boolean,
  63. required: false
  64. },
  65. range: {
  66. type: Boolean,
  67. required: false
  68. },
  69. dense: {
  70. type: Boolean,
  71. required: false
  72. },
  73. singleLine: {
  74. type: Boolean,
  75. required: false
  76. },
  77. format: {
  78. type: String,
  79. required: false,
  80. default: 'DD/MM/YYYY'
  81. },
  82. error: {
  83. type: Boolean,
  84. required: false
  85. },
  86. errorMessage: {
  87. type: String,
  88. required: false,
  89. default: null
  90. }
  91. })
  92. const { emit, $moment } = useNuxtApp()
  93. const { data, range } = props
  94. const {violation, onChange} = useFieldViolation(props.field, emit)
  95. const dateUtils = useDateUtils()
  96. const datesParsed: Ref<Array<string>|string|null> = range ? ref(Array<string>()) : ref(null)
  97. const fieldLabel = props.label ?? props.field
  98. const dateOpen: Ref<boolean> = ref(false)
  99. if (Array.isArray(datesParsed.value)) {
  100. for (const date of data as Array<string>) {
  101. if (date) {
  102. datesParsed.value.push($moment(date).format('YYYY-MM-DD'))
  103. }
  104. }
  105. } else {
  106. datesParsed.value = data ? $moment(data as string).format('YYYY-MM-DD') : null
  107. }
  108. const datesFormatted: ComputedRef<string|null> = computed(() => {
  109. if (props.range && datesParsed.value && datesParsed.value.length < 2) {
  110. return null
  111. }
  112. return datesParsed.value ? dateUtils.formatDatesAndConcat(datesParsed.value, props.format) : null
  113. })
  114. const unwatch: WatchStopHandle = watch(datesParsed, (newValue, oldValue) => {
  115. if (newValue === oldValue) { return }
  116. if (props.range && newValue && newValue.length < 2) { return }
  117. onChange(Array.isArray(newValue) ? dateUtils.sortDate(newValue) : newValue)
  118. })
  119. onUnmounted(() => {
  120. unwatch()
  121. })
  122. </script>
  123. <style scoped>
  124. </style>