Page MenuHomeSealhub

checkbox-group.ts
No OneTemporary

checkbox-group.ts

import { FlatTemplatable } from "tempstream";
import { EnumMultipleField } from "../fields/enum-multiple.js";
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";
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: EnumMultipleField<readonly string[], boolean>,
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.getValue(
fctx.ctx,
fctx.data
);
const checkboxes = this.field.options.map(
(option) =>
new CheckboxWithValue(
new ProxyFormField(
false,
this.field.name,
false,
(values || []).includes(option)
),
{
id: this.field.name + "__" + option,
label: (
this.options.checkboxNameToLabel ||
((s: string) => s)
)(option),
default_value: false,
},
option
)
);
return this.wrapGroup(fctx, checkboxes);
}
}

File Metadata

Mime Type
text/x-java
Expires
Tue, Jul 8, 08:26 (18 h, 12 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
626121
Default Alt Text
checkbox-group.ts (1 KB)

Event Timeline