1
0
Fork 0
mirror of https://gitlab.com/bramw/baserow.git synced 2025-04-13 16:49:07 +00:00
bramw_baserow/web-frontend/modules/database/mixins/durationField.js
2024-01-18 08:36:56 +00:00

57 lines
1.4 KiB
JavaScript

import { DurationFieldType } from '@baserow/modules/database/fieldTypes'
/**
* This mixin contains some method overrides for validating and formatting the
* duration field. This mixin is used in both the GridViewFieldDuration and
* RowEditFieldDuration components.
*/
export default {
data() {
return {
errorMsg: null,
formattedValue: '',
}
},
watch: {
value(value) {
this.updateFormattedValue(this.field, value)
},
},
created() {
this.updateFormattedValue(this.field, this.value)
},
methods: {
getField() {
return this.field
},
isValid() {
return this.errorMsg === null
},
getError() {
return this.errorMsg
},
formatValue(field, value) {
return DurationFieldType.formatValue(field, value)
},
updateFormattedValue(field, value) {
this.formattedValue = this.formatValue(field, value)
},
updateCopy(field, value) {
this.errorMsg = this.getValidationError(value)
if (this.errorMsg !== null) {
return
}
const newCopy = DurationFieldType.parseInputValue(field, value)
if (newCopy !== this.copy) {
this.copy = newCopy
return newCopy
}
},
onKeyPress(field, event) {
const allowedChars = ['.', ':', ' ', 'd', 'h']
if (!/\d/.test(event.key) && !allowedChars.includes(event.key)) {
return event.preventDefault()
}
},
},
}