|
| 1 | +<template> |
| 2 | + <v-menu |
| 3 | + :disabled="disabled" |
| 4 | + full-width |
| 5 | + min-width="290px" |
| 6 | + offset-y |
| 7 | + transition="scale-transition"> |
| 8 | + <v-text-field |
| 9 | + v-validate="processedValidation" |
| 10 | + slot="activator" |
| 11 | + :name="name" |
| 12 | + :value="normalizedValue" |
| 13 | + :label="label" |
| 14 | + :disabled="disabled" |
| 15 | + :error-messages="vErrors.collect(name)" |
| 16 | + :data-vv-as="label" |
| 17 | + append-icon="mdi-calendar" |
| 18 | + readonly/> |
| 19 | + <v-date-picker :value="normalizedValue" @input="save($event)" no-title/> |
| 20 | + </v-menu> |
| 21 | +</template> |
| 22 | + |
| 23 | +<script> |
| 24 | +import fecha from 'fecha'; |
| 25 | +import get from 'lodash/get'; |
| 26 | +import omit from 'lodash/omit'; |
| 27 | +
|
| 28 | +const DATE_FORMAT = 'YYYY-MM-DD'; |
| 29 | +
|
| 30 | +export default { |
| 31 | + inject: ['$validator'], |
| 32 | + props: { |
| 33 | + name: { type: String, required: true }, |
| 34 | + value: { type: String, default: null }, |
| 35 | + format: { type: String, default: DATE_FORMAT }, |
| 36 | + validate: { type: Object, default: () => ({}) }, |
| 37 | + disabled: { type: Boolean, default: false }, |
| 38 | + label: { type: String, default: null } |
| 39 | + }, |
| 40 | + computed: { |
| 41 | + normalizedValue() { |
| 42 | + return this.normalize(this.value, this.format, DATE_FORMAT); |
| 43 | + }, |
| 44 | + processedValidation() { |
| 45 | + const { validate, format: inputFormat } = this; |
| 46 | + if (!get(validate, 'before') && !get(validate, 'after')) { |
| 47 | + return omit(validate, ['before', 'after']); |
| 48 | + } |
| 49 | + const tmp = { ...validate, date_format: DATE_FORMAT }; |
| 50 | + ['before', 'after'].forEach(k => { |
| 51 | + tmp[k] && (tmp[k] = this.normalize(tmp[k], inputFormat, DATE_FORMAT)); |
| 52 | + }); |
| 53 | + return tmp; |
| 54 | + } |
| 55 | + }, |
| 56 | + methods: { |
| 57 | + save(value) { |
| 58 | + this.$emit('input', this.normalize(value, DATE_FORMAT, this.format)); |
| 59 | + }, |
| 60 | + normalize(value, inputFormat, outputFormat) { |
| 61 | + if (!value) return; |
| 62 | + const date = fecha.parse(value, inputFormat); |
| 63 | + return fecha.format(date, outputFormat); |
| 64 | + } |
| 65 | + } |
| 66 | +}; |
| 67 | +</script> |
0 commit comments