/* eslint @typescript-eslint/no-redundant-type-constituents: off */ import { randomUUID } from "crypto"; import { FlatTemplatable, tempstream } from "tempstream"; import { FormField } from "../fields/field.js"; import { FormDataValue } from "../form-types.js"; import { FormControl, FormControlContext } from "./form-control.js"; export class Derived< Consts extends Record, > extends FormControl { role = "decoration"; constructor( public fields: FormField[], public _render: ( values: Record, consts: Consts ) => Promise, public getConsts: ( fctx: FormControlContext ) => Promise = async () => ({}) as Consts ) { super(); } async getBackendValues( fctx: FormControlContext ): Promise> { const fieldnames = this.fields.map((f) => f.name); const result = Object.fromEntries( Object.entries(fctx.data.raw_values).filter(([key]) => fieldnames.includes(key) ) ); return result; } async render(fctx: FormControlContext): Promise { // the A makes it always a valid id, as it cannot start with a number const id = "A" + randomUUID(); const consts = this.getConsts(fctx); const frontend_side_refresh_code = (values_code = "getValues()") => `render(${values_code}, consts) .then(result=>document.querySelector("#${id}").innerHTML=result)`; return tempstream /* HTML */ `
${this._render({}, await consts)}
`; } }