DatePicker.vue 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <!--
  2. Sélecteur de dates
  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. @update:model-value="onUpdate($event)"
  13. @change="onChange($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 {useFieldViolation} from "~/composables/form/useFieldViolation";
  23. import {formatISO} from "date-fns";
  24. const props = defineProps({
  25. /**
  26. * v-model
  27. */
  28. modelValue: {
  29. type: String,
  30. required: false,
  31. default: null
  32. },
  33. field: {
  34. type: String,
  35. required: false,
  36. default: null
  37. },
  38. label: {
  39. type: String,
  40. required: false,
  41. default: null
  42. },
  43. readonly: {
  44. type: Boolean,
  45. required: false
  46. },
  47. format: {
  48. type: String,
  49. required: false,
  50. default: null
  51. },
  52. error: {
  53. type: Boolean,
  54. required: false
  55. },
  56. errorMessage: {
  57. type: String,
  58. required: false,
  59. default: null
  60. }
  61. })
  62. const input = ref(null)
  63. const {fieldViolations, updateViolationState} = useFieldViolation(props.field)
  64. const fieldLabel = props.label ?? props.field
  65. const emit = defineEmits(['update:model-value', 'change'])
  66. const date: Ref<Date> = ref(new Date(props.modelValue))
  67. console.log(date.value)
  68. const onUpdate = (event: string) => {
  69. emit('update:model-value', formatISO(date.value))
  70. }
  71. const onChange = (event: Event | undefined) => {
  72. updateViolationState(event)
  73. emit('change', formatISO(date.value))
  74. }
  75. </script>
  76. <style scoped>
  77. </style>