mirror of
https://github.com/baserow/baserow.git
synced 2026-12-04 08:18:42 +00:00
* fix: validate required duration while editing Validate the parsed duration copy while the editor is active instead of the stale parent value, which is only updated on blur. This lets valid required values save while preserving untouched-required and invalid-format errors. * chore: add changelog entry for #5741
69 lines
2.1 KiB
JavaScript
69 lines
2.1 KiB
JavaScript
import { DurationFieldType } from '@baserow/modules/database/fieldTypes'
|
|
import { formatDurationValue } from '@baserow/modules/database/utils/duration'
|
|
|
|
/**
|
|
* 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
|
|
},
|
|
getError() {
|
|
// `errorMsg` holds the parse error captured while the user types, because
|
|
// partially-typed text may not round-trip to `value`. A valid parsed copy
|
|
// is not emitted as `value` until blur, so validate that copy while editing.
|
|
const value = this.editing ? this.prepareValue(this.copy) : this.value
|
|
return this.errorMsg ?? this.getValidationError(value)
|
|
},
|
|
formatValue(field, value) {
|
|
// This function is used also in functional components,
|
|
// so we cannot get the field type from the registry here.
|
|
return formatDurationValue(value, field.duration_format)
|
|
},
|
|
updateFormattedValue(field, value) {
|
|
this.formattedValue = this.formatValue(field, value)
|
|
},
|
|
updateCopy(field, value) {
|
|
this.errorMsg = this.getValidationError(value)
|
|
if (this.errorMsg !== null) {
|
|
return
|
|
}
|
|
const fieldType = this.$registry.get('field', DurationFieldType.getType())
|
|
const newCopy = fieldType.parseInputValue(field, value)
|
|
if (newCopy !== this.copy) {
|
|
this.copy = newCopy
|
|
return newCopy
|
|
}
|
|
},
|
|
isValidChar(char) {
|
|
const allowedChars = ['.', ':', ' ', 'd', 'h', 'm', 's', '-']
|
|
return /\d/.test(char) || allowedChars.includes(char)
|
|
},
|
|
onKeyPress(field, event) {
|
|
if (!this.isValidChar(event.key)) {
|
|
return event.preventDefault()
|
|
}
|
|
},
|
|
onInput(field, event) {
|
|
this.updateCopy(field, event.target.value)
|
|
},
|
|
},
|
|
}
|