From 31b5fece38676eb369458cfa73ee360b8bd0635b Mon Sep 17 00:00:00 2001 From: Garrett Michael Flynn Date: Mon, 3 Jun 2024 14:02:26 -0500 Subject: [PATCH] More uniform time handling --- .../core/components/DateTimeSelector.js | 42 +++++++++++++------ .../core/components/JSONSchemaInput.js | 12 +----- src/schemas/timezone.schema.ts | 3 +- 3 files changed, 33 insertions(+), 24 deletions(-) diff --git a/src/electron/frontend/core/components/DateTimeSelector.js b/src/electron/frontend/core/components/DateTimeSelector.js index 075e031ac..3d622ef09 100644 --- a/src/electron/frontend/core/components/DateTimeSelector.js +++ b/src/electron/frontend/core/components/DateTimeSelector.js @@ -35,6 +35,18 @@ export function extractISOString( return formattedDate; } +export const renderDateTime = (value) => { + if (typeof value === "string") return extractISOString(new Date(value)); + return value; +} + +export const resolveDateTime = renderDateTime +// const resolveDateTime = (value) => { +// if (typeof value === "string") return extractISOString(new Date(value), { offset: true }); +// return value; +// } + + export class DateTimeSelector extends LitElement { static get styles() { return css` @@ -48,27 +60,36 @@ export class DateTimeSelector extends LitElement { // Manually handle value property get value() { const date = new Date(this.input.value); - return extractISOString(date, { offset: true }); + return resolveDateTime(date); } // Render the date without timezone offset set value(newValue) { - if (newValue) this.input.value = extractISOString(new Date(newValue)); + if (newValue) this.input.value = renderDateTime(new Date(newValue)); else { const d = new Date(); d.setHours(0, 0, 0, 0); - this.input.value = extractISOString(d); + this.input.value = renderDateTime(d); } } - static get properties() { - return { - min: { type: String, reflect: true }, - max: { type: String, reflect: true }, - timezone: { type: String, reflect: true }, - }; + get min() { + return this.input.min + } + + set min(value) { + this.input.min = value } + get max() { + return this.input.max + } + + set max(value) { + this.input.max = value + } + + constructor({ value, min, max } = {}) { super(); this.input = document.createElement("input"); @@ -94,9 +115,6 @@ export class DateTimeSelector extends LitElement { } render() { - this.input.min = min; - this.input.max = max; - return this.input; } } diff --git a/src/electron/frontend/core/components/JSONSchemaInput.js b/src/electron/frontend/core/components/JSONSchemaInput.js index 12114bdc0..9e186fc38 100644 --- a/src/electron/frontend/core/components/JSONSchemaInput.js +++ b/src/electron/frontend/core/components/JSONSchemaInput.js @@ -16,20 +16,10 @@ import tippy from "tippy.js"; import { merge } from "./pages/utils"; import { OptionalSection } from "./OptionalSection"; import { InspectorListItem } from "./preview/inspector/InspectorList"; -import { extractISOString } from "./DateTimeSelector"; +import { renderDateTime, resolveDateTime } from "./DateTimeSelector"; const isDevelopment = !!import.meta.env; -function resolveDateTime(value) { - if (typeof value === "string") return extractISOString(new Date(value), { offset: true }); - return value; -} - -function renderDateTime(value) { - if (typeof value === "string") return extractISOString(new Date(value)); - return value; -} - export function createTable(fullPath, { onUpdate, onThrow, overrides = {} }) { const name = fullPath.slice(-1)[0]; const path = fullPath.slice(0, -1); diff --git a/src/schemas/timezone.schema.ts b/src/schemas/timezone.schema.ts index 2528281f6..bcb089ce2 100644 --- a/src/schemas/timezone.schema.ts +++ b/src/schemas/timezone.schema.ts @@ -8,5 +8,6 @@ export default { description: "Provide a base timezone for all date and time operations in the GUIDE.", default: localTimeZone, enum: timezones, - strict: true + strict: true, + search: true }