Page MenuHomeSealhub

checkbox-group.ts
No OneTemporary

checkbox-group.ts

import { FlatTemplatable } from "tempstream";
import { ProxyFormField } from "../fields/proxy-field.js";
import { CheckboxWithValue } from "./checkbox.js";
import { FieldGroup } from "./field-group.js";
import { FormControlContext } from "./form-control.js";
import { FormFieldControl } from "./form-field-control.js";
import { CheckboxedListField } from "../fields/checkboxed-list.js";
export type DefaultCheckboxOptions = {
id?: string;
label?: string;
hide_errors?: boolean;
readonly?: boolean;
name?: string;
checkboxNameToLabel?: (name: string) => string;
classes?: string[];
};
export class CheckboxGroup<
Options extends DefaultCheckboxOptions = DefaultCheckboxOptions
> extends FormFieldControl {
constructor(
public field: CheckboxedListField<boolean, string>,
public options: Options = {} as Options
) {
super([field]);
}
wrapGroup(
fctx: FormControlContext,
checkboxes: CheckboxWithValue[]
): Promise<FlatTemplatable> {
return new FieldGroup(checkboxes, {
label: this.options.label,
classes: [
...(this.options.classes || []),
"field-group--checkboxes",
],
}).render(fctx);
}
async render(fctx: FormControlContext): Promise<FlatTemplatable> {
const { parsed: values } = await this.field.getParsedValue(
fctx.ctx,
fctx.data
);
const checkboxes = (await this.field.generateOptions(fctx.ctx)).map(
(option) =>
new CheckboxWithValue(
new ProxyFormField(
false,
this.field.name,
false,
Object.keys(values || {}).includes(option.value)
),
{
id: this.field.name + "__" + option,
label: (
this.options.checkboxNameToLabel ||
((s: string) => s)
)(option.value),
default_value: false,
},
option.value
)
);
return this.wrapGroup(fctx, checkboxes);
}
}

File Metadata

Mime Type
text/x-java
Expires
Sun, Nov 2, 17:30 (14 h, 20 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
1030460
Default Alt Text
checkbox-group.ts (1 KB)

Event Timeline