Add default value for date field
This commit is contained in:
parent
88c22aa927
commit
6d27bcd22f
|
@ -7,12 +7,43 @@
|
||||||
export let placeholder
|
export let placeholder
|
||||||
export let disabled = false
|
export let disabled = false
|
||||||
export let enableTime = false
|
export let enableTime = false
|
||||||
|
export let defaultValue
|
||||||
|
|
||||||
let fieldState
|
let fieldState
|
||||||
let fieldApi
|
let fieldApi
|
||||||
|
|
||||||
|
const parseDate = val => {
|
||||||
|
if (!val) {
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
let date
|
||||||
|
if (isNaN(val)) {
|
||||||
|
// Treat as date string of some sort
|
||||||
|
date = new Date(val)
|
||||||
|
} else {
|
||||||
|
// Treat as numerical timestamp
|
||||||
|
date = new Date(parseInt(val))
|
||||||
|
}
|
||||||
|
const time = date.getTime()
|
||||||
|
if (isNaN(time)) {
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
// By rounding to the nearest second we avoid locking up in an endless
|
||||||
|
// loop in the builder, caused by potentially enriching {{ now }} to every
|
||||||
|
// millisecond.
|
||||||
|
return new Date(Math.floor(time / 1000) * 1000)
|
||||||
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<Field {label} {field} {disabled} type="datetime" bind:fieldState bind:fieldApi>
|
<Field
|
||||||
|
{label}
|
||||||
|
{field}
|
||||||
|
{disabled}
|
||||||
|
defaultValue={parseDate(defaultValue)}
|
||||||
|
type="datetime"
|
||||||
|
bind:fieldState
|
||||||
|
bind:fieldApi
|
||||||
|
>
|
||||||
{#if fieldState}
|
{#if fieldState}
|
||||||
<CoreDatePicker
|
<CoreDatePicker
|
||||||
value={$fieldState.value}
|
value={$fieldState.value}
|
||||||
|
|
Loading…
Reference in New Issue