From ea3bd12811a849a06296620978185fbec44362e6 Mon Sep 17 00:00:00 2001 From: Andrew Kingston Date: Mon, 18 Oct 2021 17:11:12 +0100 Subject: [PATCH 1/3] Add date parsing to core date picker and ensure values are always broadcast as ISO strings --- packages/bbui/src/Form/Core/DatePicker.svelte | 35 +++++++++++++++++-- packages/bbui/src/Form/DatePicker.svelte | 10 +----- 2 files changed, 34 insertions(+), 11 deletions(-) diff --git a/packages/bbui/src/Form/Core/DatePicker.svelte b/packages/bbui/src/Form/Core/DatePicker.svelte index 176db9f497..71126a5b13 100644 --- a/packages/bbui/src/Form/Core/DatePicker.svelte +++ b/packages/bbui/src/Form/Core/DatePicker.svelte @@ -31,7 +31,11 @@ const handleChange = event => { const [dates] = event.detail - dispatch("change", dates[0]) + let newValue = dates[0] + if (newValue) { + newValue = newValue.toISOString() + } + dispatch("change", newValue) } const clearDateOnBackspace = event => { @@ -57,11 +61,38 @@ const els = document.querySelectorAll(`#${flatpickrId} input`) els.forEach(el => el.blur()) } + + const parseDate = val => { + if (!val) { + return null + } + let date + if (val instanceof Date) { + // Use real date obj if already parsed + date = val + } else 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) + } + + $: console.log(value) import Field from "./Field.svelte" import DatePicker from "./Core/DatePicker.svelte" - import { createEventDispatcher } from "svelte" export let value = null export let label = null @@ -11,13 +10,6 @@ export let enableTime = true export let placeholder = null export let appendTo = undefined - - const dispatch = createEventDispatcher() - const onChange = e => { - const isoString = e.detail.toISOString() - value = isoString - dispatch("change", isoString) - } @@ -28,6 +20,6 @@ {placeholder} {enableTime} {appendTo} - on:change={onChange} + on:change /> From f5f2611a3e085e58ca4b5947f5fef66e7aa4c595 Mon Sep 17 00:00:00 2001 From: Andrew Kingston Date: Mon, 18 Oct 2021 17:12:02 +0100 Subject: [PATCH 2/3] Remove date parsing from client date picker as it has moved to the core date picker --- .../components/app/forms/DateTimeField.svelte | 27 +------------------ 1 file changed, 1 insertion(+), 26 deletions(-) diff --git a/packages/client/src/components/app/forms/DateTimeField.svelte b/packages/client/src/components/app/forms/DateTimeField.svelte index 298fff5a1c..46ccdca4f3 100644 --- a/packages/client/src/components/app/forms/DateTimeField.svelte +++ b/packages/client/src/components/app/forms/DateTimeField.svelte @@ -12,31 +12,6 @@ let fieldState let fieldApi - - const parseDate = val => { - if (!val) { - return null - } - let date - if (val instanceof Date) { - // Use real date obj if already parsed - date = val - } else 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) - } Date: Tue, 19 Oct 2021 14:04:12 +0100 Subject: [PATCH 3/3] Fix usage of binding values to datepickers --- packages/bbui/src/Form/DatePicker.svelte | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/packages/bbui/src/Form/DatePicker.svelte b/packages/bbui/src/Form/DatePicker.svelte index bb1be87314..7d5656a22d 100644 --- a/packages/bbui/src/Form/DatePicker.svelte +++ b/packages/bbui/src/Form/DatePicker.svelte @@ -1,6 +1,7 @@ @@ -20,6 +28,6 @@ {placeholder} {enableTime} {appendTo} - on:change + on:change={onChange} />