DatePicker.vue 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 v-slot:activator="{ on, attrs }">
  16. <v-text-field
  17. v-model="datesFormatted"
  18. autocomplete="off"
  19. :label="$t(label_field)"
  20. prepend-icon="mdi-calendar"
  21. :disabled="readonly"
  22. v-bind="attrs"
  23. :dense="dense"
  24. :single-line="singleLine"
  25. v-on="on"
  26. />
  27. </template>
  28. <v-date-picker
  29. v-model="datesParsed"
  30. locale="fr"
  31. :range="range"
  32. color="ot_green lighten-1"
  33. @input="dateOpen = range && datesParsed.length < 2"
  34. />
  35. </v-menu>
  36. </main>
  37. </template>
  38. <script lang="ts">
  39. import { defineComponent, watch, ref, useContext, onUnmounted, computed, Ref, ComputedRef } from '@nuxtjs/composition-api'
  40. import { WatchStopHandle } from '@vue/composition-api'
  41. import DatesUtils from '~/services/utils/datesUtils'
  42. export default defineComponent({
  43. props: {
  44. field: {
  45. type: String,
  46. required: false,
  47. default: null
  48. },
  49. label: {
  50. type: String,
  51. required: false,
  52. default: null
  53. },
  54. data: {
  55. type: [String, Array],
  56. required: false,
  57. default: null
  58. },
  59. readonly: {
  60. type: Boolean,
  61. required: false
  62. },
  63. range: {
  64. type: Boolean,
  65. required: false
  66. },
  67. dense: {
  68. type: Boolean,
  69. required: false
  70. },
  71. singleLine: {
  72. type: Boolean,
  73. required: false
  74. }
  75. },
  76. setup (props, { emit }) {
  77. const { data, field, range } = props
  78. const { $moment } = useContext()
  79. const dateUtils = new DatesUtils($moment)
  80. const datesParsed: Ref<Array<string>|string> = range ? ref(Array<string>()) : ref('')
  81. if (Array.isArray(datesParsed.value)) {
  82. for (const date of data as Array<string>) {
  83. if (date) { datesParsed.value.push($moment(date).format('YYYY-MM-DD')) }
  84. }
  85. } else {
  86. datesParsed.value = $moment(data as string).format('YYYY-MM-DD')
  87. }
  88. const datesFormatted: ComputedRef<string> = computed(() => {
  89. return dateUtils.formattedDate(datesParsed.value, 'DD/MM/YYYY')
  90. })
  91. const unwatch: WatchStopHandle = watch(datesParsed, (newValue, oldValue) => {
  92. if (newValue === oldValue) { return }
  93. if (Array.isArray(newValue) && newValue.length < 2) { return }
  94. emit('update', Array.isArray(newValue) ? dateUtils.sortDate(newValue) : newValue, field)
  95. })
  96. onUnmounted(() => {
  97. unwatch()
  98. })
  99. return {
  100. label_field: props.label ?? props.field,
  101. datesParsed,
  102. datesFormatted,
  103. dateOpen: ref(false)
  104. }
  105. }
  106. })
  107. </script>
  108. <style scoped>
  109. </style>