Handle times properly on time only settings for timestamps
This commit is contained in:
parent
d3f1180ba8
commit
f09b6bf090
2 changed files with 30 additions and 3 deletions
|
@ -68,6 +68,11 @@
|
||||||
maximum: schema?.constraints?.length?.maximum,
|
maximum: schema?.constraints?.length?.maximum,
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
[FieldType.DATETIME]: (_field, schema) => {
|
||||||
|
return {
|
||||||
|
valueAsTimestamp: !schema?.timeOnly,
|
||||||
|
}
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
const fieldSchema = getFieldSchema(field)
|
const fieldSchema = getFieldSchema(field)
|
||||||
|
|
|
@ -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>
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue