Fix data type handling for datepicker and grid filter components

here is what's up now:

- <b-datepicker> expects v-model to be a Date
- <tailbone-datepicker> also expects a Date
- <grid-filter-date-value> uses String for its v-model

latter is so the value can represent a date range, e.g. 'YYYY-MM-DD|YYYY-MM-DD'

anyway there was previously confusion about data type among these
components, and hopefully they are straight now per the above outline
This commit is contained in:
Lance Edgar 2024-04-25 18:52:34 -05:00
parent ab57fb3f0f
commit daf68cad01
2 changed files with 60 additions and 20 deletions

View file

@ -11,7 +11,7 @@ const TailboneDatepicker = {
'icon="calendar-alt"', 'icon="calendar-alt"',
':date-formatter="formatDate"', ':date-formatter="formatDate"',
':date-parser="parseDate"', ':date-parser="parseDate"',
':value="value ? parseDate(value) : null"', ':value="buefyValue"',
'@input="dateChanged"', '@input="dateChanged"',
':disabled="disabled"', ':disabled="disabled"',
'ref="trueDatePicker"', 'ref="trueDatePicker"',
@ -26,6 +26,24 @@ const TailboneDatepicker = {
disabled: Boolean, disabled: Boolean,
}, },
data() {
let buefyValue = this.value
if (buefyValue && !buefyValue.getDate) {
buefyValue = this.parseDate(this.value)
}
return {
buefyValue,
}
},
watch: {
value(to, from) {
if (this.buefyValue != to) {
this.buefyValue = to
}
},
},
methods: { methods: {
formatDate(date) { formatDate(date) {
@ -49,7 +67,7 @@ const TailboneDatepicker = {
}, },
dateChanged(date) { dateChanged(date) {
this.$emit('input', this.formatDate(date)) this.$emit('input', date)
}, },
focus() { focus() {

View file

@ -127,40 +127,62 @@
dateRange: Boolean, dateRange: Boolean,
}, },
data() { data() {
return { let startDate = null
startDate: null, let endDate = null
endDate: null, if (this.value) {
}
},
mounted() {
if (this.dateRange) { if (this.dateRange) {
if (this.value.includes('|')) {
let values = this.value.split('|') let values = this.value.split('|')
if (values.length == 2) { if (values.length == 2) {
this.startDate = values[0] startDate = this.parseDate(values[0])
this.endDate = values[1] endDate = this.parseDate(values[1])
} else { } else { // no end date specified?
this.startDate = this.value startDate = this.parseDate(this.value)
} }
} else {
this.startDate = this.value } else { // not a range, so start date only
startDate = this.parseDate(this.value)
} }
} else { }
this.startDate = this.value
return {
startDate,
endDate,
} }
}, },
methods: { methods: {
focus() { focus() {
this.$refs.startDate.focus() this.$refs.startDate.focus()
}, },
formatDate(date) {
if (date === null) {
return null
}
// just need to convert to simple ISO date format here, seems
// like there should be a more obvious way to do that?
var year = date.getFullYear()
var month = date.getMonth() + 1
var day = date.getDate()
month = month < 10 ? '0' + month : month
day = day < 10 ? '0' + day : day
return year + '-' + month + '-' + day
},
parseDate(value) {
if (value) {
// note, this assumes classic YYYY-MM-DD (i.e. ISO?) format
const parts = value.split('-')
return new Date(parts[0], parseInt(parts[1]) - 1, parts[2])
}
},
startDateChanged(value) { startDateChanged(value) {
value = this.formatDate(value)
if (this.dateRange) { if (this.dateRange) {
value += '|' + this.endDate value += '|' + this.formatDate(this.endDate)
} }
this.$emit('input', value) this.$emit('input', value)
}, },
endDateChanged(value) { endDateChanged(value) {
value = this.startDate + '|' + value value = this.formatDate(this.startDate) + '|' + this.formatDate(value)
this.$emit('input', value) this.$emit('input', value)
}, },
}, },