Page MenuHomeSealhub

checkboxed-list-input.ts
No OneTemporary

checkboxed-list-input.ts

import { is, predicates } from "@sealcode/ts-predicates";
import { Context } from "koa";
import { tempstream } from "tempstream";
import { getRequiredClass, inputWrapper } from "../../utils/input-wrapper";
import { ChekboxedListField } from "../fields/field";
import { FormDataValue } from "../form";
import { FormFieldControl } from "./controls";
export class CheckboxedListInput extends FormFieldControl {
constructor(
public field: ChekboxedListField<boolean>,
public options: { label: string } = { label: field.name }
) {
super([field]);
}
async render(ctx: Context, data: Record<string, FormDataValue>) {
const { parsed: pickedValues } = await this.field.getValue(ctx, data);
if (!is(pickedValues, predicates.array(predicates.string))) {
throw new Error("picked values is not an array of strings");
}
const [options, isVisible] = await Promise.all([
this.field.generateOptions(ctx),
this.field.isVisible(ctx),
]);
const { name, required } = this.field;
return tempstream/* HTML */ `${isVisible
? Object.entries(options).map(([value, text]) =>
inputWrapper(
[name, getRequiredClass(required)],
/* HTML */ `
<input
type="checkbox"
id="${name}.${value}"
name="${name}.${value}"
${pickedValues.includes(value) ? "checked" : ""}
/>
<label for="${name}.${value}">${text}</label>
`
)
)
: ""}`;
}
}

File Metadata

Mime Type
text/x-java
Expires
Sat, Sep 20, 14:07 (1 d, 15 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
929236
Default Alt Text
checkboxed-list-input.ts (1 KB)

Event Timeline