Merge pull request #13865 from Budibase/BUDI-8316/cannot-save-time-value-to-internal-db

Fix time-only form fields for internal db
This commit is contained in:
Adria Navarro 2024-06-06 14:23:44 +02:00 committed by GitHub
commit d225cde835
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 34 additions and 3 deletions

View File

@ -68,6 +68,15 @@
maximum: schema?.constraints?.length?.maximum, maximum: schema?.constraints?.length?.maximum,
} }
}, },
[FieldType.DATETIME]: (_field, schema) => {
const props = {
valueAsTimestamp: !schema?.timeOnly,
}
if (schema?.dateOnly) {
props.enableTime = false
}
return props
},
} }
const fieldSchema = getFieldSchema(field) const fieldSchema = getFieldSchema(field)

View File

@ -16,15 +16,37 @@
export let onChange export let onChange
export let span export let span
export let helpText = null export let helpText = null
export let valueAsTimestamp = false
let fieldState let fieldState
let fieldApi let fieldApi
const handleChange = e => { const handleChange = e => {
const changed = fieldApi.setValue(e.detail) let value = e.detail
if (onChange && changed) { if (timeOnly && valueAsTimestamp) {
onChange({ value: e.detail }) if (!isValidDate(value)) {
// Handle time only fields that are timestamps under the hood
value = timeToDateISOString(value)
}
} }
const changed = fieldApi.setValue(value)
if (onChange && changed) {
onChange({ value })
}
}
const isValidDate = value => !isNaN(new Date(value))
const timeToDateISOString = value => {
let [hours, minutes] = value.split(":").map(Number)
const date = new Date()
date.setHours(hours)
date.setMinutes(minutes)
date.setSeconds(0)
date.setMilliseconds(0)
return date.toISOString()
} }
</script> </script>