DatePicker.vue 2.7 KB

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