DatePicker.vue 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <!--
  2. Sélecteur de dates, à placer dans un composant `UiForm`
  3. -->
  4. <template>
  5. <main>
  6. <div class="d-flex flex-column">
  7. <span>{{ $t(fieldLabel) }}</span>
  8. <UiDatePicker
  9. v-model="date"
  10. :readonly="readonly"
  11. :format="format"
  12. :position="position"
  13. @update:model-value="onUpdate($event)"
  14. />
  15. <span v-if="error || !!fieldViolations" class="theme-danger">
  16. {{ errorMessage || fieldViolations ? $t(fieldViolations) : '' }}
  17. </span>
  18. </div>
  19. </main>
  20. </template>
  21. <script setup lang="ts">
  22. import { formatISO } from 'date-fns'
  23. import type { PropType, Ref } from 'vue'
  24. import { ref } from 'vue'
  25. import { useFieldViolation } from '~/composables/form/useFieldViolation'
  26. const props = defineProps({
  27. /**
  28. * v-model
  29. */
  30. modelValue: {
  31. type: String as PropType<Date | string | null>,
  32. required: false,
  33. default: null,
  34. },
  35. /**
  36. * Nom de la propriété d'une entité lorsque l'input concerne cette propriété
  37. * - Utilisé par la validation
  38. * - Laisser null si le champ ne s'applique pas à une entité
  39. */
  40. field: {
  41. type: String,
  42. required: false,
  43. default: null,
  44. },
  45. /**
  46. * Label du champ
  47. * Si non défini, c'est le nom de propriété qui est utilisé
  48. */
  49. label: {
  50. type: String,
  51. required: false,
  52. default: null,
  53. },
  54. /**
  55. * Définit si le champ est en lecture seule
  56. */
  57. readonly: {
  58. type: Boolean,
  59. required: false,
  60. },
  61. /**
  62. * Format d'affichage des dates
  63. * @see https://vue3datepicker.com/props/formatting/
  64. */
  65. format: {
  66. type: String,
  67. required: false,
  68. default: null,
  69. },
  70. /**
  71. * Règles de validation
  72. * @see https://vuetify.cn/en/components/forms/#validation-with-submit-clear
  73. */
  74. rules: {
  75. type: Array,
  76. required: false,
  77. default: () => [],
  78. },
  79. /**
  80. * Le champ est-il actuellement en état d'erreur
  81. */
  82. error: {
  83. type: Boolean,
  84. required: false,
  85. },
  86. /**
  87. * Si le champ est en état d'erreur, quel est le message d'erreur?
  88. */
  89. errorMessage: {
  90. type: String,
  91. required: false,
  92. default: null,
  93. },
  94. /**
  95. * @see https://vue3datepicker.com/props/positioning/#position
  96. */
  97. position: {
  98. type: String as PropType<'left' | 'center' | 'right'>,
  99. required: false,
  100. default: 'center',
  101. },
  102. })
  103. const { fieldViolations, updateViolationState } = useFieldViolation(props.field)
  104. const fieldLabel = props.label ?? props.field
  105. const emit = defineEmits(['update:model-value', 'change'])
  106. const date: Ref<Date | undefined> = ref(
  107. props.modelValue ? new Date(props.modelValue) : undefined,
  108. )
  109. const onUpdate = (event: string) => {
  110. updateViolationState(event)
  111. emit('update:model-value', date.value ? formatISO(date.value) : undefined)
  112. }
  113. </script>
  114. <style scoped></style>