| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124 |
- <!--
- Sélecteur de dates
- @see https://vuetifyjs.com/en/components/date-pickers/
- -->
- <template>
- <main>
- <v-menu
- v-model="dateOpen"
- :close-on-content-click="false"
- :nudge-right="40"
- transition="scale-transition"
- offset-y
- min-width="auto"
- >
- <template #activator="{ on, attrs }">
- <v-text-field
- v-model="datesFormatted"
- autocomplete="off"
- :label="$t(label_field)"
- prepend-icon="mdi-calendar"
- :disabled="readonly"
- v-bind="attrs"
- :dense="dense"
- :single-line="singleLine"
- v-on="on"
- :error="error"
- />
- </template>
- <v-date-picker
- v-model="datesParsed"
- locale="fr"
- :range="range"
- color="ot_green lighten-1"
- @input="dateOpen = range && datesParsed.length < 2"
- />
- </v-menu>
- </main>
- </template>
- <script lang="ts">
- import { defineComponent, watch, ref, useContext, onUnmounted, computed, Ref, ComputedRef } from '@nuxtjs/composition-api'
- import { WatchStopHandle } from '@vue/composition-api'
- import DatesUtils from '~/services/utils/datesUtils'
- import {$useError} from "~/composables/form/useError";
- export default defineComponent({
- props: {
- field: {
- type: String,
- required: false,
- default: null
- },
- label: {
- type: String,
- required: false,
- default: null
- },
- data: {
- type: [String, Array],
- required: false,
- default: null
- },
- readonly: {
- type: Boolean,
- required: false
- },
- range: {
- type: Boolean,
- required: false
- },
- dense: {
- type: Boolean,
- required: false
- },
- singleLine: {
- type: Boolean,
- required: false
- }
- },
- setup (props, { emit }) {
- const { data, field, range } = props
- const { $moment } = useContext()
- const dateUtils = new DatesUtils($moment)
- const {error, onChange} = $useError(props.field, emit)
- const datesParsed: Ref<Array<string>|string> = range ? ref(Array<string>()) : ref('')
- if (Array.isArray(datesParsed.value)) {
- for (const date of data as Array<string>) {
- if (date) { datesParsed.value.push($moment(date).format('YYYY-MM-DD')) }
- }
- } else {
- datesParsed.value = $moment(data as string).format('YYYY-MM-DD')
- }
- const datesFormatted: ComputedRef<string> = computed(() => {
- return dateUtils.formattedDate(datesParsed.value, 'DD/MM/YYYY')
- })
- const unwatch: WatchStopHandle = watch(datesParsed, (newValue, oldValue) => {
- if (newValue === oldValue) { return }
- if (Array.isArray(newValue) && newValue.length < 2) { return }
- onChange(Array.isArray(newValue) ? dateUtils.sortDate(newValue) : newValue)
- })
- onUnmounted(() => {
- unwatch()
- })
- return {
- label_field: props.label ?? props.field,
- datesParsed,
- datesFormatted,
- dateOpen: ref(false),
- error
- }
- }
- })
- </script>
- <style scoped>
- </style>
|