| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124 |
- <!--
- Sélecteur de dates, à placer dans un composant `UiForm`
- -->
- <template>
- <main>
- <div class="d-flex flex-column">
- <span>{{ $t(fieldLabel) }}</span>
- <UiDatePicker
- v-model="date"
- :readonly="readonly"
- :format="format"
- :position="position"
- @update:model-value="onUpdate($event)"
- />
- <span v-if="error || !!fieldViolations" class="theme-danger">
- {{ errorMessage || fieldViolations ? $t(fieldViolations) : '' }}
- </span>
- </div>
- </main>
- </template>
- <script setup lang="ts">
- import { useFieldViolation } from '~/composables/form/useFieldViolation'
- import { formatISO } from 'date-fns'
- import type { PropType } from '@vue/runtime-core'
- const props = defineProps({
- /**
- * v-model
- */
- modelValue: {
- type: String as PropType<string | null>,
- required: false,
- default: null,
- },
- /**
- * Nom de la propriété d'une entité lorsque l'input concerne cette propriété
- * - Utilisé par la validation
- * - Laisser null si le champ ne s'applique pas à une entité
- */
- field: {
- type: String,
- required: false,
- default: null,
- },
- /**
- * Label du champ
- * Si non défini, c'est le nom de propriété qui est utilisé
- */
- label: {
- type: String,
- required: false,
- default: null,
- },
- /**
- * Définit si le champ est en lecture seule
- */
- readonly: {
- type: Boolean,
- required: false,
- },
- /**
- * Format d'affichage des dates
- * @see https://vue3datepicker.com/props/formatting/
- */
- format: {
- type: String,
- required: false,
- default: null,
- },
- /**
- * Règles de validation
- * @see https://vuetify.cn/en/components/forms/#validation-with-submit-clear
- */
- rules: {
- type: Array,
- required: false,
- default: () => [],
- },
- /**
- * Le champ est-il actuellement en état d'erreur
- */
- error: {
- type: Boolean,
- required: false,
- },
- /**
- * Si le champ est en état d'erreur, quel est le message d'erreur?
- */
- errorMessage: {
- type: String,
- required: false,
- default: null,
- },
- /**
- * @see https://vue3datepicker.com/props/positioning/#position
- */
- position: {
- type: String as PropType<'left' | 'center' | 'right'>,
- required: false,
- default: 'center',
- },
- })
- const input = ref(null)
- const { fieldViolations, updateViolationState } = useFieldViolation(props.field)
- const fieldLabel = props.label ?? props.field
- const emit = defineEmits(['update:model-value', 'change'])
- const date: Ref<Date> = ref(new Date(props.modelValue))
- const onUpdate = (event: string) => {
- updateViolationState(event)
- emit('update:model-value', formatISO(date.value))
- }
- </script>
- <style scoped></style>
|