import { Context } from "koa"; import { SimpleInput } from "../controls/controls.js"; import { FormControlContext } from "../controls/form-control.js"; import { FormDataValue } from "../form-types.js"; import { FieldParseResult } from "./field.js"; import { SimpleFormField } from "./simple-form-field.js"; function pad(n: number) { return String(n).padStart(2, "0"); } export class DateTime extends SimpleFormField< Required, SimpleInput, Date | null > { constructor(required: Required) { super(required, "datetime-local"); } public getEmptyValue(): Date | null { return null; } async parse( ctx: Context, raw_value: FormDataValue ): Promise> { const parsed_value = new Date(raw_value as string); return { parsable: true, error: null, parsed_value }; } async getSealiousCreateValue( fctx: FormControlContext ): Promise { const { parsed } = await this.getParsedValue( fctx.ctx, fctx.data.raw_values ); if (parsed == null) { return null; } return new Date(parsed).getTime(); } getControl(): SimpleInput { return new SimpleInput(this, { label: this.label || this.name, type: this.type, }); } async sealiousValueToForm( ctx: Context, value: number | null ): Promise { if (value == null) { return ""; } const d = new Date(value); return `${d.getFullYear()}-${pad(d.getMonth() + 1)}-${pad( d.getDate() )}T${pad(d.getHours())}:${pad(d.getMinutes())}`; } }