| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162 |
- <template >
- <div v-click-outside="onOutsideClick">
- <v-text-field
- v-model="dateIntervalText"
- type="text"
- class="text-field"
- outlined
- readonly
- clearable
- hide-details
- :label="$t('when') + ' ?'"
- append-icon="mdi-calendar"
- @click="onClick"
- @change="$emit('change', $event ? $event.value : '')"
- @click:append="onClick"
- />
- <v-card v-show="show" class="date-picker pa-2">
- <div class="d-flex flex-row">
- <div class="presets">
- <a
- v-for="preset in presets"
- class="preset-link"
- @click="presetClicked(preset.range)"
- >
- {{ preset.label }}
- </a>
- </div>
- <v-date-picker
- ref="datePicker1"
- :value="isoDateStart"
- range
- :min="today"
- scrollable
- no-title
- locale="fr"
- @change="dateRangeChanged"
- />
- </div>
- </v-card>
- </div>
- </template>
- <script lang="ts">
- import Vue from 'vue'
- import { format, parse, addDays, addMonths, nextSunday } from 'date-fns'
- interface DateRangePreset {
- label: string,
- range: DateRange
- }
- export default Vue.extend({
- props: {
- value: {
- type: Object
- }
- },
- data () {
- return {
- model: null as DateRange | null,
- dateStart: null as Date | null,
- dateEnd: null as Date | null,
- show: false
- }
- },
- methods: {
- onClick() {
- this.show = !this.show
- },
- onOutsideClick() {
- this.show = false
- },
- presetClicked(range: DateRange) {
- this.dateStart = range.start
- this.dateEnd = range.end
- },
- clear () {
- this.dateStart = null
- this.dateEnd = null
- },
- dateToString(date: Date | null) {
- return date !== null ? format(date, 'yyyy-MM-dd') : ''
- },
- stringToDate(date: string) {
- return new Date(date)
- }
- },
- computed: {
- today() {
- return format(new Date(), 'yyyy-MM-dd')
- },
- presets (): Array<DateRangePreset> {
- const today = new Date()
- // Today
- const today_preset: DateRangePreset = {
- label: this.$t('today').toString(),
- range: {start: today, end: today}
- }
- // Cette semaine
- const week_preset: DateRangePreset = {
- label: this.$t('next_week').toString(),
- range: {start: today, end: addDays(today, 7)}
- }
- // Ce week-end
- const sunday: Date = nextSunday(today)
- const weekend_preset = {
- label: this.$t('next_weekend').toString(),
- range: {start: addDays(sunday, -1), end: sunday}
- }
- // Ce mois
- const month_preset: DateRangePreset = {
- label: this.$t('next_month').toString(),
- range: {start: today, end: addMonths(today, 1)}
- }
- return [today_preset, week_preset, weekend_preset, month_preset]
- },
- dateIntervalText (): string {
- if (this.dateStart !== null && this.dateEnd !== null) {
- return format(this.dateStart, 'dd/MM/yyyy') + ' ~ ' + format(this.dateEnd, 'dd/MM/yyyy')
- } else if (this.dateStart !== null) {
- return format(this.dateStart, 'dd/MM/yyyy') + ' ~ ?'
- } else if (this.dateEnd !== null) {
- return '? ~ ' + format(this.dateEnd, 'dd/MM/yyyy')
- } else {
- return ''
- }
- }
- }
- })
- </script>
- <style scoped>
- .date-picker {
- z-index: 1;
- position: absolute;
- }
- .presets {
- display: flex;
- flex-direction: column;
- padding: 12px;
- min-width: 120px;
- font-size: 14px;
- }
- .preset-link {
- padding: 12px 0;
- }
- .preset-link:hover {
- text-decoration: underline;
- }
- </style>
|