Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Alow datetime to have additional information than allowed by the input #573

Merged
merged 7 commits into from
Jan 29, 2024
25 changes: 20 additions & 5 deletions src/renderer/src/stories/JSONSchemaInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,18 @@ import { Search } from "./Search";
import tippy from "tippy.js";
import { merge } from "./pages/utils";

const dateTimeRegex = /(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2})/;

function resolveDateTime(value) {
if (typeof value === "string") {
const match = value.match(dateTimeRegex);
if (match) return `${match[1]}-${match[2]}-${match[3]}T${match[4]}:${match[5]}`;
return value;
}

return value;
}

export function createTable(fullPath, { onUpdate, onThrow, overrides = {} }) {
const name = fullPath.slice(-1)[0];
const path = fullPath.slice(0, -1);
Expand Down Expand Up @@ -1027,18 +1039,21 @@ export class JSONSchemaInput extends LitElement {
></textarea>`;
// Handle other string formats
else {
const type =
schema.format === "date-time"
? "datetime-local"
: schema.format ?? (schema.type === "string" ? "text" : schema.type);
const isDateTime = schema.format === "date-time";

const type = isDateTime
? "datetime-local"
: schema.format ?? (schema.type === "string" ? "text" : schema.type);

const value = isDateTime ? resolveDateTime(this.value) : this.value;

return html`
<input
class="guided--input schema-input ${schema.step === null ? "hideStep" : ""}"
type="${type}"
step=${isNumber && schema.step ? schema.step : ""}
placeholder="${schema.placeholder ?? ""}"
.value="${this.value ?? ""}"
.value="${value ?? ""}"
@input=${(ev) => {
let value = ev.target.value;
let newValue = value;
Expand Down
Loading