DatePicker.vue 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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(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. :error="error"
  27. />
  28. </template>
  29. <v-date-picker
  30. v-model="datesParsed"
  31. locale="fr"
  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 lang="ts">
  40. import { defineComponent, watch, ref, useContext, onUnmounted, computed, Ref, ComputedRef } from '@nuxtjs/composition-api'
  41. import { WatchStopHandle } from '@vue/composition-api'
  42. import DatesUtils from '~/services/utils/datesUtils'
  43. import {$useError} from "~/composables/form/useError";
  44. export default defineComponent({
  45. props: {
  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. },
  78. setup (props, { emit }) {
  79. const { data, field, range } = props
  80. const { $moment } = useContext()
  81. const dateUtils = new DatesUtils($moment)
  82. const {error, onChange} = $useError(props.field, emit)
  83. const datesParsed: Ref<Array<string>|string> = range ? ref(Array<string>()) : ref('')
  84. if (Array.isArray(datesParsed.value)) {
  85. for (const date of data as Array<string>) {
  86. if (date) { datesParsed.value.push($moment(date).format('YYYY-MM-DD')) }
  87. }
  88. } else {
  89. datesParsed.value = $moment(data as string).format('YYYY-MM-DD')
  90. }
  91. const datesFormatted: ComputedRef<string> = computed(() => {
  92. return dateUtils.formattedDate(datesParsed.value, 'DD/MM/YYYY')
  93. })
  94. const unwatch: WatchStopHandle = watch(datesParsed, (newValue, oldValue) => {
  95. if (newValue === oldValue) { return }
  96. if (Array.isArray(newValue) && newValue.length < 2) { return }
  97. onChange(Array.isArray(newValue) ? dateUtils.sortDate(newValue) : newValue)
  98. })
  99. onUnmounted(() => {
  100. unwatch()
  101. })
  102. return {
  103. label_field: props.label ?? props.field,
  104. datesParsed,
  105. datesFormatted,
  106. dateOpen: ref(false),
  107. error
  108. }
  109. }
  110. })
  111. </script>
  112. <style scoped>
  113. </style>